1
votes

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.

3
I got an error when I try your code, but when I change it to the following I don't get the error and also no dialog. var excelApp = new Microsoft.Office.Interop.Excel.Application(); var x = excelApp.ActiveDialog; - Sjips
Oh sorry i'm using using Excel = Microsoft.Office.Interop.Excel - civic.sir
When you use XlFileFormat.xlExcel8 then you must save the file with an .xls extension. Crystal ball says that you use .xlsx, a very different format. - Hans Passant
No i want to save it as XLS extension... only problem is i don't even get inside the inner foreach loop. it goes right to oExcelApp.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

3 Answers

0
votes

I don't know of a way to do exactly what you describe -- to intercept a dialog box within the application.

What I think you want to do is have Excel suspend warnings in the first place. You can do this by modifying the DisplayAlerts property to false.

var excelApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
excelApp.DisplayAlerts = false;

// This should not give you any warnings
excelApp.Workbooks.Open("c:/cdh/test.xls");

And when you are done, put things back as they were:

excelApp.DisplayAlerts = true;
0
votes

hi you can create another application in which you can write logic of close alert box of Excel and execute from your main application

foreach (Process proc in Process.GetProcesses())
        {
            if (proc.MainWindowTitle.Contains("Excel"))
                ....
        }, 

so no need to user interaction for close this alert box, and you can save different alert message in database and check if this message come in alert box you can close

0
votes

Thanks all for your help. I was able to go around this and disable all alerts for Microsoft Excel Application using the REGEDIT. This was the only way to go around this situation. Just posting an answer to help others in the same situation:

REGEDIT Solution (Read near the bottom)