I have a small issues which i was not able to find any solution on StackOverFlow. The Situation: I have an application that automatically opens up EXCEL application. When it opens it right away gives a dialog box that says "The file format and extension of xxx don't match. The file could be corrupted or unsafe..." I am trying to create an application that focus on this running EXCEL application and hit the "YES" and resave the excel with VERSION 2007 so this error message will not come up again.
Here is what i have so far:
var excelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
var c = excelApp.ActiveDialog;
Not sure how to do a click event on the YES using the excelApp variable. I also tried through
foreach (Process proc in Process.GetProcesses())
{
if (proc.MainWindowTitle.Contains("Excel"))
....
}
Nov. 06 2017: This is what i have so far now:
var oExcelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
Process[] processlist = Process.GetProcessesByName("Excel"); //Shows number of running Excel apps
foreach (Process theprocess in processlist) //foreach Excel app running
{
if (oExcelApp.Workbooks.Count >= 0) //for worbooks in each Excel app
{
foreach (Excel.Workbook wkb in oExcelApp.Application.Workbooks)
{
wkb.SaveAs(filePath, Excel.XlFileFormat.xlExcel8);
wkb.Close(true, null, null);
Marshal.FinalReleaseComObject(wkb);
}
oExcelApp.Workbooks.Close();
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
oExcelApp.Quit();
Marshal.ReleaseComObject(oExcelApp);
}
This works ONLY If there was no error message at the beginning. If the process automatically opens excel and has error message showing this will throw an exception because the application is busy on the error message.
var excelApp = new Microsoft.Office.Interop.Excel.Application(); var x = excelApp.ActiveDialog;- Sjipsusing Excel = Microsoft.Office.Interop.Excel- civic.siroExcelApp.Workbooks.Close();and throws an exception -- the message is "The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER))" because its stuck on the alert message "The file format and extension of ..." - civic.sir