gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:24:49 27 4
gpt4 key购买 nike

问题
我在 Word 2010 中使用 VBA 设置文档属性时遇到了一些问题。
我有一个包含多个 Heading 1 的文档部分,我使用宏提取选定的部分(及其内容)并将其粘贴到新文档中。
这部分工作正常,但最后我需要设置几个文档属性,但没有设置它们。
我正在尝试设置内置和自定义属性,但出于这个问题的目的,我想设置 标题 , 主题 和,类别 .
我创建了一个函数来设置我想要的属性(如下所示),并且 VBA 没有抛出任何错误(即使我删除了函数中的错误处理)。
有谁知道我做错了什么?

函数应该如何工作
这是该函数应该做什么的简短摘要,但如果您发现更容易检查,完整的函数如下 -

  • 检查属性是否已经存在
  • 确实如此,它是一个 default属性(property)
  • 设置默认属性
  • 设置 PropertyTypeUsed变量为 default

  • 确实如此,它是一个 custom属性(property)
  • 设置自定义属性
  • 设置 PropertyTypeUsed变量为 custom

  • 它根本不存在
  • 创建新的自定义属性
  • 设置自定义属性
  • 设置 PropertyTypeUsed变量为 custom


  • 检查是否已成功设置值
  • 一个 default属性应该已经设置
  • 属性设置成功了吗?

  • 一个 custom属性应该已经设置
  • 属性设置成功了吗?


  • 返回结果

  • 我认为导致问题的功能
    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

    完整的项目代码
    该项目的完整代码可以在两个 Paste Bins 中找到 -
  • The functions
  • The form

  • 我不确定是否有可能获得用于实际创建表单的代码(没有导出它,但我没有把它放在哪里),但无论如何它非常简单 -
  • 表格 - frmChooseDocument
  • 标签 - lblChooseDocument (您要导出哪个新的 Starter 文档?)
  • 组合框 - comChooseDocument
  • 取消按钮 - btnCancel
  • 确定按钮 - btnOK (最初禁用)

  • 实际上,我将包含此代码的文档用作新手的“主”文档,其中包含有关如何使用各种应用程序的详细说明。
    代码本身寻找 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 - 如何使用 VBA 正确设置文档属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986234/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com