Finding text from a second document

S

Sam

This is going to sound strange, but I have a piece of code that was given to me in answer to a question a while ago (not on this forum), but the code isn't complete, and I can't figure out how to make it work.

I have a document (C:\mycomments.doc) that contains a table with words or sentences on one side, and comments on the other. I want to use this document in such a way that I can look for any instance of the left-hand phrases in my active document, and insert the comments that appear to the right at that point in the active document.

The below code looks good, but nowhere is "DataDocument" defined. How do I set DataDocument = C:\mycomments.doc?

Thanks!



VB:
--------------------------------------------------------------------------------
Sub addComments()
Dim r As Range
Dim mySearchText As String
Dim myCommentText As String

For i = 1 To DataDocument.Tables(1).Rows.Count

mySearchText = DataDocument.Tables(1).Cell(i, 1).Range.Text
myCommentText = DataDocument.Tables(1).Cell(i, 2).Range.Text

Set r = ActiveDocument.Range
With r.Find
.Text = mySearchText
.Forward = True
.Wrap = wdFindStop
.MatchWholeWord = True
Do While .Execute
ActiveDocument.Comments.Add Range:=r, _
Text:=myCommentText
Loop
End With

Next i


End Sub
 
D

Doug Robbins - Word MVP

Hi Sam,

Use

Sub addComments()
Dim r As Range
Dim mySearchText As String
Dim myCommentText As String
Dim DataDocument as Document, TargetDocument as Document

Set TargetDocument = ActiveDocument
Set DataDocument = Documents.Open("Drive:\Path\Filename.doc") '.FullName for
the datadocument

For i = 1 To DataDocument.Tables(1).Rows.Count

mySearchText = DataDocument.Tables(1).Cell(i, 1).Range.Text
myCommentText = DataDocument.Tables(1).Cell(i, 2).Range.Text

Set r = TargetDocument.Range
With r.Find
.Text = mySearchText
.Forward = True
.Wrap = wdFindStop
.MatchWholeWord = True
Do While .Execute
TargetDocument.Comments.Add Range:=r, _
Text:=myCommentText
Loop
End With

Next i

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Sam said:
This is going to sound strange, but I have a piece of code that was given
to me in answer to a question a while ago (not on this forum), but the code
isn't complete, and I can't figure out how to make it work.
I have a document (C:\mycomments.doc) that contains a table with words or
sentences on one side, and comments on the other. I want to use this
document in such a way that I can look for any instance of the left-hand
phrases in my active document, and insert the comments that appear to the
right at that point in the active document.
The below code looks good, but nowhere is "DataDocument" defined. How do I
set DataDocument = C:\mycomments.doc?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top