Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Batch change custom property field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Claire - 16 Oct 2006 06:40 GMT
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.
Dave Lett - 16 Oct 2006 14:09 GMT
Hi Claire,

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

Dave

>I have approximately 1400 Word documents with several custom fields in it
>one
[quoted text clipped - 19 lines]
> 2002.
> Any help is appreciated. Thanks.
Claire - 17 Oct 2006 02:09 GMT
Hi Dave,

Yes they are all in the one folder.  
Regards
Claire

> Hi Claire,
>
[quoted text clipped - 26 lines]
> > 2002.
> > Any help is appreciated. Thanks.
Dave Lett - 17 Oct 2006 18:53 GMT
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

> Hi Dave,
>
[quoted text clipped - 36 lines]
>> > 2002.
>> > Any help is appreciated. Thanks.
Claire - 23 Oct 2006 09:08 GMT
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
Dave Lett - 23 Oct 2006 15:30 GMT
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

> Hi Dave,
>
[quoted text clipped - 89 lines]
> Regards
> Claire
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.