vba Word BuiltInDocumentProperties 不会改变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15932902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:32:16  来源:igfitidea点击:

Word BuiltInDocumentProperties don't change

vbaword-2003

提问by Natzely

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.

我在 Word 2003 中有一个宏,它将打开文档的属性字段应用于同一文件夹中的所有 .doc。该代码工作一次。如果我创建一个文件夹,在该文件夹中创建三个新文档,打开文档并运行宏,它会起作用。如果我在该文件夹中打开一个文档并再次运行该宏,它只会更改运行该宏的活动文档的属性。

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

该宏位于 Normal 模板的模块中。

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.

提前致谢。

回答by Natzely

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

好的,我现在可以使用了。Word 有一个.Saved布尔值,显然如果它已经设置为 true,当您使用.Save和通过 VBA 更改属性时,它不会保存更改,这似乎不算是一个足够重要的更改来设置.Saved为 false。也许.SaveAs仍然有效。无论如何,我.Saved = False在之前添加.Save,现在它工作得很好。

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.

只是想让任何人知道谁可能会在某个时候思考同样的事情。这可能只是确保文件始终保存的好方法。