Word应用与操作
触手可及的 Word 2003 使用技巧
2006-08-01 14:27:19

    向 Word 文档中添加按钮并指定运行时的单击事件 本示例演示使用 Microsoft Visual Basic for Applications (VBA) 过程,以编程方式向 Word 文档中添加控件,然后为该控件添加 Click 事件处理程序。

1.  在 Word 中新建一个空白文档。   2.  按 Alt+F11 转到 Visual Basic 编辑器。   3.  单击 Tools,然后单击 References 并选择 Microsoft Visual Basic for Applications Extensibility 引用。   4.  单击 Insert 菜单插入新模块,然后单击 Module。添加以下代码:

Sub AddButton()

    'Add a command button to a new document     Dim doc As Word.Document     Dim shp As Word.InlineShape     Set doc = Documents.Add         Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")     shp.OLEFormat.Object.Caption = "Click Here"         'Add a procedure for the click event of the inlineshape     '**Note: The click event resides in the This Document module     Dim sCode As String     sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _             "   MsgBox ""You Clicked the CommandButton""" & vbCrLf & _             "End Sub"     doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

End Sub

  5.  运行“AddButton”过程。   6.  代码运行完成后,可在新建文档中看到一个新的 CommandButton 控件。单击该命令按钮时,其 Click 事件引发,显示一条消息。  

返回页首 在打印文档前显示消息对话框 可以显示消息对话框,以在打印文档前向用户提供信息。例如,可以显示一条消息,告知用户打印文档后在何处选取此文档。为此,请创建一个名为“FilePrint”的宏,它允许您截获打印命令,添加到您自己的对话框中,然后添加可按正常模式打印文档的代码。下面是一些示例代码:

1.  在 Tools 菜单上,指向 Macro,然后单击 Visual Basic Editor。   2.  在代码窗口中键入以下过程:

Sub FilePrint()     MsgBox "I am printing " + ActiveDocument.Name     ActiveDocument.PrintOut End Sub

请注意:必须将宏命名为 FilePrint。   3.  关闭 Visual Basic 编辑器。   4.  单击 File 菜单,然后单击 Print,以打印此文档。

将显示该消息对话框。   5.  单击 OK,关闭对话框并打印文档。  

返回页首 向菜单项中添加命令 您是否曾有这样的愿望:您的用户可以像单击菜单项一样简单地运行宏?按照以下步骤就可实现您的愿望:

1.  单击 Tools,单击 Customize,单击 Toolbars 选项卡,然后选择 Shortcut menus 项。   2.  展开此工具栏上的下拉列表,并找到希望添加此命令的菜单。   3.  接着,移至 Customize 对话框的 Commands 选项卡上,并选择 Macros 类别,然后单击并拖动要移至菜单上的宏。   4.  右键单击该项,并根据需要自定义。.  

返回页首 从选定的形状返回名称和索引号 下面的过程演示了如何返回选定 Shape 对象的名称和索引号。例如,如果您需要访问代码中的特定形状对象,并需要名称或索引,那么这就非常有用。

Sub ReturnShapeData()     Dim ShapeName As String 'Name     Dim i As Integer     ShapeName = Selection.ShapeRange.Name     For i = 1 To ActiveDocument.Shapes.Count        If ActiveDocument.Shapes(i).Name = ShapeName Then           MsgBox "Shape " & Chr(34) & ShapeName & Chr(34) & " is Shape " & i        End If        Exit For     Next i End Sub

返回页首 将短日期转换为完整日期格式 以下代码用于将日期从 ##/##/#### 格式更改为完整日期格式。例如,将 10/20/2004 改为 2004 年 10 月 20 日星期三。

Sub ChangeDate()    With Selection.find       .Text = "[0-9]{2}/[0-9]{2}/[0-9]{4}"       .MatchWildcards = True       While .Execute          Selection.Text = Format(Selection.Text, "DDDD d. MMMM YYYY")          Selection.Collapse direction:=wdCollapseEnd       Wend    End With End Sub

返回页首 使用文件名填充列表框 以下过程搜索指定目录,并使用找到的文件名填充一个列表对话框。

Sub ListFilenames()    Dim strMyFile As String    Dim lngCounter As Long    Dim DirectoryListArray() As String    ReDim DirectoryListArray(1000)    strMyFile = Dir$("c:\docs\*.*")    Do While strMyFile <> ""       DirectoryListArray(lngCounter) = strMyFile       strMyFile = Dir$       lngCounter = lngCounter + 1    Loop    ReDim Preserve DirectoryListArray(lngCounter - 1)    Frm.lstNormals.List = DirectoryListArray End Sub

返回页首 确定文件是否存在 以下示例检查目录中是否存在文件。如果 Dir 函数返回一个长度为零的字符串,表示未找到该文件,并显示消息框。如果 Dir 函数找到此文件,则显示一个相应的消息框。

Sub DoesFileExist(SearchFile As String)    Dim FileInQuestion As String     FileInQuestion = Dir(SearchFile)    If FileInQuestion = "" Then       MsgBox "No such file!"    Else       MsgBox "File exists!"    End If End Sub

返回页首 创建屏蔽密码的对话框 在 Word 中,您可以使用文本框创建自定义对话框,为用户提供信息。一般情况下,当在文本框中键入文本时,该文本按您键入的方式显示。然而,您可以使用 Visual Basic Edition UserForm 的属性来创建隐藏和屏蔽文本框效果。在创建密码对话框(不希望显示在文本框中所键入的文本)时,这很有用。要对此进行测试,请遵循如下操作:

创建对话框

1.  启动 Word。   2.  按 Alt+F11 启动 Visual Basic 编辑器。   3.  在 Insert 菜单上,单击 User Form。   4.  使用“控件”工具箱将文本框和命令按钮添加到您的用户窗体中。   5.  在 Properties 页的 Object 列表中,单击 TextBox1。   6.  在 Alphabetic 选项卡上,单击 PasswordChar。   7.  键入一个星号 ( * )。   8.  在 Properties 页上的 Object 列表中,单击 UserForm1。   9.  单击用户窗体将其选定。   10.  在 Run 菜单上,单击 Run User Form。  

键入字母时,不会显示键入的字母,而显示此星号。

检索文本的代码示例

要检索写入此文本框中的文本字符串,可以使用以下示例代码:

1.  在您的用户窗体上双击 CommandButton1。   2.  键入以下代码:

Private Sub CommandButton1_Click()     MsgBox Me.TextBox1 End Sub

  3.  单击 Save 保存项目。   4.  在 Run 菜单上,单击 Run Sub/User Form。   5.  在文本框中键入一个单词,然后单击 Command 按钮。键入的文本显示在消息框中。  

返回页首 取消/重设文档保护 以下过程取消或重设文档保护:

Sub ProtectIt(ProtectType)     If ProtectType <> wdNoProtection Then         If ActiveDocument.ProtectionType = wdNoProtection Then             ActiveDocument.Protect Type:=ProtectType, NoReset:=True, Password:="My_PassWord"         End If     End If End Sub Function UnprotectIt()     wasLocked = ActiveDocument.ProtectionType     If ActiveDocument.ProtectionType <> wdNoProtection Then         ActiveDocument.Unprotect "My_Password"     End If     UnprotectIt = wasLocked End Function

要在代码段中使用它,请尝试以下操作:

' First unprotect the document. ProType = UnprotectIt() ' Here goes the code to be done in the unprotected document ' then lock it back up. ProtectIt ProType