从选定的形状返回名称和索引号下面的过程演示了如何返回选定 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 |