如何使用VBA正确设置文档属性? [英] How do you correctly set document properties using VBA?

查看:149
本文介绍了如何使用VBA正确设置文档属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Word 2010中使用VBA设置文档属性时遇到麻烦.

I'm having some trouble setting document properties using VBA in Word 2010.

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

I have a document containing several Heading 1 sections and I use a macro to extract a selected section (along with it's contents) and paste it to a new document.

这部分工作正常,但是最后我需要设置几个文档属性,但是没有设置任何属性.

This part works fine, but at the end I need to set several document properties, but none of them are being set.

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

I'm trying to set both built-in and custom properties, but for the purpose of this question I'd like to set title, subject and, category.

我已经创建了一个函数来设置所需的属性(如下所示),并且VBA不会抛出错误(即使当我删除函数中的错误处理时也是如此).

I've created a function to set the properties I desire (as below), and VBA is throwing no error (even when I remove error handling in the function).

有人知道我在做什么错吗?

Does anybody know what I am doing wrong?

这里是该功能的简要概述,但是如果您发现更容易检查该功能,则在下面显示完整功能-

Here is a brief summary of what the function should do, but the full function is below should you find it easier to check that -

  1. 检查该属性是否已存在
    • 它确实是并且是default属性
      • 设置默认属性
      • PropertyTypeUsed变量设置为default
  1. Check to see if the property already exists
    • It does and it is a default property
      • Set the default property
      • Set the PropertyTypeUsed variable to default
  • 设置自定义属性
  • PropertyTypeUsed变量设置为custom
  • Set the custom property
  • Set the PropertyTypeUsed variable to custom
  • 创建新的自定义属性
  • 设置自定义属性
  • PropertyTypeUsed变量设置为custom
  • Create a new custom property
  • Set the custom property
  • Set the PropertyTypeUsed variable to custom
  • 应该已设置default属性
    • 该属性设置成功吗?
    • 该属性设置成功吗?


    我认为是引起问题的功能

    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
    


    完整的项目代码

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


    Full project code

    The full code for this project can be found in two Paste Bins -

    • The functions
    • The form

    我不确定是否可以获取用于实际创建表单的代码(没有导出表单,但是我没有放置它的位置),但是无论如何它都很简单-

    I'm not sure if it's possible to get the code for actually creating the form (short of exporting it, but I have no where to put it), but in any case it's very simple -

    1. 表格-frmChooseDocument
    2. 标签-lblChooseDocument(您要导出哪个新入门文档?)
    3. 组合框-comChooseDocument
    4. 取消按钮-btnCancel
    5. 确定"按钮-btnOK(最初禁用)
    1. The form - frmChooseDocument
    2. The label - lblChooseDocument (Which New Starter document would you like to export?)
    3. The combobox - comChooseDocument
    4. The cancel button - btnCancel
    5. The OK button - btnOK (Initially disabled)

    实际上,我正在使用将该代码存储为新起点的主"文档的文档,其中包含有关如何使用各种应用程序的详细说明.

    In reality I'm using the document that houses this code as a 'master' document for new startes, containing detailed instructions on how to use variouse applications.

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

    The code itself looks for Heading 1 formatted text within the document and adds them to the combobox in the form, allowing the user to select a section to export. A new document is then created and saved as a PDF.

    正如评论中所建议的那样,我检查了所设置的值的类型是否与传递给函数的值的类型相匹配,并且确实如此.

    As suggested in the comments, I have checked that the type of value being set matches that of the value being passed to the function and it does.

    对于上述所有3个属性,我要传递的值和针对文档存储的属性均为string类型.

    In the case of all 3 properties described above, both the value that I am passing and the property as stored against the document are of type string.

    我添加了几行代码来输出类型和值,我将其设置为结果,并且看起来都不错,但是显然不是!

    I've added a couple of lines to output the type and value where I am setting the result and all looks well, but obviously it is not!

    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文档标题.我首先将"Saved"属性设置为false,以确保Word记录了状态更改.

    I managed to set my word document title by saving the document after changing the property. I set the "Saved" property to false first to make sure that Word registers the change in state.

    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正确设置文档属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆