Link to home
Start Free TrialLog in
Avatar of chassouna
chassounaFlag for Switzerland

asked on

VBA Word Macro: skip write- and/or password-protected files

Hi there

The following Word VBA Macro Code changes the AttachedTemplate Path of all Documents in a specified folder and its subfolders to the NormalTemplate (got the code from another EE article). Everything works fine unless a write- and/or password-protected doc comes up. Is there a possibility to skip such documents so the process does not wait at these files and expecting an action? Also when another user has opened the document, this one should be skipped.

Optional: The macro should only change documents that have a specific path pattern in the Attached Template Path (e.g. "\\servername\..."). These documents and its paths should be logged in a file (txt, log, doc, ...).

Thanks for assistance!
Chris
Sub CallChangeTemplates()
 
  ChangeTemplates "c:\temp\docs", "*.doc*"
End Sub
 
Sub ChangeTemplates(strFolder As String, strFilePattern As String)
    Dim strFileName As String
    Dim strFolders() As String
    Dim iFolderCount As Integer
    Dim i As Integer
    Dim Doc As Document
    'collect child folders
    strFileName = Dir$(strFolder & "\", vbDirectory)
    Do Until strFileName = ""
        If (GetAttr(strFolder & "\" & strFileName) And vbDirectory) = vbDirectory Then
            If Left$(strFileName, 1) <> "." Then
                ReDim Preserve strFolders(iFolderCount)
                strFolders(iFolderCount) = strFolder & "\" & strFileName
                iFolderCount = iFolderCount + 1
            End If
        End If
        strFileName = Dir$()
    Loop
   
    'process files in current folder
    strFileName = Dir$(strFolder & "\" & strFilePattern)
    Do Until strFileName = ""
            DoEvents
            Set Doc = Documents.Open(strFolder & "\" & strFileName)
                Doc.AttachedTemplate = NormalTemplate
                Doc.Close wdSaveChanges
        strFileName = Dir$()
    Loop
   
    'look through child folders
    For i = 0 To iFolderCount - 1
        ChangeTemplates strFolders(i), strFilePattern
    Next i
End Sub

Open in new window

Avatar of Jeews
Jeews
Flag of Sri Lanka image

put a "on error resume next" statement in the line 29 and you can write the name of the file which failed to open to a log and refer later.
Avatar of chassouna

ASKER

With the "on error resume next" option the macro continues processing the files after confirming an action for the specific file with password protect or file lock. This is better than canceling. But wat I want is that no action is required for those docs, that they are skipped without any interaction. The goal is to run the macro on a file share over night and so it should not be interrupted.
ASKER CERTIFIED SOLUTION
Avatar of Jeews
Jeews
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please initialize lcCurrentDir variable with the path you want ie: according to the code you pasted this should be "c:\temp\docs"

Sorry for the inconvenience

Awesome! That's what I was looking for... Thank you so much!
Awesome! That's what I was looking for... Thank you so much!