共用方式為


顯示內建的 Word 對話框

本主題包含以下章節的資訊與範例。

顯示內建的對話方塊

你可以透過 Visual Basic for Applications (VBA) ,顯示內建對話框來取得使用者輸入或控制Word。 Dialog 物件的 Show 方法會顯示並執行在內建 Word 對話方塊中採取的任何動作。 若要存取特定的內建 Word 對話方塊,請使用 Dialogs 屬性來指定 WdWordDialog 常數。 例如,下列巨集指令會顯示 [開啟舊檔] 對話方塊 (wdDialogFileOpen)。

Sub ShowOpenDialog() 
 Dialogs(wdDialogFileOpen).Show 
End Sub

If a file is selected and OK is clicked, the file is opened (the action is executed). 以下範例顯示 wdDialogFilePrint) (列印對話框

Sub ShowPrintDialog() 
 Dialogs(wdDialogFilePrint).Show 
End Sub

您可以設定 DefaultTab 屬性,以便存取 Word 對話方塊中的特定索引標籤。 以下範例顯示「邊界與陰影」對話框中的頁面邊框標籤。

Sub ShowBorderDialog() 
 With Dialogs(wdDialogFormatBordersAndShading) 
 .DefaultTab = wdDialogFormatBordersAndShadingTabPageBorder 
 .Show 
 End With 
End Sub

注意事項

[!注意事項] 此外,您也可以在 Word 中使用 VBA 屬性,以便顯示使用者資訊而不顯示對話方塊。 下列範例會使用 Application 物件的 UserName 屬性來顯示應用程式的使用者名稱,但不顯示 [使用者資訊] 對話方塊。

Sub DisplayUserInfo() 
 MsgBox Application.UserName 
End Sub

如果上一則範例中的使用者名稱已變更,這項變更將不會在對話方塊中設定。 您可以使用 Execute 方法來執行對話方塊中的設定而不顯示對話方塊。 下列範例會顯示 [使用者資訊] 對話方塊,而且如果名稱不是空字串時,就會使用 Execute 方法在對話方塊中設定這些設定。

Sub ShowAndSetUserInfoDialogBox() 
 With Dialogs(wdDialogToolsOptionsUserInfo) 
 .Display 
 If .Name <> "" Then .Execute 
 End With 
End Sub

注意事項

Use the VBA properties and methods in Word to set the user information without displaying the dialog box. The following code example changes the user name through the UserName property of the Application object, and then it displays the User Information dialog box to show that the change has been made. Note that displaying the dialog box is not necessary to change the value of a dialog box.

Sub SetUserName() 
 Application.UserName = "Jeff Smith" 
 Dialogs(wdDialogToolsOptionsUserInfo).Display 
End Sub

傳回及變更對話方塊設定

Dialog 物件回傳或更改對話框的值效率不高,因為你可以用屬性或方法回傳或更改。 此外,在大部分情況下,當您使用 VBA 程式碼來取代存取 Dialog 物件時,程式碼會更簡單且更簡短。 因此,下列範例也包含使用對應 VBA 屬性來執行相同工作的範例。

使用 Dialog 物件來傳回或變更對話方塊設定之前,您必須先識別個別的對話方塊。 您可以使用 Dialogs 屬性搭配 WdWordDialog 常數來完成此工作。 在產生 Dialog 物件的例項之後,您就可以傳回或設定對話方塊中的選項。 下列範例會顯示 [段落] 對話方塊中的右邊縮排。

Sub ShowRightIndent() 
 Dim dlgParagraph As Dialog 
 Set dlgParagraph = Dialogs(wdDialogFormatParagraph) 
 MsgBox "Right indent = " & dlgParagraph.RightIndent 
End Sub

注意事項

使用 Word 的 VBA 屬性和方法來顯示段落正確的縮排設定。 下列範例會使用 ParagraphFormat 物件的 RightIndent 屬性來顯示位於插入點位置之段落的右邊縮排。

Sub ShowRightIndexForSelectedParagraph() 
 MsgBox Selection.ParagraphFormat.RightIndent 
End Sub

Just as you can return dialog box settings, you can also set dialog box settings. The following example marks the Keep with next check box in the Paragraph dialog box.

Sub SetKeepWithNext() 
 With Dialogs(wdDialogFormatParagraph) 
 .KeepWithNext = 1 
 .Execute 
 End With 
End Sub

注意事項

您還可以使用 VBA 屬性和方法來變更段落的右邊縮排。 以下範例使用 ParagraphFormat 物件的 KeepWithNext 屬性,將所選段落保持為下一段。

Sub SetKeepWithNextForSelectedParagraph() 
 Selection.ParagraphFormat.KeepWithNext = True 
End Sub

注意事項

[!注意事項] 您可以使用 Update 方法來確保對話方塊的值會反映目前的值。 如果您先前在巨集中定義一個對話方塊變數,然後想要傳回或變更目前的設定,可能就必須使用 Update 方法。

檢查對話方塊的關閉方式

The value returned by the Show and Display methods indicates which button was clicked to close the dialog box. The following example displays the Break dialog box, and if OK is clicked, a message is displayed on the status bar.

Sub DialogBoxButtons() 
 If Dialogs(wdDialogInsertBreak).Show = -1 Then 
 StatusBar = "Break inserted" 
 End If 
End Sub

下表將說明與對話方塊中按鈕關聯的傳回值。

傳回值 描述
-2 The Close button.
-1 The OK button.
0 (零) The Cancel button.
> 0 (zero) 指令按鈕:1 是指第一個按鈕,2 是指第二個按鈕,依此類推。

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應