我有一个 ac# 应用程序,它正在打开一个 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!";