zoukankan      html  css  js  c++  java
  • 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#程序设计有所帮助。

  • 相关阅读:
    HDU 5640 King's Cake
    HDU 5615 Jam's math problem
    HDU 5610 Baby Ming and Weight lifting
    WHU1604 Play Apple 简单博弈
    HDU 1551 Cable master 二分
    CodeForces659C Tanya and Toys map
    Codeforces 960E 树dp
    gym 101485E 二分匹配
    Codeforces 961E 树状数组,思维
    Codeforces Round #473 (Div. 2) D 数学,贪心 F 线性基,模板
  • 原文地址:https://www.cnblogs.com/Alex80/p/4494789.html
Copyright © 2011-2022 走看看