在宏结束时存储值

   

宏结束时,变量中的值并未自动存储在磁盘上。如果宏需要保留一个值,则必须在宏的执行操作完成以前,将该值保存在宏的外部。本主题介绍了五个易于存储和检索宏值的位置。

文档变量

文档变量允许您将宏值作为文档或模板的一部分存储。例如,可在宏驻留的文档或模板中存储宏的值。可用 Variables 集合的 Add 方法向文档或模板添加变量。下列示例用 ThisDocument 属性将文档变量保存于运行宏的相同位置(文档或模板)。

Sub AddDocumentVariable()
    ThisDocument.Variables.Add Name:="Age", Value:=12
End Sub

下列示例用 Variable 对象的 Value 属性返回文档变量的值。

Sub UseDocumentVariable()
    Dim intAge As Integer
    intAge = ThisDocument.Variables("Age").Value
End Sub

说明

可用 DOCVARIABLE 域将文档变量插入文档。

文档属性

与文档变量相同,文档属性允许将值作为文档或模板的一部分存储。可在的“属性”对话框中查看文档属性(单击“文件”菜单上的“属性”可显示该对话框)。

Word 对象模块将文档属性分为两组:内置的和自定义的。自定义文档属性包括“属性”对话框中“自定义”选项卡上显示的属性。内置文档属性包括“属性”对话框中除“自定义”选项卡以外的所有选项卡上的属性。

若要访问内置属性,可使用 BuiltInDocumentProperties 属性返回一个包含内置文档属性的 DocumentProperties 集合。使用 CustomDocumentProperties 属性返回一个包含自定义文档属性的 DocumentProperties 集合。下列示例在运行的宏所在的相同位置(文档或模板)创建一个名为“YourName”的自定义文档属性。

Sub AddCustomDocumentProperties()
    ThisDocument.CustomDocumentProperties.Add Name:="YourName", _
        LinkToContent:=False, Value:="Joe", Type:=msoPropertyTypeString
End Sub

内置文档属性不能添至 BuiltInDocumentProperties 属性返回的 DocumentProperties 集合中。但是可检索内置文档属性的内容或更改可读/写的内置文档属性的值。

说明

可用 DOCPROPERTY 域将文档属性插入文档中。

“自动图文集”词条

“自动图文集”词条可用于在模板中存储信息。与文档变量或属性不同,“自动图文集”词条可包含除宏变量以外的其他条目,例如带格式文本或图形。可使用 AutoTextEntries 集合的 Add 方法创建新的“自动图文集”词条。下列示例创建一个包含所选内容的名为“MyText”的“自动图文集”词条。如果下列指令是模板宏的一部分,则新的“自动图文集”词条将存储在模板中;否则,“自动图文集”词条将存储在驻留指令的文档附加模板中。

Sub AddAutoTextEntry()
    ThisDocument.AttachedTemplate.AutoTextEntries.Add Name:="MyText", _
        Range:=Selection.Range
End Sub

使用 AutoTextEntry 对象的 Value 属性可检索“自动图文集”词条对象的内容。

设置文件

可使用 PrivateProfileString 属性从设置文件中检索信息并进行设置。Windows 设置文件的结构与 Windows 3.1 的 WIN.INI 文件相同。下列示例将 Macro.ini 文件中 DocTracker 节下的 DocNum 键设为 1。

Sub MacroSystemFile()
    System.PrivateProfileString( _
        FileName:="C:\My Documents\Macro.ini", _
        Section:="DocTracker", Key:="DocNum") = 1
End Sub

运行上述指令后,Macro.ini 文件包含下列文本:

[DocTracker]
DocNum=1

PrivateProfileString 属性有三个参数:FileNameSectionKeyFileName 参数用于指定设置文件的路径和文件名。Section 参数指定出现在相关的键前面的括号之间的节的名称(不包括节名的括号)。Key 参数指定其后跟随等号 (=) 的键名和设置。

使用相同的 PrivateProfileString 属性可从设置文件中检索设置。下列示例检索 Macro.ini 文件中 DocTracker 节下的 DocNum 设置。

Sub GetSystemFileInfo()
    Dim intDocNum As Integer
    intDocNum = System.PrivateProfileString( _
        FileName:="C:\My Documents\Macro.ini", _
        Section:="DocTracker", Key:="DocNum")
    MsgBox "DocNum is " & intDocNum
End Sub

Windows 注册表

可使用 PrivateProfileString 属性设置和检索 Windows 注册表中的信息。下列示例从 Windows 注册表中检索 Microsoft Word 2002 程序目录。

Sub GetRegistryInfo()
    Dim strSection As String
    Dim strPgmDir As String
    strSection = "HKEY_CURRENT_USER\Software\Microsoft" _
        & "\Office\10.0\Word\Options"
    strPgmDir = System.PrivateProfileString(FileName:="", _
        Section:=strSection, Key:="PROGRAMDIR")
    MsgBox "The directory for Word is - " & strPgmDir
End Sub

PrivateProfileString 属性有三个参数:FileNameSectionKey。若要返回或设置注册表项的值,应将 FileName 参数指定为空字符串 ("")。Section 参数应为注册表子键的完整路径。Key 参数应为 Section 指定的子键中项的名称。

也可用下列 PrivateProfileString 语法在 Windows 注册表中设置信息。

System.PrivateProfileString(FileName, Section, Key) = value

下列示例将 Windows 注册表中 Word 2002 的 Option 子键的 DOC-PATH 项设为“C:\My Documents”。

Sub SetDocumentDirectory()
    Dim strDocDirectory As String
    strDocDirectory = "HKEY_CURRENT_USER\Software\Microsoft" _
        & "\Office\10.0\Word\Options"
    System.PrivateProfileString(FileName:="", _
        Section:=strDocDirectory, Key:="DOC-PATH") = "C:\My Documents"
End Sub