2016-06-29 43 views
0
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窗口,而不是打開的文件。 宏也被定義。我不斷收到一個空白excel打開錯誤,沒有打開文件

+0

您可以避免所有這些oMissing '參數,如果你使用'動態'類型。添加它是爲了使互操作更容易。 –

+0

我曾嘗試刪除這些,仍然出現相同的錯誤。 –

回答

0

爲什麼不直接使用的例子在 https://msdn.microsoft.com/en-us/library/b3k79a5x.aspx

更換

oBook = oBooks.Open(fileName, oMissing, oMissing, oMissing, ... 

隨着

oBook = oBooks.Open(fileName); 

而且,我會確保該文件名具有完整路徑工作簿時」重新嘗試打開

+0

試過,不幸沒有改變。 –

+0

您是否調試過並驗證過您具有文件名的完整路徑? 如果你在該行設置了一個斷點,你應該能夠複製「fileName」的值,點擊windows + R並粘貼你的值。跑。這是否打開你想要的工作簿? – Jonny