PDA

View Full Version : Update all files in folder



clhare
02-20-2012, 04:37 PM
Hi all,

I need to do a search/replace in all files in a specific folder (including its subfolders). Some of the files are locked (without a password) and some are not. Is there a way to use a macro to go through each file in the folder, determine whether it is locked or unlocked initially, do the search/replace, and then put the lock state back the way it was initially?

Any help is greatly appreciated!

fumei
02-21-2012, 05:58 PM
Locked without a password?

macropod
02-23-2012, 04:33 AM
Maybe formfield protection and/or read-only recommended

fumei
02-23-2012, 02:56 PM
we need to know...

clhare
02-27-2012, 09:19 AM
Some were locked using Tools > Protect Document without a password... just to make it easy to tab from formfield to formfield in the document.

Frosty
02-27-2012, 11:07 AM
The short answer is: Yes, you can do this, and it shouldn't be too difficult. The code for looping through files in a folder isn't hard to set up (probably using the FileSystemObject, but there are multiple strategies for this).

Once you get your document open, you need to check the ActiveDocument.ProtectionType ... there are a couple of values, sounds like yours would be wdAllowOnlyFormFields.

If that protection exists, then you would want to flag for it, do your find/replace, and then re-protect if you set your flag using:

ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

Make sure you use that NoReset parameter, or you will wipe out the data in your form fields.

Even if there was a password, you can embed that in the code, but it would get tricky if you had multiple passwords (which would require you to try to identify the document while it was locked, which may or may not be possible).

This is pseduo code, but it would be along the lines of...


If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
Set bProtected = True
ActiveDocument.Unprotect
End If

'do find replace stuff

If bProtected Then
ActiveDocument.Protect wdAllowOnlyFormFields, True
End If

ActiveDocument.Save
ActiveDocument.Close

'go to your next document
Is that enough to get you going, or do you need more?