繁体   English   中英

如何使用VBA正确设置文档属性?

[英]How do you correctly set document properties using VBA?

问题

我在使用Word 2010中的VBA设置文档属性时遇到了一些问题。

我有一个包含几个Heading 1部分的文档,我使用宏来提取选定的部分(及其内容)并将其粘贴到新文档中。

这部分工作正常,但最后我需要设置几个文档属性,但没有设置它们。

我正在尝试设置内置和自定义属性,但出于这个问题的目的,我想设置标题主题类别

我已经创建了一个函数来设置我想要的属性(如下所示),并且VBA没有抛出任何错误(即使我删除了函数中的错误处理)。

有谁知道我做错了什么?


该功能应如何工作

以下是该功能应该做什么的简要总结,但如果您发现更容易检查 - 下面的完整功能 -

  1. 检查该属性是否已存在
    • 它确实是一个default属性
      • 设置默认属性
      • PropertyTypeUsed变量设置为default
    • 它确实是一个custom属性
      • 设置自定义属性
      • PropertyTypeUsed变量设置为custom
    • 它根本不存在
      • 创建一个新的自定义属性
      • 设置自定义属性
      • PropertyTypeUsed变量设置为custom
  2. 检查是否已成功设置值
    • 应该已经设置了default属性
      • 该物业成功设定?
    • 应该已经设置了custom属性
      • 该物业成功设定?
  3. 返回结果

我相信这个功能导致了这个问题

Function UpdateDocumentProperty(ByRef doc As Document, _
                                ByVal propertyName As String, _
                                ByVal propertyValue As Variant, _
                                Optional ByVal propertyType As Office.MsoDocProperties = 4)

    '** Set the result to 'False' by default '*
    Dim result As Boolean
    result = False

    '** A property to hold whether or not the property used is default or custom *'
    Dim propertyTypeUsed As String

    '** Check to see if the document property already exists *'
    If PropertyExists(doc, propertyName) Then                           ' A default property exists, so use that
        doc.BuiltInDocumentProperties(propertyName).value = propertyValue
        propertyTypeUsed = "default"
    ElseIf PropertyExists(doc, propertyName, "custom") Then             ' A custom property exists, so use that
        doc.CustomDocumentProperties(propertyName).value = propertyValue
        propertyTypeUsed = "custom"
    Else                                                                ' No property exists, so create a custom property
        doc.CustomDocumentProperties.Add _
            name:=propertyName, _
            LinkToContent:=False, _
            Type:=propertyType, _
            value:=propertyValue
        propertyTypeUsed = "custom"
    End If

    '** Check whether or not the value has actually been set *'
    On Error Resume Next
    If propertyTypeUsed = "default" Then
        result = (doc.BuiltInDocumentProperties(propertyName).value = propertyValue)
    ElseIf propertyTypeUsed = "custom" Then
        result = (doc.CustomDocumentProperties(propertyName).value = propertyValue)
    End If
    On Error GoTo 0

    UpdateDocumentProperty = result

End Function

完整的项目代码

该项目的完整代码可以在两个粘贴箱中找到 -

我不确定是否有可能获得实际创建表单的代码(缺少导出它,但我无处可去),但无论如何它都非常简单 -

  1. 表格 - frmChooseDocument
  2. 标签 - lblChooseDocument (您要导出哪个新入门文档?)
  3. 组合框 - comChooseDocument
  4. 取消按钮 - btnCancel
  5. 确定按钮 - btnOK (最初禁用)

实际上,我正在使用包含此代码的文档作为新版本的“主”文档,其中包含有关如何使用variouse应用程序的详细说明。

代码本身在文档中查找Heading 1格式的文本,并将它们添加到表单中的组合框中,允许用户选择要导出的部分。 然后创建一个新文档并将其另存为PDF。


更新

正如评论中所建议的那样,我已经检查过所设置的值的类型是否与传递给函数的值的类型相匹配。

在上述所有3个属性的情况下,我传递的值和针对文档存储的属性都是string类型。

我添加了几行来输出我设置结果的类型和值,所有看起来都很好,但显然不是!

Debug.Print "My value:        (" & TypeName(propertyValue) & ")" & propertyValue
Debug.Print "Stored property: (" & TypeName(doc.BuiltInDocumentProperties(propertyName).value) & ")" & doc.BuiltInDocumentProperties(propertyName).value

这是输出 -

My value:        (String)New Starter Guide - Novell
Stored property: (String)New Starter Guide - Novell
My value:        (String)New starter guide
Stored property: (String)New starter guide
My value:        (String)new starters, guide, help
Stored property: (String)new starters, guide, help

我设法通过在更改属性后保存文档来设置我的word文档标题。 我首先将“已保存”属性设置为false,以确保Word注册状态更改。

Function ChangeDocumentProperty(doc As Document, sProperty As String, sNewValue As String)

    Debug.Print "Initial Property, Value: " & sProperty & ", " & doc.BuiltInDocumentProperties(sProperty)

    doc.BuiltInDocumentProperties(sProperty) = sNewValue

    doc.Saved = False
    doc.Save

    ChangeDocumentProperty = (doc.Saved = True And doc.BuiltInDocumentProperties(sProperty) = sNewValue)

    Debug.Print "Final Property, Value: " & sProperty & ", " & doc.BuiltInDocumentProperties(sProperty)

End Function

即时窗口:

? ThisDocument.ChangeDocumentProperty(ThisDocument, "Title", "Report Definitions")
Initial Property, Value: Title, Report Glossary
Final Property, Value: Title, Report Definitions
True

函数不能设置永久对象属性。 换句话说,VBA不允许函数具有在函数完成运行后持续存在的副作用。

将该函数重写为Sub,它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM