Batch change custom property field

C

Claire

I have approximately 1400 Word documents with several custom fields in it one
of which is Last Review Date. This field was put in as a text field but it
caused a problem whereby on the screen the date was correct but when printed
the date
was printed out in the American format so 8 Dec 2006 becomes 12 Aug 2006 and
the document field was updated as well. I worked out that if I changed the
field to a Date field, this did not occur. Unfortunately I have 1400
documents which have the field as a text field and I need to change them to a
date field (and make sure that the date is in the Australian format
dd-MMM-yyyy.) Can anyone tell me how to write a macro to make these changes?
Or how I can make these changes without having to open each document up
individually? I don't have much experience writing macros (except using the
record macro in Word which won't work in this case). I am using Word 2002.
Any help is appreciated. Thanks.
 
D

Dave Lett

Hi Claire,

Are all of the files in the same folder?
Or are they at least in the same directory?

Dave
 
D

Dave Lett

Hi Claire,

You might want to try to run the following on your files. Please NOTE that I
did NOT include code to unprotect and reprotect your document. When you
reprotect you document, be sure that you set the NoReset parameter to True.
That way, you can keep all the existing data in the form. Also, you should
ONLY run this routine on a backup copy of your source files. That way, if
anything goes wrong, you still have the integrity of your source files.

Dim iCount As Integer
Dim sPath As String
Dim oDoc As Document
Dim sValue As String
sPath = "C:\Test\"

Dim MyFile As String
Dim Counter As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)

'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$(sPath & "*.*")
Do While MyFile <> ""
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
Counter = Counter + 1
Loop

'Reset the size of the array without losing its values by using Redim
Preserve
ReDim Preserve DirectoryListArray(Counter - 1)

For Counter = 0 To UBound(DirectoryListArray)
Set oDoc = Documents.Open(FileName:=DirectoryListArray(Counter))
oDoc.FormFields("LastReviewDate").Select
With Dialogs(wdDialogFormFieldOptions)
sValue = .TextDefault
.TextType = 2
.TextDefault = sValue
.TextFormat = "dd-MMM-yyyy"
.Execute
End With
Next Counter


Good luck

HTH,
Dave
 
C

Claire

Hi Dave,

Thank you for your help. I forgot to mention that the fields were custom
document properties so I altered the code to suit however I can not get the
changes to save. If you go into Word, File/Properties click on the custom
tab you can see that if you make any changes to the field you have to click
the Modify button for the field to actually change. I am not sure how to
code that bit. I have the following code: (What I am trying to do is change
the field to a date field and then taking the date from the database storing
the information and updating the field with it.)

Dim iCount As Integer
Dim sPath As String
Dim oDoc As Document
Dim sValue As String
sPath = "\\perfs1\isdept\TiwestScripts\Clean_DocTrackV1\cHECKEDin\test\"

Dim MyFile As String
Dim Counter As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
Dim strConnection As String
ReDim DirectoryListArray(1000)

'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$(sPath & "*.doc")
Do While MyFile <> ""
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
Counter = Counter + 1
Loop

If Counter < 1 Then
MsgBox "No documents"
End
End If
'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter - 1)
'Stop

ChDir sPath

For Counter = 0 To UBound(DirectoryListArray)
Set oDoc = Documents.Open(FileName:=sPath & DirectoryListArray(Counter))

For Each afield In oDoc.CustomDocumentProperties
If afield.Name = "ReviewDate" Then

If afield.Type <> 3 Then

DateChanged = "N"
Let afield.Type = 3
Open sPath & "ReviewDate.txt" For Input As #1
Do While Not EOF(1) ' Loop until end of file.
Input #1, MyDocName, MyReviewDate ' Read data into
two variables.
sMyDocName = "D" & Format(MyDocName, "0000000") & ".doc"
'Stop
If UCase(sMyDocName) = UCase(oDoc.Name) Then
Let afield.Value = Replace(MyReviewDate, """", "")
DateChanged = "Y"
Exit Do
End If
Loop
Close #1
Stop
If DateChanged = "N" Then
MsgBox (oDoc.Name & " date could not be changed '" &
afield.Value & "'")
Else
oDoc.Save

End If
End If
End If
Next afield
oDoc.Close
Next Counter
End Sub

On the stop sections I can see the fields have been updated correctly but
the odoc.Save does not seem to save the changes. Any help is appreciated.

Regards
Claire
 
D

Dave Lett

Hi Claire,

I think you need to have the .Save method in a different location. You never
save the document if "DateChanged" equals "N"; however, you do change the
document when "DateChanged" equals "N".

You also might have a look at the following:


For Counter = 0 To UBound(DirectoryListArray)
Set oDoc = Documents.Open(FileName:=sPath & DirectoryListArray(Counter))
For Each afield In oDoc.CustomDocumentProperties
If afield.Name = "ReviewDate" Then
If afield.Type <> 3 Then
With oDoc
With .CustomDocumentProperties("ReviewDate")
'''set its new value first; must be a legit date
'''then change its type
.Value = Now
.Type = 4
End With
.Save
End With


Finally, a couple of comments about variables:
1) always dimension your variables (i.e., Dim DateChanged as String)
2) I prefer to add a letter to my variables as a shorthand for their type
(e.g., Dim sDateChanged as String or Dim bDateChanged as Boolean)
3) If you can, make your variables appropriate to the information you want
to return. For example, you want a True/False value for DateChanged;
therefore, use a boolean. This would change your code to the following:


bDateChanged = False
...
If bDateChanged Then
oDoc.Save
Else
MsgBox (oDoc.Name & " date could not be changed '" & afield.Value & "'")
End If
....

HTH,
Dave
 

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