问题描述
Microsoft word中是否有一个功能(宏)可以自动越过剪贴板中的所有内容? 我正在研究宏,以检查剪贴板中是否有新内容并将其粘贴到单词中,而无需我参与该过程
解决方法
请尝试这种方法,
-
在您的文档项目或Normal.dot中插入一个表单。必须仅执行此操作以接收引用(“ Microsoft Forms 2.0对象库”)。现在您可以删除它。参考将保留。从理论上讲,您可以直接添加引用,但有时Word不会在可能添加的引用之间显示引用;
-
插入标准模块并粘贴下一个代码:
Sub testPutfromclipboard()
Dim clp As DataObject,nextLine As String
'nextLine = vbCrLf
Set clp = New DataObject
clp.GetFromClipboard
If clp.GetFormat(1) Then
If clp.GetText <> Empty Then
ActiveDocument.Content.InsertAfter Text:=nextLine & clp.GetText
clp.SetText Text:=Empty: clp.PutInClipboard
End If
End If
End Sub
- 运行它,剪贴板将被粘贴到活动文档文本的末尾。
如果您希望将其粘贴到下一行,只需取消注释行'nextLine = vbCrLf
...
您可以每秒轮询一次。
Public Declare Function GetClipboardOwner Lib "user32" () As Long
Public Declare Function GetClipboardSequenceNumber Lib "user32" () As Long
RTFString = RTFString & "\b1 Clipboard Sequence Number: \b0 " & CStr(GetClipboardSequenceNumber()) & "\par" & vbCr
RTFString = RTFString & "\b1 Clipboard Owner Window Handle: \b0 " & CStr(GetClipboardOwner()) & "\par" & vbCr
Msgbox RTFString
还有一种更好的方法,但是它要求您对表单进行子类化。参见https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-addclipboardformatlistener
您不需要对表单进行子类化,可以对Excel的主窗口进行子类化。