Forum Discussion

demaionewton's avatar
demaionewton
Copper Contributor
May 24, 2023

VBA: DoCmd.RunCommand acCmdCopy not working

I have a Rich Text Formatted Access field on a subform that I need to copy to the clipboard. 

 

Because Writeline doesn't have a FormattedText option, I can't simply write the field to the clipboard. Doing that copies it with the code and unformatted text.

 

So instead, I'm trying to use  DoCmd.RunCommand acCmdCopy

 

Here's the code:

 

'set focus on subform

  Me![Case Attachments subform].SetFocus

'go to the last record on the subform
  DoCmd.GoToRecord , , acLast
'Copy the Field value
  DoCmd.RunCommand acCmdCopy

 

The copy isn't working. The clipboard is empty.

 

Interestingly, when I add a debug test right before the acCmdCopy:

 

  Dim tryit As String
  tryit = Me![Case Attachments subform].Form![Doc Description].Text
  MsgBox (tryit)

 

it shows me that tryit equals the text I need to copy. I.e., I have the right field selected in its entirety.

 

Anyone have any suggestions on how to get it to work or alternatives to copy the rich text to the clipboard so it can be paste correctly. (And, before you offer, NO, I don't want to write VBA code to jump over to Word, create a new file, paste it there, copy it, and delete the Word file).

    • demaionewton's avatar
      demaionewton
      Copper Contributor

      Sorry, my bad. I meant writeline, not Writeln.  Writeline writes the text following it to the clipboard. Here's an example:
      Private Sub bCopyHelloWorldtoClipboard_Click()
      Dim WshShell, oExec, oIn

      Set WshShell = CreateObject("WScript.Shell")
      Set oExec = WshShell.Exec("clip")
      Set oIn = oExec.stdIn

      oIn.WriteLine "Hello World"
      oIn.Close

      End Sub

      • demaionewton's avatar
        demaionewton
        Copper Contributor

        The problem with Writeln is that it writes the rich text field with all of the html code exposed.  Only DoCmd.RunCommand acCmdCopy will copy rich text to the clipboard. demaionewton 

  • Hi,

     

    > I.e., I have the right field selected in its entirety.

     

    Not necessarily, as the Text property would always return the complete text in the text box regardless of the actual selection as long as it is the active control in the subform.

     

    I would try to first explicitely set the focus to a different control in the subform and then explicitely to the desired control. With standard cursor behaviour set in the options, this should ensure that the complete text in the control is selected for the copy action.

     

    Servus
    Karl
    ****************

    Access Bug Trackers

    Access News
    Access DevCon

    • demaionewton's avatar
      demaionewton
      Copper Contributor
      We are thinking alike. Here's what I tried to be sure it was all selected. The following code focuses on the subform field, selects the Start & Length, puts the Text in a string and shows it in a message box. The copy in the message box is correct. BUT, the next line DoCmd.RunCommand acCmdCopy does NOT copy to the clipboard.

      Me![Case Attachments subform].Form![Doc Description].SetFocus
      Me![Case Attachments subform].Form![Doc Description].SelStart = 0
      Me![Case Attachments subform].Form![Doc Description].SelLength = Len(Me![Case Attachments subform].Form![Doc Description].Text)
      Dim tryit As String
      tryit = Me![Case Attachments subform].Form![Doc Description].Text
      MsgBox (tryit)
      DoCmd.RunCommand acCmdCopy

Resources