Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to remove comments from Word files 2

Status
Not open for further replies.

richiwatts

Technical User
Jun 21, 2002
180
GB
Hi,

Does anyone know of a Macro that will remove all comments from all open files. I have over 100 Word files that I need to remove the comments that have been added. It will take ages if I have to do every file one by one.

Thanks
Rich
 
untested!

Code:
Sub foo()
    Dim notes As Collection
    Dim n As Comment
    Dim d As Document
    
    For Each d In Application.Documents
        Set notes = New Collection
        For Each n In d.Comments
            notes.Add n
        Next n
        
        For Each n In notes
            n.Delete
        Next n
        Set notes = Nothing
    Next d

End Sub
 
> why are you putting it all in a collection first?

because mutating a collection you are iterating over may lead to weird results
 
how about:

Code:
Do While ActiveDocument.Comments.Count > 0
  ActiveDocument.Comments(1).Delete
Loop

mr s. <;)

 
because mutating a collection you are iterating over may lead to weird results"

Yepp, true. But only if you refer to indexes. Not if you use a "for each" loop.
That is exactly what you are using in your "for each d in n" loop, as there you ARE mutating a collection you are iterating over.
;)

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
I have tried them all and none of them remove comments from all open documents. Rich
 
Oops. Overread that ALL OPEN passage.

Code:
Dim doc as document, cmt as comment
For each doc in Documents
  For each cmt in doc.comments
     cmt.delete
  next cmt
'*******
'optional:
  doc.save
'*******
next doc

Msgbox "All comments removed"

That it?
:)

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
Overread another thingy: "Over 100 documents"
...

You don't want to have them all open in Word, do ya?
Code:
Dim i as integer, maxnum as integer, doc as document, cmt as comment
With Application.FileSearch
    .LookIn = InputBox("Please enter path to docs ", "Path")
    .FileType = msoFileTypeWordDocuments
    .FileName = "*.doc"
    .SearchSubFolders = False
    .Execute
    maxnum = .FoundFiles.Count
End With

for i=1 to maxnum
  Set doc = Documents.Open(Application.FileSearch.FoundFiles(i))
  for each cmt in doc.comments
    cmt.delete
  next cmt
  doc.close True
next i

msgbox "All comments in all documents removed!"

Okie dokie?
;-)

[blue]Help us, join us, participate
IAHRA - International Alliance of Human Rights Advocates[/blue]
 
Thanks makeitso,

That is what I was looking ofr. You have save me hours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top