Macros to add comments to the track changes

D

Designingsally

hi i m trying to program so that when the macros encounter some words, a
comment must be highlighted voer that particular word and inside the comment
i want some message like hi.
i tried to code that but i m getting a run-time error 5935, which says
comments, endnotes and footnotes can be added to the main story. And when i
press end- the comments are appearing with no message and the small secondary
window at the bottom of the screen is appearing having a drop down box like
author. if i click end then comments are appearing again where its been
previously appearing.

Can someone help me thru this??

Sample data is This pen is good.

Code



Sub Macro1()
'
' Macro1 Macro
'
'
Selection.FInd.ClearFormatting
With Selection.FInd
.Text = "pen"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.FInd.Execute
Selection.FInd.ClearFormatting
With Selection.FInd
.Text = "pen"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Comments.Add Range:=Selection.Range
Selection.TypeText Text:="hi"
With Selection.FInd
.Text = "this"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.FInd.Execute
Selection.FInd.ClearFormatting
With Selection.FInd
.Text = "this"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Comments.Add Range:=Selection.Range
Selection.TypeText Text:="hi"
End Sub

I believe in Hope.

DesigningSally
 
G

Graham Mayor

We showed you how to use an array to find terms recently. This can easily be
adapted to add comments to the found texts

Dim oRng As Range
Dim vFindText As Variant
Dim r As Range
Dim i As Long
vFindText = Array("pen", "good", "this")
For i = 0 To UBound(vFindText)
With Selection
.HomeKey wdStory
With .Find
Do While .Execute(findText:=vFindText(i), _
Forward:=True)
Set oRng = Selection.Range
oRng.Comments.Add Range:=oRng
Selection.TypeText "hi"
oRng.Select
Loop
End With
End With
Next


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi Sally,

Graham's macro works just fine, but it can be simplified. There is no need
to add a Range object in order to save the location of the text found and
return to that location after adding text to the comment. The text can be
added directly to the comment when it is created through the Text parameter
of the Comments.Add method, according to the documentation at
http://msdn.microsoft.com/en-us/library/bb237072.aspx. This change eliminates
three lines of code in the loop and would have made your original code work
without error. The result, which more clearly demonstrates how human-created
macros can be more compact and efficient than recorded macros, is as follows.

Dim vFindText As Variant
Dim i As Long
vFindText = Array("pen", "good", "this")
For i = 0 To UBound(vFindText)
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
Do While .Execute(findText:=vFindText(i), _
Forward:=True)
Selection.Comments.Add _
Range:=Selection.Range, Text:="Hi"
Loop
End With
End With
Next
 
F

f.tecnig

regarding to the code written by P. Shelnitz
Selection.Comments.Add Range:=Selection.Range, Text:="Hi"

is any difference between
Selection.Comments.Add Range:=Selection.Range, Text:="Hi"
ActiveDocument.Comments.Add Range:=Selection.Range, Text:="Hi"

I think it's the same because both lines access the same Comments collection

why use -Selection- in -Selection.Comments- if -Range:=Selection.Range- is a
parameter needed in that case?
 
P

Pesach Shelnitz

Hi,

There is no functional difference, and it would have been better for
educational purposes to follow the example in MSDN as closely as possible. My
only excuse is that I wrote my code before I looked in MSDN to see if the
Text parameter was clearly documented and didn't notice this difference.

There is, however, a technical difference between the
ActiveDocument.Comments collection and the Selection.Comments collection
after the first comment is created, since the ActiveDocument.Comments
collection includes all the comments in the document, and the
Selection.Comments collection includes only the comments in the selection.

Thanks,
Pesach
 
D

Designingsally

thanks
--
I believe in Hope.

DesigningSally


Graham Mayor said:
We showed you how to use an array to find terms recently. This can easily be
adapted to add comments to the found texts

Dim oRng As Range
Dim vFindText As Variant
Dim r As Range
Dim i As Long
vFindText = Array("pen", "good", "this")
For i = 0 To UBound(vFindText)
With Selection
.HomeKey wdStory
With .Find
Do While .Execute(findText:=vFindText(i), _
Forward:=True)
Set oRng = Selection.Range
oRng.Comments.Add Range:=oRng
Selection.TypeText "hi"
oRng.Select
Loop
End With
End With
Next


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

damen lee turks

I tried implementing the code you provided (thanks), but it seems to be finding all instances of the words (e.g., "good" in the array returns both "good" and "goodness").

What can I do to tweak this?




Pesach Shelnitz wrote:

Hi Sally,Graham's macro works just fine, but it can be simplified.
05-Aug-09

Hi Sally

Graham's macro works just fine, but it can be simplified. There is no nee
to add a Range object in order to save the location of the text found an
return to that location after adding text to the comment. The text can b
added directly to the comment when it is created through the Text paramete
of the Comments.Add method, according to the documentation a
http://msdn.microsoft.com/en-us/library/bb237072.aspx. This change eliminate
three lines of code in the loop and would have made your original code wor
without error. The result, which more clearly demonstrates how human-create
macros can be more compact and efficient than recorded macros, is as follows

Dim vFindText As Varian
Dim i As Lon
vFindText = Array("pen", "good", "this"
For i = 0 To UBound(vFindText
With Selectio
..HomeKey wdStor
With .Fin
..ClearFormattin
Do While .Execute(findText:=vFindText(i),
Forward:=True
Selection.Comments.Add
Range:=Selection.Range, Text:="Hi
Loo
End Wit
End Wit
Nex

-
Hope this helps
Pesach Shelnit

:

Previous Posts In This Thread:

Macros to add comments to the track changes
hi i m trying to program so that when the macros encounter some words,
comment must be highlighted voer that particular word and inside the commen
i want some message like hi
i tried to code that but i m getting a run-time error 5935, which say
comments, endnotes and footnotes can be added to the main story. And when
press end- the comments are appearing with no message and the small secondar
window at the bottom of the screen is appearing having a drop down box lik
author. if i click end then comments are appearing again where its bee
previously appearing

Can someone help me thru this?

Sample data is This pen is good

Cod


Sub Macro1(

' Macro1 Macr


Selection.FInd.ClearFormattin
With Selection.FIn
..Text = "pen
..Replacement.Text = "
..Forward = Tru
..Wrap = wdFindContinu
..Format = Fals
..MatchCase = Fals
..MatchWholeWord = Fals
..MatchWildcards = Fals
..MatchSoundsLike = Fals
..MatchAllWordForms = Fals
End Wit
Selection.FInd.Execut
Selection.FInd.ClearFormattin
With Selection.FIn
..Text = "pen
..Replacement.Text = "
..Forward = Tru
..Wrap = wdFindContinu
..Format = Fals
..MatchCase = Fals
..MatchWholeWord = Fals
..MatchWildcards = Fals
..MatchSoundsLike = Fals
..MatchAllWordForms = Fals
End Wit
Selection.Comments.Add Range:=Selection.Rang
Selection.TypeText Text:="hi
With Selection.FIn
..Text = "this
..Replacement.Text = "
..Forward = Tru
..Wrap = wdFindContinu
..Format = Fals
..MatchCase = Fals
..MatchWholeWord = Fals
..MatchWildcards = Fals
..MatchSoundsLike = Fals
..MatchAllWordForms = Fals
End Wit
Selection.FInd.Execut
Selection.FInd.ClearFormattin
With Selection.FIn
..Text = "this
..Replacement.Text = "
..Forward = Tru
..Wrap = wdFindContinu
..Format = Fals
..MatchCase = Fals
..MatchWholeWord = Fals
..MatchWildcards = Fals
..MatchSoundsLike = Fals
..MatchAllWordForms = Fals
End Wit
Selection.Comments.Add Range:=Selection.Rang
Selection.TypeText Text:="hi
End Su

I believe in Hope

DesigningSally

We showed you how to use an array to find terms recently.
We showed you how to use an array to find terms recently. This can easily b
adapted to add comments to the found text

Dim oRng As Rang
Dim vFindText As Varian
Dim r As Rang
Dim i As Lon
vFindText = Array("pen", "good", "this"
For i = 0 To UBound(vFindText
With Selectio
..HomeKey wdStor
With .Fin
Do While .Execute(findText:=vFindText(i),
Forward:=True
Set oRng = Selection.Rang
oRng.Comments.Add Range:=oRn
Selection.TypeText "hi"
oRng.Select
Loop
End With
End With
Next


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Designingsally wrote:

Hi Sally,Graham's macro works just fine, but it can be simplified.
Hi Sally,

Graham's macro works just fine, but it can be simplified. There is no need
to add a Range object in order to save the location of the text found and
return to that location after adding text to the comment. The text can be
added directly to the comment when it is created through the Text parameter
of the Comments.Add method, according to the documentation at
http://msdn.microsoft.com/en-us/library/bb237072.aspx. This change eliminates
three lines of code in the loop and would have made your original code work
without error. The result, which more clearly demonstrates how human-created
macros can be more compact and efficient than recorded macros, is as follows.

Dim vFindText As Variant
Dim i As Long
vFindText = Array("pen", "good", "this")
For i = 0 To UBound(vFindText)
With Selection
..HomeKey wdStory
With .Find
..ClearFormatting
Do While .Execute(findText:=vFindText(i), _
Forward:=True)
Selection.Comments.Add _
Range:=Selection.Range, Text:="Hi"
Loop
End With
End With
Next

--
Hope this helps,
Pesach Shelnitz


:

regarding to the code written by P. Shelnitz Selection.Comments.
regarding to the code written by P. Shelnitz
Selection.Comments.Add Range:=Selection.Range, Text:="Hi"

is any difference between
Selection.Comments.Add Range:=Selection.Range, Text:="Hi"
ActiveDocument.Comments.Add Range:=Selection.Range, Text:="Hi"

I think it is the same because both lines access the same Comments collection

why use -Selection- in -Selection.Comments- if -Range:=Selection.Range- is a
parameter needed in that case?


:

Hi,There is no functional difference, and it would have been better for
Hi,

There is no functional difference, and it would have been better for
educational purposes to follow the example in MSDN as closely as possible. My
only excuse is that I wrote my code before I looked in MSDN to see if the
Text parameter was clearly documented and did not notice this difference.

There is, however, a technical difference between the
ActiveDocument.Comments collection and the Selection.Comments collection
after the first comment is created, since the ActiveDocument.Comments
collection includes all the comments in the document, and the
Selection.Comments collection includes only the comments in the selection.

Thanks,
Pesach


:

Re: Macros to add comments to the track changes
thanks
--
I believe in Hope.

DesigningSally


:

thanks for the code and explainin the difference-- I believe in Hope.
thanks for the code and explainin the difference
--
I believe in Hope.

DesigningSally


:


Submitted via EggHeadCafe - Software Developer Portal of Choice
Crypto Obfuscator for .NET - Product Review
http://www.eggheadcafe.com/tutorial...f8-f5fd987fafb1/crypto-obfuscator-for-ne.aspx
 
D

Doug Robbins - Word MVP

Use

Dim vFindText As Variant
Dim i As Long
vFindText = Array("pen", "good", "this")
For i = 0 To UBound(vFindText)
With Selection
..HomeKey wdStory
With .Find
..ClearFormatting
Do While .Execute(FindText:=vFindText(i), MatchWholeWord:=True, _
Forward:=True)
Selection.Comments.Add _
Range:=Selection.Range, Text:="Hi"
Loop
End With
End With
Next


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

in message
 
G

Graham Mayor

The macro as supplied does not differentiate between whole and part words,
you can make it do so by adding the relevant parameter to the line

Do While .Execute(findText:=vFindText(i), _
Forward:=True)

eg

Do While .Execute(findText:=vFindText(i), _
Forward:=True, MatchWholeWord:=True)

or

Do While .Execute(findText:=vFindText(i), _
Forward:=True, MatchWildcards:=True)


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

damenleeturks

Awesome. Thanks. I knew it was needed, I just didn't know where to put the
parameter.

Again, thanks.
 

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