Updating docproperty value in multiple documents

V

vicki

Does anyone know if it is possible to automatically update
a docproperty value in multiple documents at once? I have
a huge document I had to break into smaller docs but each
doc needs to have the same version number. This is handled
with a version number that is set in the Custom tab in the
doc's Properties dialog box.
 
G

Greg Maxey

Vicki,

This is rough and I hope one of the macor heavy hitters will come along and
clean it up.

I found a macro for doing batch find and replace in a directory. At first I
thought I could just delete the find and replace bit and replace it with a
command to set a docProperty. The problem I faced is that by simply setting
a docProperty Word doesn't regonize the document has changed. I had to set
a docProperty and then trick Word into thinking I made a recognized change
(replaced all spaces with a space ;-) ).

Here it is:

Option Explicit
Public Sub BatchReplaceDocProperties()

Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Expr1 As String

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges
'Define Path (set to meet your needs)
PathToUse = "D:\My Documents\Word Documents\Word Tips\Macros\"
'Solicit User Input (Set prompt and Title to meet your needs)
Expr1 = InputBox("Enter the Document Subject:", "Subject")
'Set the directory and type of file to batch process
myFile = Dir$(PathToUse & "*.doc")
While myFile <> ""
'Open document
Set myDoc = Documents.Open(PathToUse & myFile)
'Trick Word into thinking the document changed
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
'Set DocProperty
With myDoc
'This next line set the "Title" property to the value of Expr1
'Adapt to meet your needs
.BuiltInDocumentProperties("Title").Value = Expr1
End With
myDoc.Close SaveChanges:=wdSaveChanges
'Process next file in folder
myFile = Dir$()
Wend
End Sub

HTH

If anyone can streamline this code or help me find a better way of saving
the docProperty without having to use the find and replace bit please let me
know.
 
D

Doug Robbins - Word MVP

Hi Greg,

In place of

'Trick Word into thinking the document changed
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

Use

myDoc.Saved = False

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top