【问题标题】:Why won't this loop to add CustomDocumentProperties work?为什么这个添加 CustomDocumentProperties 的循环不起作用?
【发布时间】:2018-02-20 03:47:01
【问题描述】:

我正在尝试将一些自定义文档属性添加到 word 文档文件夹中。

我知道循环本身可以正常工作,因为我使用具有不同代码的相同循环来修改并更新预先存在的自定义文档属性。

添加自定义文档属性的代码也有效,我通过在它自己的宏中为单个文档运行它来测试它,效果很好。

由于循环有效并且循环中的代码也有效,我就是不知道它有什么问题。

代码如下:

Sub add_custom_docproperties()

Dim file
Dim path As String

Dim filepath As Variant
filepath = InputBox("Please enter the filepath for the files you want to 
update.", "Input Filepath", "Copy filepath here...")
Select Case StrPtr(response)
Case 0
endednotification = MsgBox("The macro has been ended.", , "Notification")
Exit Sub
Case Else
End Select

path = filepath & "\"

file = Dir(path & "*.*")

'Application.ScreenUpdating = False

Do While file <> ""

    Documents.Open FileName:=path & file

        Check = MsgBox(path & file, , "Check")

        ActiveDocument.CustomDocumentProperties.Add Name:="firstdocprop", 
          _LinkToContent:=False, Type:=msoPropertyTypeString, Value:="The First One"
        ActiveDocument.CustomDocumentProperties.Add Name:="seconddocprop", 
          _LinkToContent:=False, Type:=msoPropertyTypeString, Value:="Second"
        ActiveDocument.CustomDocumentProperties.Add Name:="thirddocprop", 
          _LinkToContent:=False, Type:=msoPropertyTypeString, Value:="Third"

'original example from:
'https://msdn.microsoft.com/en-us/vba/office-shared-vba
/articles/documentproperties-add-method-office

    ActiveDocument.Save
    ActiveDocument.Close

    'set file to next in Dir
    file = Dir()

Loop

'Application.ScreenUpdating = True

MsgBox "The macro is complete."

End Sub

如您所见,我对我从 msdn 尝试的第一个示例进行了评论,并对其进行了修改。

提前感谢您的帮助,即使您可以向我指出一个资源,解释我哪里出了问题或类似的东西。

【问题讨论】:

    标签: vba loops ms-word


    【解决方案1】:

    当您执行 Save 命令时,Word 不会将 CustomDocumentProperties 的更改识别为对实际保存文档足够重要 - 除非您进行了其他更改,否则它只是决定忽略 Save。

    您可以通过告诉 Word 自上次更改文档以来尚未保存文档来强制保存:

    ActiveDocument.Saved = False
    ActiveDocument.Save
    ActiveDocument.Close
    

    【讨论】:

    • 非常感谢!我以为这会很简单。这让我发疯了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    相关资源
    最近更新 更多