Macro to add comments to certain words

S

Sam

Hi, I'm trying to write a macro that when run will search
through the document for certain words and add specific
comments to them.
While I've programmed in VB before, I don't know where to
start with macros. Can someone give advice, or tell me
where to look for answers?
Thanks!
 
J

Jonathan West

Hi Sam

This might help you

Creating a macro with no programming experience using the recorder
http://www.mvps.org/word/FAQs/MacrosVBA/UsingRecorder.htm

Getting To Grips With VBA Basics In 15 Minutes
http://www.mvps.org/word/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

(I realise you *do* have programming experience, but not of the Word object
model)

Word VBA is all about getting to grips with this humungous object model.
Post back here if you get stuck.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
M

Mark Tangard

Sam,

And just to nudge you a little faster into the fireswamp:

ActiveDocument.Comments.Add Range:=Selection.Range, _
Text:="This is a comment."
 
S

Sam

Ok, so I've got that far (thanks for help!). Now my
problem is that I'm trying to replace each instance of a
word, but it stops after the first one. Here's my code:

Sub test3()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "SomeWord"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Selection.Comments.Add Range:=Selection.Range
Selection.TypeText Text:="Here's my comment!
ActiveWindow.Panes(1).Activate

End Sub

It's obviosuly something simple that I'm doing wrong. Can
you see what it is?
Thanks!
 
J

Jonathan West

Sam said:
Ok, so I've got that far (thanks for help!). Now my
problem is that I'm trying to replace each instance of a
word, but it stops after the first one. Here's my code:
It's obviosuly something simple that I'm doing wrong. Can
you see what it is?

You need to put the relevant code in a loop like this.

Sub test3()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "SomeWord"
.Forward = True
.Wrap = wdFindStop 'this line is changed
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Do While Selection.Find.Execute 'this line is changed
Selection.Comments.Add Range:=Selection.Range, _
Text:="Here's my comment!
Loop ' This line is added
End Sub

I made the following changes

I've created a Do-Loop structure round the code that searches and adds the
comment

I've changed the Wrap property so that the Find doesn't start again at the
top of the document when it reaches the end.

I've modified the code that actually adds the comment, so that the one line
inserts the text of the comment without the need for a TypeText command and
changing the active pane. That will speed things up quite a bit.


The trick of this is that the Execute method of the Find object returns a
Boolean value which is True if something is found, and False otherwise. That
is used to determine whether you drop out of the loop.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 

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