Hide Excel 2013 while programmatic change a workbook

六眼飞鱼酱① 提交于 2020-01-13 19:03:53

问题


A really good new feature of Excel 2013 is that it cannot forced to show more than one Excel workbook in one application. This seems the cause of my Problem:

If I open an Excel workbook programmatically using c# and interop Excel 2013 starts with a new application window. I can working with the workbook in code without problems but I want to hide the application. Using

   Excel.Application excelApp = new Excel.Application();
    ......
    excelApp.Workbooks.Open(...);
    excelApp.Visible = false;

hides the application window after showing it. Is there a way to stop showing the application as in Excel 2010 or earlier Version?


回答1:


In my Excel 2013, using excelApp = new Excel.Application doesn't show any window.

May it be some VBA code in opened workbook which displays window?




回答2:


So I know the question is old but I needed an answer and none of the given ones worked for me. I ended up just setting Visible to false when initializing to avoid the window flashing open before hiding.

Excel.Application excelApp = new Excel.Application() { Visible = false };



回答3:


Hide Excel application your code has launched, before opening any Workbook :

Excel.Application excel = new Excel.Application();
excel.Visible = false;
[...]
Excel.Workbook workbook;
workbook = excel.Workbooks.Open(...);



回答4:


You should always put the Visible into try/catch-block

Excel.Application xlsApp = new Excel.Application();
try
{
     // Must be surrounded by try catch to work.
     // http://naimishpandya.wordpress.com/2010/12/31/hide-power-point-application-window-in-net-office-automation/
     xlsApp.Visible = false;
     xlsApp.DisplayAlerts = false;
}
catch (Exception e)
{
     Console.WriteLine("-------Error hiding the application-------");
     Console.WriteLine("Occured error might be: " + e.StackTrace);
}
 Excel.Workbook workbook;
 workbook = xlsApp.Workbooks.Open(File, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                            Type.Missing, Type.Missing);


来源:https://stackoverflow.com/questions/17610610/hide-excel-2013-while-programmatic-change-a-workbook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!