C#使用Word中的内置对话框实例
本文實例講述了C#使用Word中的內置對話框的方法,分享給大家供大家參考。具體實現方法如下:
使用 Microsoft Office Word 時,有時需要顯示用戶輸入對話框。雖然可以創建自己的對話框,您也許還希望采用使用 Word 中內置對話框的方法,這些對話框在Application 對象的Dialogs 集合中公開。這使您能夠訪問 200 個以上的內置對話框,它們以枚舉的形式表示。
適用于:本文中的信息適用于 Word 2013 和 Word 2010 的文檔級項目和應用程序級項目。有關更多信息,請參見按 Office 應用程序和項目類型提供的功能:http://msdn.microsoft.com/zh-cn/library/vstudio/aa942839.aspx。
顯示對話框:
若要顯示對話框,請使用 WdWordDialog 枚舉的值之一來創建Dialog 對象,該對象表示要顯示的對話框。然后,調用Dialog 對象的Show 方法。
下面的代碼示例演示如何顯示“打開”對話框。若要使用此示例,請從項目內的ThisDocument 或 ThisAddIn 類中運行此示例。
?
復制代碼代碼如下: Word.Dialog dlg = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];dlg.Show();
?
訪問可通過后期綁定使用的對話框成員
Word 中對話框的某些屬性和方法只能通過后期綁定使用。在 Visual Basic 項目Option Strict位置打開,您必須使用反射來訪問這些成員。有關更多信息,請參見Office 解決方案中的后期綁定:http://msdn.microsoft.com/zh-cn/library/vstudio/3xxe951d.aspx。
下面的代碼示例在 Option Strict或在 Visual C# 項目面向 .NET Framework 4 或 .NET Framework 4.5的 Visual Basic 項目演示如何使用文件已打開 對話框的 Name 屬性。若要使用此示例,請從項目內的ThisDocument 或ThisAddIn 類中運行此示例。
?
復制代碼代碼如下: dynamic dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];dialog.Name = "Testing";
dialog.Show();
MessageBox.Show(dialog.Name);
?
下面的代碼示例演示如何使用反射來 文件已打開 對話框Name 屬性在 Visual Basic 中的項目的訪問 Option Strict位置打開。若要使用此示例,請從項目內的ThisDocument 或 ThisAddIn 類中運行此示例。
?
復制代碼代碼如下: Dim dlg As Word.Dialog = Application.Dialogs(Word.WdWordDialog.wdDialogFileOpen)Dim dlgType As Type = GetType(Word.Dialog)
?
' Set the Name property of the dialog box.
dlgType.InvokeMember("Name", _
??? Reflection.BindingFlags.SetProperty Or _
??????? Reflection.BindingFlags.Public Or _
??????? Reflection.BindingFlags.Instance, _
??? Nothing, dlg, New Object() {"Testing"}, _
??? System.Globalization.CultureInfo.InvariantCulture)
' Display the dialog box.
dlg.Show()
' Show the Name property.
MessageBox.Show(dlgType.InvokeMember("Name", _
??? Reflection.BindingFlags.GetProperty Or _
??????? Reflection.BindingFlags.Public Or _
??????? Reflection.BindingFlags.Instance, _
??? Nothing, dlg, Nothing, _
??? System.Globalization.CultureInfo.InvariantCulture))
?
希望本文所述對大家的C#程序設計有所幫助。
總結
以上是生活随笔為你收集整理的C#使用Word中的内置对话框实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oc runtime
- 下一篇: HDU 5226 Tom and mat