Deleting comments and accepting all changes for word document from excel

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
I am not able to test the code at the moment but I was curious if anyone knows if the following code would delete all comments in a word document from excel vba. The line which has me worried is Set oComments = WordApp.WordDoc.Comments. I am not sure if that is the correct way to set the oComment object variable or if its even allowed. I am also curious if WordApp.WordDoc.Revisions.AcceptAll is the appropriate syntax.

Code:
Sub FinalizeWordDoc()
   
'Open Word Doc and replace standard language
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim n As Long
Dim oComments As Comments


Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open("c/blah blah.docx")
WordDoc.Visible=1

Set oComments = WordApp.WordDoc.Comments


For n = oComments.Count To 1 Step -1
    oComments(n).Delete
Next 'n

'Remove track changes
WordApp.WordDoc.Revisions.AcceptAll


Set oComments = Nothing
Set WordDoc = Nothing
Set WordApp = Nothing

End Sub
 
Last edited:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I was able to test it and get it to work. The only problem I dont like is I have to declare oComments as a Object but I know I can declare it as something more specific but I cant find what. Any ideas?

Code:
Sub FinalizeWordDoc()
   
    'Open Word Doc and replace standard language
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Set WordApp = CreateObject("Word.Application")
    Set WordDoc = WordApp.Documents.Open("c://asdfasdfasf")
    Dim n As Long
    Dim oComments As Object


    Set oComments = WordDoc.Comments
    
    For n = oComments.Count To 1 Step -1
        oComments(n).Delete
    Next 'n


    'Remove track changes
    WordDoc.Revisions.AcceptAll
  
    WordApp.Documents.Save
    
    WordApp.Visible = True
        
    Set oComments = Nothing
    Set WordDoc = Nothing
    Set WordApp = Nothing


End Sub
 
Upvote 0
The problem is that you haven't Dimmed the comment object properly, for example:
Dim WdCmnt As Word.Comment
FWIW, your code could be simplified to:
Code:
Sub FinalizeWordDoc()
Dim WdApp As New Word.Application, WdDoc As Word.Document
Set WdDoc = WdApp.Documents.Open("c/blah blah.docx", ReadOnly:=False, AddToRecentFiles:=False)
With WdDoc
  .Revisions.AcceptAll
  While .Comments.Count > 0
    .Comments(1).Delete
  Wend
End With
WdApp.Visible = True
Set WdDoc = Nothing: Set WdApp = Nothing
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,306
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top