I am trying to change the Category field of a series of Word documents in a directory.
Sometimes the script works, then I can run the same script with a new category value and the change does not get saved.
I get no error messages or any indication of any problem.
Here is an abbreviated version of the script. The working parts are unchanged, what is left out is the UI portion to get the target directory, new Category contents, etc.:
dim tgtDir
dim fs
dim f
dim fc
dim wrd
dim newCat
dim fl
tgtDir = "D:\Experiment\"
set fs = CreateObject("Scripting.FileSystemObject")
set f = fs.GetFolder(tgtDir)
Set fc = f.Files
set wrd = createobject("word.application")
wrd.visible = true
newCat = "New Cat Value"
for each fl in fc
fileName = """" & tgtDir & fl.Name & """"
set doc = wrd.documents.open (fileName)
doc.Builtindocumentproperties (18) = newCat
call doc.Save
call doc.close (-1)
next
wrd.Quit
As you can see, I actually try to save the file twice. Once in the doc.Save line, and then again in the doc.Close line (-1 is the constant for 'save file').
If I comment out the "doc.close" and "wrd.Quit" lines, I can look at the files and see that the change is in the Category field. But Word lets me close the files with no prompt to save, and the change is then lost, in spite of the doc.Save call.
Any ideas on what is going on here?
Sometimes the script works, then I can run the same script with a new category value and the change does not get saved.
I get no error messages or any indication of any problem.
Here is an abbreviated version of the script. The working parts are unchanged, what is left out is the UI portion to get the target directory, new Category contents, etc.:
dim tgtDir
dim fs
dim f
dim fc
dim wrd
dim newCat
dim fl
tgtDir = "D:\Experiment\"
set fs = CreateObject("Scripting.FileSystemObject")
set f = fs.GetFolder(tgtDir)
Set fc = f.Files
set wrd = createobject("word.application")
wrd.visible = true
newCat = "New Cat Value"
for each fl in fc
fileName = """" & tgtDir & fl.Name & """"
set doc = wrd.documents.open (fileName)
doc.Builtindocumentproperties (18) = newCat
call doc.Save
call doc.close (-1)
next
wrd.Quit
As you can see, I actually try to save the file twice. Once in the doc.Save line, and then again in the doc.Close line (-1 is the constant for 'save file').
If I comment out the "doc.close" and "wrd.Quit" lines, I can look at the files and see that the change is in the Category field. But Word lets me close the files with no prompt to save, and the change is then lost, in spite of the doc.Save call.
Any ideas on what is going on here?