【问题标题】:Getting word message box contents using c#使用c#获取word消息框内容
【发布时间】:2012-10-09 02:35:16
【问题描述】:

我有一个 c# 应用程序,它正在打开一个 word 文档、运行一个 .bas 宏并关闭 word。所有这些工作正常。宏生成 2 个带有宏结果的消息框对话框。我想将这些消息传达给我的 c# 应用程序。我该怎么做?

这是我的代码:

        // Object for missing (or optional) arguments.
        object oMissing = System.Reflection.Missing.Value;

        // Create an instance of Word, make it visible,
        // and open Doc1.doc.
        Word.Application oWord = new Word.Application();
        oWord.Visible = true;
        Word.Documents oDocs = oWord.Documents;
        object oFile = @"c:\\Macro.docm";

        // Open the file.
        Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing);

        // Run the macro.
        oWord.GetType().InvokeMember("Run",
        System.Reflection.BindingFlags.Default |
        System.Reflection.BindingFlags.InvokeMethod,
        null, oWord, new Object[] { "MyMacro" });

        // Quit Word and clean up.
        oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
        oDoc = null;
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs);
        oDocs = null;
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
        oWord = null;

        return "all done!";

【问题讨论】:

    标签: c# visual-studio-macros


    【解决方案1】:

    我过去为此使用的解决方案不适合胆小的人。它基本上涉及使用良好的老式 Windows API 调用来查找消息框,然后枚举其“窗口”(控件),直到找到带有所需文本的控件。

    如果消息框始终具有相同的标题,您应该能够使用 API 调用 FindWindowEx 来定位窗口。一旦你有了它的窗口句柄,你就可以使用 EnumChildWindows 来运行它的控件,直到你找到你想要的那个。您通常可以使用 GetWindowText 或 GetClassName 或两者的组合来限定正确的控件。一般来说,控件的文本应该可以通过 GetWindowText 获得,但我不知道 MS 用于这个特定窗口的控件是什么。

    祝你好运!

    FindWindowEx example

    EnumChildWindows example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2013-04-02
      • 2016-11-15
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多