Batch Update of Various Document Properties

B

Bill Foley

Hey Gang,

Long time no hear (or need)! Got a need using Word 2003 whereby I have
hundreds of documents that I want to be able to batch update various
document properties.

Doug provided me some code to do this but it locks up at one line. I
haven't gotten his permission to post the code yet so if anyone has code out
there that does this, I would appreciate a post. If not, when I get his
permission to post, maybe you can figure out why it works on his end but not
mine.

Thanks in advance!

Bill Foley
 
G

Graham Mayor

The basic batch processing code is as follows. oDoc is the document
currently being processed. As you have not said what you want to do with the
documents' properties, it is difficult to advise further.

And when Doug looks in, he will want to know which line causes the error!

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strFileName = Dir$(strPath & "*.doc")
While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)

'++++++++++++++++++++++++++++++++++++++++
'Do what you want with oDoc here
'++++++++++++++++++++++++++++++++++++++++

oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

Bill Foley

Thanks, Graham. The code that Doug provided is similar to yours and breaks
at:

strFileName = Dir$(strPath & "*.doc")

But you have a little more stuff here. Let me give it a go and post back.

THANKS!

Bill Foley
 
B

Bill Foley

Graham,

It worked like a champ! What I wanted to do was to do a batch replace of
the "Title" property for a bunch of files in a common folder. The following
code worked a peach:

Sub BatchProcessTitleChanges()
Dim strFileName As String
Dim strPath As String
Dim strTitle as String
Dim oDoc As Document
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , _
"List Folder Contents"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" _
Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
If Left(strPath, 1) = Chr(34) Then
strPath = Mid(strPath, 2, Len(strPath) - 2)
End If
strTitle = InputBox("Enter the new Title to be assigned to each document.",
"Batch Replace Titles")
strFileName = Dir$(strPath & "*.doc")
While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
oDoc.BuiltInDocumentProperties(wdPropertyTitle) = strTitle
'Close the modified document after saving changes
oDoc.Close SaveChanges:=wdSaveChanges
strFileName = Dir$()
Wend
End Sub


Sincerely,

Bill Foley
 

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