Our company Logo has been changed. and we have over 5000 templates (.doc, .docx, .dotx, .xlsx, etc) Some of the documents are pw protected, others do not.
A ex-colleague before me created these (the person is not active in the company anymore)
So, I've have "created" a VBA code that semi works. This section is the same for all 3 macros. (only the Call changes)
Sub RemovePassword()
Dim strPath As String
Dim strFile As String
Dim doc As Document
On Error GoTo ErrHandler
'Batch process to go through all files in a selected folder
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select a folder with Word documents"
If .Show = False Then
MsgBox "You didn't select a folder.", vbInformation
Exit Sub
End If
strPath = .SelectedItems(1)
End With
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
Application.ScreenUpdating = False
strFile = Dir(strPath & "*.doc")
Do While strFile <> ""
Set doc = Documents.Open(strPath & strFile)
'Call Macro (code) to process (replace only the name)
Call RemovePwd
strFile = Dir
doc.Save
doc.Close
Loop
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
End Sub
Sub RemovePwd()
'Remove existing Pwd
ActiveDocument.Unprotect Password:="Password" *'not the real pw'*
End Sub
This one removes the password of the .doc documents in a selected folder (code works) I have 2 issues with this code.
- When a document is not password protected this macro skips all documents in the selected folder. So, the documents remain locked. I have to find all the not protected ones manually remove them out of the folder or add the protection to them as well.
can the code be adjusted so when a document has no password it skips that document and continues to the next?
- can the code be adjusted that this happens for all extension for Word and/or Excel.
The other 2 macro's
Removing the old Logo
Sub RemoveOldLogo()
Dim hdr As HeaderFooter
Dim sec As Section
Dim sh As Shape
'Loop through all existing headers in document
For Each sec In ActiveDocument.Sections
For Each hdr In sec.Headers
Set rng = hdr.Range
For Each sh In hdr.Shapes
'Delete found Logo
sh.Delete
Next sh
Next hdr
Next sec
End Sub
Adding the new logo
Sub AddNewLogo()
'Copy Logo from Master template
ChangeFileOpenDirectory "C:\MASTER_TEMPLATE\"
Documents.Open FileName:= _
"C:\MASTER_TEMPLATE\MASTER_Logo.doc", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageHeader
Selection.WholeStory
Selection.Copy
ActiveWindow.Close
'Paste Logo
ActiveWindow.ActivePane.View.SeekView = wdSeekFirstPageHeader
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End Sub
All these macros combined to run with the following macro
Sub RunAllMacros()
RemovePassword
RemoveOldLogo
AddNewLogo
End Sub
Like I said these code work with the exception of when 1 document in a folder is not pw protected, it doesn't removed it from the other documents in that folder that do have a pw.
If someone has a better solution on how to do this that info is welcome too!
I'm not very experience with VBA and such, this is all found online, adjusted and combined from different code.
Thanks
Kr, Thierry