C# 使用c获取word消息框内容#

C# 使用c获取word消息框内容#,c#,visual-studio-macros,C#,Visual Studio Macros,我有一个c#应用程序,它打开word文档,运行.bas宏,然后关闭word。所有这些都很好。宏将生成两个消息框对话框,其中包含宏的结果。我想将这些消息传递给我的c#应用程序。我该怎么做 这是我的密码: // Object for missing (or optional) arguments. object oMissing = System.Reflection.Missing.Value; // Create an instance of W

我有一个c#应用程序,它打开word文档,运行.bas宏,然后关闭word。所有这些都很好。宏将生成两个消息框对话框,其中包含宏的结果。我想将这些消息传递给我的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!";

我过去使用的解决方案不是针对心脏虚弱的人。它基本上涉及到使用老式的Windows API调用来查找消息框,然后通过其“窗口”(控件)枚举,直到找到包含所需文本的控件

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

祝你好运