我一直得到一个空白的excel打开的错误,没有文件打开
本文关键字:excel 错误 文件 空白 一直 一个 | 更新日期: 2023-09-27 17:53:21
object oMissing = System.Reflection.Missing.Value;
MessageBox.Show("The excel file about to open");
Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
oExcel.Visible = true;
Excel.Workbooks oBooks = oExcel.Workbooks;
Excel._Workbook oBook = null;
MessageBox.Show("opening excel sheet");
oBook = oBooks.Open(fileName, oMissing, oMissing,oMissing,oMissing,oMissing,oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
MessageBox.Show("THE FILE IS OPEN");
MessageBox.Show("Macro about to be run");
RunMacro(oExcel, new Object[] { "Macro1" });
MessageBox.Show("the macro is completed");
oBook.Close(false, oMissing, oMissing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
oBook = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
oBooks = null;
oExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
oExcel = null;
这段代码打开一个空白的excel窗口,而不是正在打开的文件。宏也被定义了
为什么不直接使用https://msdn.microsoft.com/en-us/library/b3k79a5x.aspx
代替oBook = oBooks.Open(fileName, oMissing, oMissing, oMissing, ...
oBook = oBooks.Open(fileName);
另外,我要确保fileName有你要打开的工作簿的完整路径