Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-18-2018, 03:25 PM
Peterson Peterson is offline Error 4605 when looping through files in folder and deleting comments Windows 7 64bit Error 4605 when looping through files in folder and deleting comments Office 2016
Competent Performer
Error 4605 when looping through files in folder and deleting comments
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default Error 4605 when looping through files in folder and deleting comments

The macro below loops through all Word files in a folder, calls another macro (or multiple others), then saves/closes. A called macro, also below, simply deletes comments in the file; however, I'm getting Run-time error 4605 (The command is not available); the debugger highlights the ActiveDocument.DeleteAllComments line. I tried to modify it based on some web searching, but I'm coming up short, having tried to modify the code to ignore the error.

Testing the macros on a set of files just now, it failed partway through the files; I can't determine how the file that was opened when it failed differs from the others. I then tried to run the macro again and it failed on the first file.

How can I resolve this?

Code:
Sub LoopAllWordFilesInFolder()

'Loop through all Word files in a user-selected folder and perform other macros.

'Adapted from: https://www.thespreadsheetguru.com/the-code-vault/2014/4/23/loop-through-all-excel-files-in-a-given-folder

    Dim docDocument As Document
    Dim strPath As String
    Dim strFile As String
    Dim strExtension As String
    Dim FolderPicker As FileDialog
            
    'Optimize macro speed
    Application.ScreenUpdating = False
    
    'Get target folder path from user
    Set FolderPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FolderPicker
            .Title = "Choose the folder containing your files"
            .AllowMultiSelect = False
                If .Show <> -1 Then GoTo NextCode
                                
                strPath = .SelectedItems(1) & "\"
        End With
        
'In case the user cancels
NextCode:
    strPath = strPath
    If strPath = "" Then GoTo ResetSettings
    
    strExtension = "*.doc*"
    
    'Target path with file extension
    strFile = Dir(strPath & strExtension)
    
    'Loop through each file in the folder
    Do While strFile <> ""
    
        'Set variable equal to opened document
        Set docDocument = Documents.Open(FileName:=strPath & strFile)
    
        'Use DoEvents to ensure document has opened before moving on to next line of code.
        '(https://answers.microsoft.com/en-us/msoffice/forum/msoffice_powerpoint-mso_other/vba-doevents/0bdc05d5-a54e-49a8-875d-5ee962acf78e)
        DoEvents
        
        '################################################################
        'CALL OTHER MACRO(S) HERE.
            
            Call CommentsDeleteAll
                      
        '################################################################
        
        'Save and close document
        docDocument.Close savechanges:=True
        
        'Ensure document has closed before moving on to next line of code
        DoEvents
        
        'Get next file name
        strFile = Dir
    Loop
                
    MsgBox "The macro has looped through the files in the folder you chose and completed the tasks."
        
'Reset macro optimization settings
ResetSettings:
    Application.ScreenUpdating = True
    
End Sub



Code:
Sub CommentsDeleteAll()
    
    ActiveDocument.DeleteAllComments

End Sub
Reply With Quote
  #2  
Old 04-18-2018, 04:22 PM
macropod's Avatar
macropod macropod is offline Error 4605 when looping through files in folder and deleting comments Windows 7 64bit Error 4605 when looping through files in folder and deleting comments Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
With ActiveDocument
  If .Comments.Count > 0 Then .DeleteAllComments
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-19-2018, 08:45 AM
Peterson Peterson is offline Error 4605 when looping through files in folder and deleting comments Windows 7 64bit Error 4605 when looping through files in folder and deleting comments Office 2016
Competent Performer
Error 4605 when looping through files in folder and deleting comments
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

Perfect -- thank you!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping through a folder of PDF files and saving them as docx ballpoint Word VBA 5 11-22-2018 05:18 AM
Adding comments to PPT slides causes error Render9 PowerPoint 0 02-09-2016 08:46 AM
Deleting imap files from program files Sharilyn Outlook 0 05-12-2014 11:35 AM
PasteAppendTable not available (Run-Time Error 4605) q_scribe Word VBA 1 08-12-2013 09:56 AM
Code Error - Hiding comments and revisions silvrwoman Word VBA 6 03-24-2012 10:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft