'How to skip the popup message boxes?
I am trying to open and save excel files to prevent the corrupt-load using .NET Framework (4.8). However, the file I am trying to re-save has some pictures in it and is an .xlsm file. How can I skip the popup messages? I tried to Sendkeys but it seems to not work. My code is attached below for review. Any suggestions?
using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace ReadingExcelData
{
class Program
{
static void Main(string[] args)
{
try
{
Excel.Application xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
xlApp.Visible = false;
xlApp.UserControl = false;
xlApp.AskToUpdateLinks = false;
string my_path = "link to input xlsm file";
string output_path = "link to output xlsm file";
Excel.Workbook book = xlApp.Workbooks.Open(my_path, 0, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlCorruptLoad.xlRepairFile);
if (book.HasVBProject)
{
book.SaveAs(output_path, Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
SendKeys.SendWait("{right}");
SendKeys.SendWait("{right}");
SendKeys.SendWait("{right}");
}
else
{
book.SaveAs(output_path, Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
book.Close(true, Type.Missing, Type.Missing);
xlApp.Quit();
}
catch(Exception)
{
System.Windows.MessageBox.Show("Caught error");
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


