會員登錄 - 用戶注冊 - 網站地圖 Office中國(office-cn.net),專業(yè)Office論壇
當前位置:主頁 > 技巧 > Word技巧 > 操作 > 正文

觸手可及的 Word 2003 使用技巧

時間:2013-07-10 13:38 來源:china msbn 作者:佚名 閱讀:

    向 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 事件引發(fā),顯示一條消息。
 

返回頁首
在打印文檔前顯示消息對話框
可以顯示消息對話框,以在打印文檔前向用戶提供信息。例如,可以顯示一條消息,告知用戶打印文檔后在何處選取此文檔。為此,請創(chuàng)建一個名為“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,關閉對話框并打印文檔。
 

返回頁首
向菜單項中添加命令
您是否曾有這樣的愿望:您的用戶可以像單擊菜單項一樣簡單地運行宏?按照以下步驟就可實現(xiàn)您的愿望:

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

返回頁首
創(chuàng)建屏蔽密碼的對話框
在 Word 中,您可以使用文本框創(chuàng)建自定義對話框,為用戶提供信息。一般情況下,當在文本框中鍵入文本時,該文本按您鍵入的方式顯示。然而,您可以使用 Visual Basic Edition UserForm 的屬性來創(chuàng)建隱藏和屏蔽文本框效果。在創(chuàng)建密碼對話框(不希望顯示在文本框中所鍵入的文本)時,這很有用。要對此進行測試,請遵循如下操作:

創(chuàng)建對話框

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

 

(責任編輯:admin)

頂一下
(0)
0%
踩一下
(0)
0%
發(fā)表評論
請自覺遵守互聯(lián)網相關的政策法規(guī),嚴禁發(fā)布色情、暴力、反動的言論。
評價: