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