Word BuiltInDocumentProperties don't change

2019-06-27 04:41发布

问题:

I have a macro in Word 2003 that applies the property fields of the open document to all the .doc's in the same folder. The code works once. If I create a folder, create three new documents in that folder, open of the documents and run the macro, it would work. If I open a document in that folder and run the macro again, it would only change the properties of the active document that ran the macro.

The macro is located in a module of the Normal template.

The code:

title = ActiveDocument.BuiltInDocumentProperties("Title")
subject = ActiveDocument.BuiltInDocumentProperties("Subject")
author = ActiveDocument.BuiltInDocumentProperties("Author")
manager = ActiveDocument.BuiltInDocumentProperties("Manager")
company = ActiveDocument.BuiltInDocumentProperties("Company")
category = ActiveDocument.BuiltInDocumentProperties("Category")
keywords = ActiveDocument.BuiltInDocumentProperties("Keywords")
comments = ActiveDocument.BuiltInDocumentProperties("Comments")

fileDirectory = ActiveDocument.Path

vFile = Dir(fileDirectory & "\*.doc")

Do While vFile <> ""
    Set wordDoc = Documents.Open(fileDirectory & "\" & vFile)
    With wordDoc
        .BuiltInDocumentProperties("Title") = title
        .BuiltInDocumentProperties("Subject") = subject
        .BuiltInDocumentProperties("Author") = author
        .BuiltInDocumentProperties("Manager") = manager
        .BuiltInDocumentProperties("Company") = company
        .BuiltInDocumentProperties("Category") = category
        .BuiltInDocumentProperties("Keywords") = keywords
        .BuiltInDocumentProperties("Comments") = comments
        .Save
        .Close
    End With
    vFile = Dir
Loop

I'm not sure if it has something with the way I'm opening or saving the files. At least if it didn't work at all I would know the code is just wrong, but since it works on a new document at least once... I have no idea.

Thanks in advance.

回答1:

Alright, I have it working now. Word has a .Saved boolean which apparently if it is set to true already it won't save the changes when you use .Save and changing the properties through VBA doesn't seem to count as a important enough change to set the .Saved to false. Maybe .SaveAs would still work though. Anyways, I added .Saved = False before the .Save and now it is working just fine.

Just wanted to let anyone know who might ponder the same thing at some point. This might just be a decent way to make sure that the file always saves.



标签: vba word-2003