PDA

View Full Version : [SOLVED:] Help Creating a Macro



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

gmaxey
12-07-2015, 02:29 PM
Something like this

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim oDoc1 As Document, oDoc2 As Document
Dim strText As String
Dim strComment As String
'Assumes document A and B is opened, B was opened first, and A is the active document with a selected word.
Set oDoc1 = Documents(1)
Set oDoc2 = Documents(2)
strText = Selection.Text
Set oRng = oDoc2.Range
With oRng.Find
.Text = strText
If .Execute Then
If oRng.Information(wdWithInTable) Then
oRng.Move wdCell, 1
strComment = Left(oRng.Cells(1).Range.Text, Len(oRng.Cells(1).Range.Text) - 2)
End If
End If
End With
Selection.Comments.Add Selection.Range, strComment
lbl_Exit:
Exit Sub

End Sub

tstan
12-07-2015, 02:51 PM
Greg, thank you for responding. I just tested the macro and it adds a Comment bubble to the selected text in Document A, but doesn't paste the text from Document B. I'll continue to tinker with the macro, but if you would provide any additional advice I would appreciate it.

gmaxey
12-07-2015, 03:02 PM
Then the text must not have been found in the table.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Dim oDoc1 As Document, oDoc2 As Document
Dim strText As String
Dim strComment As String
'Assumes document A and B is opened, B was opened first, and A is the active document with a selected word.
Set oDoc1 = Documents(1)
Set oDoc2 = Documents(2)
strText = Selection.Text
Set oRng = oDoc2.Range
With oRng.Find
.Text = strText
If .Execute Then
If oRng.Information(wdWithInTable) Then
oRng.Move wdCell, 1
strComment = Left(oRng.Cells(1).Range.Text, Len(oRng.Cells(1).Range.Text) - 2)
Selection.Comments.Add Selection.Range, strComment
Else
Msgbox "Found but not in table."
End If
Else
Msgbox "Not found"
End If
End With
lbl_Exit:
Exit Sub

End Sub

tstan
12-07-2015, 03:23 PM
You're right; for some reason it's unable to find the text in the table. I kept getting the "Not found" message until I listed some of the words below the table, and then the message, "Found but not in table" started appearing. I don't actually need to have a table at all - I could combine and list the content in the first column with the content in the second. Do you think that would help?

Again, thank you for your help.

gmaxey
12-07-2015, 05:16 PM
It sounds like your table is not located in the main text story of the document. Is the table in text box or something?

tstan
12-07-2015, 07:34 PM
Greg, yes it is a table and there are no text boxes in the document. The table is listed below a heading and sentence. After testing the macro more, I discovered that I don't always get the message, "Found but not in table" for content I've listed below the table. Additionally, the macro does seem to work occasionally - copying the contents of the cell in the second column and pasting it as a Comment in Document A. But more often than not, I receive the "Not found" message.

Would it be easier if the content was not in a table? As I mentioned, I can easily combine the content from the first column with the second. I originally organized it in a table, but I can also list the content together, say by bullet or numeral.

gmaxey
12-08-2015, 04:21 AM
Send me website feedback and I will reply this evening. You can then send me the two files with a definition of one of the examples that doesn't work and I will try to determine why.

tstan
12-08-2015, 09:09 AM
Greg, thank you very much! I got it to work. The problem occurred because I was double-clicking each word in Document A before running the macro. That enlarged the selection to include an extra space. I'll take a look at your website and send feedback later tonight. Thank you again!

gmaxey
12-08-2015, 03:46 PM
It is often something simple. Glad you have it working.