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
Word BuiltInDocumentProperties don't change
提问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 .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.
好的,我现在可以使用了。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.
只是想让任何人知道谁可能会在某个时候思考同样的事情。这可能只是确保文件始终保存的好方法。