9

I've used a routine for years to put a plain text string into the clipboard that I can paste into another program such as:

targetData.SetText "This is a plain text string"
targetData.PutInClipboard

When I use this in Excel Office 2013 the data isn't in the clipboard and therefore I can't paste it. This never happened in prior versions.

Under closer inspection I've found that the string does go to the clipboard but as "System String" but not as "Text" or "Unicode Text".

BUT... about 10% of the time it acutally works as it should putting the string into the clipboard as "Text".

Any ideas??

4

2 回答 2

7

user2140261 的评论是正确的解决方案:

如何:将信息发送到剪贴板

(以下内容只是从上面的链接复制而来)

如果需要将表单或报表上的活动控件的内容复制到剪贴板,只需要这段代码:

Private Sub cmdCopy_Click() 
   Me!txtNotes.SetFocus 
   DoCmd.RunCommand acCmdCopy 
End Sub

但是,这是您旧例程所需的替换:

1.创建一个模块,将其命名为“WinAPI”或其他名称,将以下代码放入其中:

Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

2. 在定义旧例程的模块中,用以下代码替换旧例程:

Function ClipBoard_SetData(MyString As String) 
   Dim hGlobalMemory As Long, lpGlobalMemory As Long 
   Dim hClipMemory As Long, X As Long 

   ' Allocate moveable global memory. 
   '------------------------------------------- 
   hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1) 

   ' Lock the block to get a far pointer 
   ' to this memory. 
   lpGlobalMemory = GlobalLock(hGlobalMemory) 

   ' Copy the string to this global memory. 
   lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString) 

   ' Unlock the memory. 
   If GlobalUnlock(hGlobalMemory) <> 0 Then 
      MsgBox "Could not unlock memory location. Copy aborted." 
      GoTo OutOfHere2 
   End If 

   ' Open the Clipboard to copy data to. 
   If OpenClipboard(0&) = 0 Then 
      MsgBox "Could not open the Clipboard. Copy aborted." 
      Exit Function 
   End If 

   ' Clear the Clipboard. 
   X = EmptyClipboard() 

   ' Copy the data to the Clipboard. 
   hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory) 

OutOfHere2: 

   If CloseClipboard() = 0 Then 
      MsgBox "Could not close Clipboard." 
   End If 

End Function

3. 然后,这样称呼它:

' doesn't work on Windows 8: targetData.SetText "This is a plain text string"
'doesn't work on Windows 8: targetData.PutInClipboard
ClipBoard_SetData ("This is a plain text string")
于 2014-01-23T16:33:37.863 回答
2

您应该将“Microsoft forms 2.0 object library”添加到您的引用中,然后您就可以使用此功能。您可以通过转到 Excel/developer/Visual basic(或您的 VBE 模块)并在工具/参考中选择浏览,然后在您的 system32 文件夹中搜索“FM20.DLL”文件,然后在最后选择此文件来执行此操作将出现参考列表中的“Microsoft forms 2.0 object library”。激活那个。现在一切都和以前一样好;)

于 2013-07-15T09:12:32.990 回答