PDA

View Full Version : [SOLVED:] Request for Help with Existing Macro



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

gmaxey
03-09-2016, 02:13 PM
Can you send your two documents with the code. What you are describing doesn't make sense to me.

tstan
03-09-2016, 06:59 PM
Hi Greg,

Thanks for responding. Attached are the two documents. Document A is labeled, "Sample Document," and Document B, "Medical Terms." Really, Document A is any new document that I create from scratch.

I tested the macro again, I couldn't repeat what happened with my "listeria" example. However, I did include two words in Document A that illustrate my problem: when I highlight the word "pneumonia" in Document A, it pulls in "bronchopneumonia" because (1) it contains "pneumonia, and (2) it appears before "pneumonia" in the table. If possible, I would like to update the macro so that "pneumonia" will only pull in "pneumonia."

Thank you again for your help.

Best,

Todd

gmaxey
03-09-2016, 07:44 PM
Add the matchwholeword attribute:

With oRng.Find
.MatchWholeWord = True
.Text = strText

tstan
03-09-2016, 08:46 PM
Thank you, Greg! That solved it!

gmaxey
03-10-2016, 04:47 AM
You're welcome.