Dav*_*ard 11 vba word-vba word-2010
我在使用Word 2010中的VBA设置文档属性时遇到了一些问题.
我有一个包含几个Heading 1部分的文档,我使用宏来提取所选部分(及其内容)并将其粘贴到新文档中.
这部分工作正常,但最后我需要设置几个文档属性,但没有设置它们.
我正在尝试设置内置和自定义属性,但出于这个问题的目的,我想设置标题,主题和类别.
我已经创建了一个函数来设置我想要的属性(如下所示),并且VBA没有抛出任何错误(即使我删除了函数中的错误处理).
有谁知道我做错了什么?
以下是该功能应该做什么的简要总结,但如果您发现更容易检查 - 下面的完整功能 -
default属性
PropertyTypeUsed变量设置为defaultcustom财产
PropertyTypeUsed变量设置为customPropertyTypeUsed变量设置为customdefault应该设置一个属性
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
Run Code Online (Sandbox Code Playgroud)
该项目的完整代码可以在两个粘贴箱中找到 -
我不确定是否有可能获得实际创建表单的代码(缺少导出它,但我无处可去),但无论如何它都非常简单 -
frmChooseDocumentlblChooseDocument(您要导出哪个新的入门文档?)comChooseDocumentbtnCancelbtnOK(最初禁用)实际上,我正在使用包含此代码的文档作为新版本的"主"文档,其中包含有关如何使用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
Run Code Online (Sandbox Code Playgroud)
这是输出 -
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13910 次 |
| 最近记录: |