tstan
03-09-2016, 11:33 AM
Hi all,
I need help with a macro that Greg Maxey kindly created for one of my previous posts. It works great, however, I've run into some trouble recently. The macro:
Step 1: Copies text I have selected in one document ("Document A")
Step 2: Activates the second document ("Document B") and perform a search of the copied text
Step 3: Selects the first instance of the copied text (which happens to be listed in the first column of a table)
Step 4: Tabs (or moves) to the adjacent cell and makes a selection
Step 5: Copies the selection
Step 6: Activates Document A
Step 7: Adds a comment to the selected word from the first step
Step 8: Pastes the copied text from Document B in the Comment bubble
In my previous post, I failed to indicate that some of the words in Document B are spelled the same for at least part of the word. So, for instance, the macro may pull the word "listeria" from Document B when I selected the word "lisinopril" in Document A. I assume this is happening because both begin with "lis" and "listeria" appears in the table before "lisinopril."
Can anyone help me with the below macro so that it only selects the word from Document B that matches the whole word in Document A? For example, once I select "lisinopril" in Document A, the macro will only search and select "lisinopril"? Thank you in advance!
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
I need help with a macro that Greg Maxey kindly created for one of my previous posts. It works great, however, I've run into some trouble recently. The macro:
Step 1: Copies text I have selected in one document ("Document A")
Step 2: Activates the second document ("Document B") and perform a search of the copied text
Step 3: Selects the first instance of the copied text (which happens to be listed in the first column of a table)
Step 4: Tabs (or moves) to the adjacent cell and makes a selection
Step 5: Copies the selection
Step 6: Activates Document A
Step 7: Adds a comment to the selected word from the first step
Step 8: Pastes the copied text from Document B in the Comment bubble
In my previous post, I failed to indicate that some of the words in Document B are spelled the same for at least part of the word. So, for instance, the macro may pull the word "listeria" from Document B when I selected the word "lisinopril" in Document A. I assume this is happening because both begin with "lis" and "listeria" appears in the table before "lisinopril."
Can anyone help me with the below macro so that it only selects the word from Document B that matches the whole word in Document A? For example, once I select "lisinopril" in Document A, the macro will only search and select "lisinopril"? Thank you in advance!
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