tstan
12-07-2015, 01:04 PM
Hello,
I am new to creating macros and need help. I want to create a macro that will streamline the copying and pasting I do between documents. Specifically, I need the macro to do the following:
Copy text I have selected in one document (for this example, let's call it "Document A")
Activate the second document ("Document B") and perform a search of the copied text
Select the first instance of the copied text (which happens to be listed in the first column of a table)
Tab (or move) to the adjacent cell and make a selection
Copy the selection
Activate Document A
Add a comment to the selected word from the first step
Paste the copied text from Document B in the Comment bubble
I am struggling with getting the copied text from the Clipboard. I have read that I need to activate the available reference, "Microsoft Forms 2.0 Object Library," which I have done, and use the following code:
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
strPaste = DataObj.GetText(1)
I'm not exactly sure how to use the above code with what I have created below. Can someone please help?
Sub CopyandPasteMacro()
If Documents.Count <> 2 Then
MsgBox "You must have two documents open!"
Exit Sub
End If
Set ThisDoc = ActiveDocument
If ThisDoc = Documents(1) Then
Set OtherDoc = Documents(2)
Else
Set OtherDoc = Documents(1)
End If
OtherDoc.Activate
Selection.MoveRight Unit:=wdCell
Selection.Copy
ThisDoc.Activate
Selection.Comments.Add Range:=Selection.Range
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Set ThisDoc = Nothing
Set OtherDoc = Nothing
End Sub
I am new to creating macros and need help. I want to create a macro that will streamline the copying and pasting I do between documents. Specifically, I need the macro to do the following:
Copy text I have selected in one document (for this example, let's call it "Document A")
Activate the second document ("Document B") and perform a search of the copied text
Select the first instance of the copied text (which happens to be listed in the first column of a table)
Tab (or move) to the adjacent cell and make a selection
Copy the selection
Activate Document A
Add a comment to the selected word from the first step
Paste the copied text from Document B in the Comment bubble
I am struggling with getting the copied text from the Clipboard. I have read that I need to activate the available reference, "Microsoft Forms 2.0 Object Library," which I have done, and use the following code:
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
strPaste = DataObj.GetText(1)
I'm not exactly sure how to use the above code with what I have created below. Can someone please help?
Sub CopyandPasteMacro()
If Documents.Count <> 2 Then
MsgBox "You must have two documents open!"
Exit Sub
End If
Set ThisDoc = ActiveDocument
If ThisDoc = Documents(1) Then
Set OtherDoc = Documents(2)
Else
Set OtherDoc = Documents(1)
End If
OtherDoc.Activate
Selection.MoveRight Unit:=wdCell
Selection.Copy
ThisDoc.Activate
Selection.Comments.Add Range:=Selection.Range
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Set ThisDoc = Nothing
Set OtherDoc = Nothing
End Sub