检查是否设置了BuiltInDocumentProperty而没有错误捕获

时间:2017-01-20 14:46:09

标签: excel-vba vba excel

任务: 我的目标是检查是否已在Excel工作簿的BuiltInDocumentProperties集合中设置了值。

放大评论: 我知道某些doc属性项在Excel中从不显示值 属于ms word或ppt应用程序(例如,项目15'单词数',项25'幻灯片'......)。 另一方面,一些属性在首次使用时仅偶尔出现值:

  • 第10项:'上次打印时间'
  • 第12项:'上次保存时间'

当然可以通过错误捕获来做到这一点:

带有错误捕获的示例代码:

Sub test_showDocPropValue()
' Name of built in doc prog
  Dim propName As String
' a) Choose builtin doc prop disposing about a set value, such as 'Author', 'Category', ...
'    propName = "Category"
' b) Choose builtin doc prop of another ms application
'    propName = "Number of pages"

' c) Choose doc prop with occasionally set values
  propName = "Last print time"

' Show result
  MsgBox propName & " = " & showDocPropValue(propName), vbInformation, "BuiltInDocumentProperties"
End Sub

Function showDocPropValue(ByVal propName As String) As Variant
  Dim prop As Object
  Dim ret
' Built in Doc Props collection
  Set prop = ThisWorkbook.BuiltinDocumentProperties
' Error trapping
  On Error Resume Next
  ret = prop(propName).Value
  If Err.Number <> 0 Then
     ret = "(No value set)"
     Debug.Print Err.Number & ": " & Err.Description
  End If
' Return
  showDocPropValue = ret
End Function

我的问题: 出于主要原因,我想知道是否有一种直接的方法来获取builtinDocumentProperties值以避免错误捕获

其他提示 只是通过在CUSTOM doc props中显示没有错误捕获的方法来完成主题,您可以使用以下代码轻松检查这些项目是否存在:

Private Function bCDPExists(sCDPName As String) As Boolean
' Purp.: return True|False if custom document property name exists
' Meth.: loop thru CustomDocumentProperties and check for existing sCDPName parameter 
' Site:  <http://stackoverflow.com/questions/23917977/alternatives-to-public-variables-in-vba/23918236#23918236>
' cf:    <https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_other/using-customdocumentproperties-with-vba/91ef15eb-b089-4c9b-a8a7-1685d073fb9f>
Dim cdp As Variant      ' element of CustomDocumentProperties Collection
Dim boo As Boolean      ' boolean value showing element exists
For Each cdp In ThisWorkbook.CustomDocumentProperties
    If LCase(cdp.Name) = LCase(sCDPName) Then
       boo = True      ' heureka
       Exit For        ' exit loop
    End If
Next
bCDPExists= boo          ' return value to function
End Function

1 个答案:

答案 0 :(得分:2)

我认为没有直接的方法 - 这是一个Collection,它没有一种简单的方法来测试项目的存在(对比一个Dictionary.Exists方法,或对数组使用Match函数,等等。除了错误捕获(这似乎是非常简单的IMO)之外,你基本上要对集合的项目使用暴力迭代,检查.Name属性是否等价。

这是一种类似于CustomDocumentProperties的方法,以避免错误处理(如果需要)(尽管我没有看到任何明显错误的方法)。修改了您的showDocPropValue函数,并添加了一个额外的GetDocProp函数以便串联使用。这适用于您的测试用例:

Function showDocPropValue(ByVal propName As String) As Variant
Dim prop As Object
Dim ret
' Get the BuiltInDocumentProperty(propName) if it exists
Set prop = GetDocProp(propName)
If prop Is Nothing Then
    ret = "(No value set)"
Else
    ret = prop(propName).Value
End If
' Return
showDocPropValue = ret
End Function

Function GetDocProp(ByVal propName$)
' returns the BuiltInDocumentProperties(propName) object if exists, else Nothing
Dim p As Object
Dim prop As Object
Set prop = ThisWorkbook.BuiltinDocumentProperties
For Each p In prop
    If p.Name = propName Then
        Set GetDocProp = p
        GoTo EarlyExit
    End If
Next
Set GetDocProp = Nothing
EarlyExit:
End Function

就个人而言,我会使用此版本(GetDocProp函数中的错误处理):

Function GetDocProp(ByVal propName$)
' returns the BuiltInDocumentProperties(propName) object if exists, else Nothing
Dim ret As Object

On Error Resume Next
Set ret = ThisWorkbook.BuiltinDocumentProperties(propName)
If Err.Number <> 0 Then Set ret = Nothing 'just to be safe...

Set GetDocProp = ret

End Function