Excel VBA解读(90):文档属性

Excel VBA解读(90):文档属性



本文讲解

Workbook

对象的内置文档属性和自定义文档属性。

 

BuiltinDocumentProperties

属性

返回

DocumentProperties

集合,代表指定工作簿的所有内置文档属性,只读。其语法为:

Workbook

对象

.BuiltinDocumentProperties

 

该属性返回内置文档属性的完整的集合,使用

Item

方法指定属性的名称或索引值来返回集合中单个的成员(即

DocumentProperty

对象)。

 

下面的代码列出了内置文档属性的名称:

Sub ListBuiltinDocProperties()

   Dim lngRow As Long, lngCol As Long

   Dim DocProperty As DocumentProperty

 

   lngRow = 1

   lngCol = 1

 

   For Each DocProperty In ActiveWorkbook.BuiltinDocumentProperties

        Cells(lngRow, lngCol).Value =http://www.gunmi.cn/v/DocProperty.Name

        lngRow = lngRow + 1

        If lngRow = 16 Then

            lngCol = lngCol + 1

            lngRow = 1

        End If

   Next DocProperty

 

   Cells.EntireColumn.AutoFit

End Sub

 

Excel 2007

工作簿中运行的结果如图

1

所示。

Excel VBA解读(90):文档属性

1

 

下面的语句获取当前工作簿作者名称:

ActiveWorkbook.BuiltinDocumentProperties("Author").Value

 

CustomDocumentProperties

属性

返回或者设置

DocumentProperties

集合,代表指定工作簿的所有自定义文档属性。其语法为:

Workbook

对象

.CustomDocumentProperties

 

自定义属性对应的对话框如图

2

所示。

Excel VBA解读(90):文档属性

2

 

该属性返回自定义文档属性的完整的集合,使用

Item

方法指定属性的名称或索引值来返回集合中单个的成员(即

DocumentProperty

对象)。

 

下面的代码列出了自定义文档属性的名称和值:

Sub ListCustomDocumentProperty()

   Dim lngRow As Long

   Dim DocProperty

 

   lngRow = 1

 

   For Each DocProperty In ActiveWorkbook.CustomDocumentProperties

        Cells(lngRow, 1).Value =http://www.gunmi.cn/v/DocProperty.Name

        Cells(lngRow, 2).Value =http://www.gunmi.cn/v/DocProperty.Value

        lngRow = lngRow + 1

   Next DocProperty

End Sub

 

下面的代码,给当前工作簿添加自定义属性:

Sub AddCustomProperty()

   With ActiveWorkbook.CustomDocumentProperties

        .Add Name:="LastModifiedBy",_

            LinkToContent:=True, _

            Type:=msoPropertyTypeString, _

            LinkSource:="Last author"

        .Add Name:="CustomNumber", _

            LinkToContent:=False, _

            Type:=msoPropertyTypeNumber, _

            Value:=1000

        .Add Name:="CustomString", _

            LinkToContent:=False, _

            Type:=msoPropertyTypeString, _

            Value:="This is a customproperty."

        .Add Name:="CustomDate", _

            LinkToContent:=False, _

            Type:=msoPropertyTypeDate, _

            Value:=Date

   End With

End Sub

 

自定义文档属性是存储要在代码中使用的相关工作簿信息的好地方。例如,如图

2

所示,在工作簿中创建了自定义属性“

fanjy

”,另外有一个用于创建自定义工具栏的加载项。如果激活含有自定义属性“

fanjy

”的工作簿,那么应用自定义工具栏。

 

在标准模块中,设置常量来识别工具栏名称和属性名称:

Public Const gstrISFanjy As String= “fanjy”

public Const gstrGTool As String =“MyTool”

 

在类模块中,创建应用级事件基于自定义属性来显示和隐藏工具栏:

Private SubxlApp_WindowActivate(ByVal wb As Workbook, _

                                    ByVal wn As Window)

    DimblnFanjy As Boolean

    OnError Resume Next

    blnFanjy= wb.CustomDocumentProperties(gstrISFanjy)

    OnError GoTo 0

    IfblnFanjy Then

       Application.CommandBars(gstrGTool).Visible= True

    EndIf

End Sub

 

Private SubxlApp_WindowDeactivate(ByVal wb As Workbook, _

                                     ByVal wn As Window)

    OnError Resume Next

    Application.CommandBars(gstrGTool).Visible= False

End Sub

 

示例:在自定义文档属性中存储值

有时,可以在自定义文档属性中存储值为以后使用。本例中,每次调用代码时,存储在自定义属性中的值都会增加

1

Sub SaveValueInCustomDocProperty()

   Const strVer As String = "MyWbVer"

   

    "

如果名字不存在

,

那么创建并设置初始值为

1

   On Error Resume Next

   Dim DocProperty As DocumentProperty

   Set DocProperty = ThisWorkbook.CustomDocumentProperties(strVer)

   If Err.Number > 0 Then

        ThisWorkbook.CustomDocumentProperties.Add_

            Name:=strVer, _

            LinkToContent:=False, _

            Type:=msoPropertyTypeNumber, _

            Value:=1

   Else

    "

如果名字存在

,

需要将其值加

1

        Dim strDocVal As String

        strDocVal =ThisWorkbook.CustomDocumentProperties(strVer).Value

        "

重设名称为新值

       ThisWorkbook.CustomDocumentProperties(strVer).Value = http://www.gunmi.cn/v/CLng(strDocVal) +1

   End If

   Set DocProperty = Nothing

End Sub

Excel VBA解读(90):文档属性