Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mods to MS Word files via VBScript - File Save not working

Status
Not open for further replies.

JasReich

Programmer
Apr 21, 2005
3
US
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?
 
You may try this:
doc.Saved = False
doc.Save
doc.Close True

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank you! That does seem to help (more tests under way)!

I guess I'm not sure why changing the Category doesn't set the Saved flag false in itself, but I also guess it may not really matter... :)

Many thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top