如何在Excel中将CustomProperty添加到CustomDocumentProperties?

sig*_*gil 12 excel vba excel-vba

我正在尝试将DocumentProperty添加到CustomDocumentProperties集合中.代码如下:

Sub testcustdocprop()
Dim docprops As DocumentProperties
Dim docprop As DocumentProperty

Set docprops = ThisWorkbook.CustomDocumentProperties
Set docprop = docprops.Add(Name:="test", LinkToContent:=False, Value:="xyz")

End Sub
Run Code Online (Sandbox Code Playgroud)

运行这个给我以下错误:

Run-time error '5':
Invalid procedure call or argument
Run Code Online (Sandbox Code Playgroud)

我尝试将其.Add作为void函数运行,如下所示:

docprops.Add Name:="test", LinkToContent:=False, Value:="xyz"
Run Code Online (Sandbox Code Playgroud)

这给了我同样的错误.如何添加自定义文档属性?

Pet*_*ert 14

试试这个例程:

Public Sub updateCustomDocumentProperty(strPropertyName As String, _
    varValue As Variant, docType As Office.MsoDocProperties)

    On Error Resume Next
    ActiveWorkbook.CustomDocumentProperties(strPropertyName).Value = varValue
    If Err.Number > 0 Then
        ActiveWorkbook.CustomDocumentProperties.Add _
            Name:=strPropertyName, _
            LinkToContent:=False, _
            Type:=docType, _
            Value:=varValue
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

编辑:用法示例

五年后,'官方'文档仍然是一个烂摊子......我想我会添加一些用法示例:

设置自定义属性

Sub test_setProperties()
    updateCustomDocumentProperty "my_API_Token", "AbCd1234", msoPropertyTypeString
    updateCustomDocumentProperty "my_API_Token_Expiry", #1/31/2019#, msoPropertyTypeDate
End Sub
Run Code Online (Sandbox Code Playgroud)

获取自定义属性

Sub test_getProperties()
    MsgBox ActiveWorkbook.CustomDocumentProperties("my_API_Token") & vbLf _
        & ActiveWorkbook.CustomDocumentProperties("my_API_Token_Expiry")
End Sub
Run Code Online (Sandbox Code Playgroud)

列出所有自定义属性

Sub listCustomProps()
    Dim prop As DocumentProperty
    For Each prop In ActiveWorkbook.CustomDocumentProperties
        Debug.Print prop.Name & " = " & prop.Value & " (" & Choose(prop.Type, _
            "msoPropertyTypeNumber", "msoPropertyTypeBoolean", "msoPropertyTypeDate", _
            "msoPropertyTypeString", "msoPropertyTypeFloat") & ")"
    Next prop
End Sub
Run Code Online (Sandbox Code Playgroud)

删除自定义属性

Sub deleteCustomProps()
    ActiveWorkbook.CustomDocumentProperties("my_API_Token").Delete
    ActiveWorkbook.CustomDocumentProperties("my_API_Token_Expiry").Delete
End Sub
Run Code Online (Sandbox Code Playgroud)

  • @sigil不仅intellisense将其显示为可选,它[还记录为可选](http://office.microsoft.com/en-au/excel-help/HV080558571.aspx).事实证明并非如此.这有时会发生. (3认同)

San*_*arn 6

我想我应该将上述答案从 2013 年扩展到工作,而不必传入 docType 参数:

Private Function getMsoDocProperty(v As Variant) As Integer
    'VB TYPES:
        'vbEmpty                0       Empty (uninitialized)
        'vbNull                 1       Null (no valid data)
        'vbInteger              2       Integer
        'vbLong                 3       Long integer
        'vbSingle               4       Single-precision floating-point number
        'vbDouble               5       Double-precision floating-point number
        'vbCurrency             6       Currency value
        'vbDate                 7       Date value
        'vbString               8       String
        'vbObject               9       Object
        'vbError                10      Error value
        'vbBoolean              11      Boolean value
        'vbVariant              12      Variant (used only with arrays of variants)
        'vbDataObject           13      A data access object
        'vbDecimal              14      Decimal value
        'vbByte                 17      Byte value
        'vbUserDefinedType      36      Variants that contain user-defined types
        'vbArray                8192    Array

    'OFFICE.MSODOCPROPERTIES.TYPES
        'msoPropertyTypeNumber  1       Integer value.
        'msoPropertyTypeBoolean 2       Boolean value.
        'msoPropertyTypeDate    3       Date value.
        'msoPropertyTypeString  4       String value.
        'msoPropertyTypeFloat   5       Floating point value.

    Select Case VarType(v)
        Case 2, 3
            getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeNumber
        Case 11
            getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeBoolean
        Case 7
            getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeDate
        Case 8, 17
            getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeString
        Case 4 To 6, 14
            getMsoDocProperty = Office.MsoDocProperties.msoPropertyTypeFloat
        Case Else
            getMsoDocProperty = 0
    End Select
End Function

Public Sub subUpdateCustomDocumentProperty(strPropertyName As String, _
    varValue As Variant, Optional docType As Office.MsoDocProperties = 0)

    If docType = 0 Then docType = getMsoDocProperty(varValue)
    If docType = 0 Then
        MsgBox "An error occurred in ""subUpdateCustomDocumentProperty"" routine", vbCritical
        Exit Sub
    End If

    On Error Resume Next
    Wb.CustomDocumentProperties(strPropertyName).Value _
        = varValue
    If Err.Number > 0 Then
        Wb.CustomDocumentProperties.Add _
            Name:=strPropertyName, _
            LinkToContent:=False, _
            Type:=docType, _
            Value:=varValue
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)