3

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?

shyam
  • 9,134
  • 4
  • 29
  • 44
Harald Pitro
  • 113
  • 1
  • 7
  • Have you tried setting `Visible` to false before calling `Workbooks.Open()`? – cremor Jul 12 '13 at 08:37
  • Yes, without any effect. – Harald Pitro Jul 12 '13 at 08:38
  • I have noticed that manually opening a spreadsheet causes a blank window to appear if you programmatically open a Workbook even with `Visible`, `ScreenUpdating`, and `DisplayAlerts` all disabled, so there are two blank Excel windows open and the one you manually opened never shows you anything. – Elaskanator Nov 07 '19 at 22:10

4 Answers4

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?

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
  • Hmm, this could be possible. Because I didn't see this problem each time I open an Excel workbook (not the application!). – Harald Pitro Jul 12 '13 at 08:41
  • Sorry, there is no VBA code and worse Excel is only sometimes displayed. Any idea which could force this behaviour? – Harald Pitro Jul 30 '13 at 06:29
0

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 };
Broom
  • 596
  • 2
  • 18
0

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(...);
Gaetan LOISEL
  • 263
  • 3
  • 7
-2

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);
mike27015
  • 686
  • 1
  • 6
  • 19
  • That's not true, at least not for Word and Excel in versions 2007 and 2010. I've never got an exception from setting `Visible` to false and it always worked. Maybe it's true for Powerpoint (that's the application the blog post liked in your code comment is targeted to), but definetly not for Word and Excel. – cremor Jul 12 '13 at 09:13
  • It works ... I launch it for Word, Excel and Powerpoint. So the downvote is wrong ... Testing from 2003 2007 2010 and 2010 for an Add-In and it works perfect, because the application is being hidden – mike27015 Jul 12 '13 at 09:20
  • That has no effect. Some Excel sheets will be displayed in Excel 2013 parallel. – Harald Pitro Jul 12 '13 at 09:22
  • 1
    @mike27015 I didn't say that this doesn't work. I only said that it's not true that you always have to use a try/catch-block. – cremor Jul 12 '13 at 10:47
  • See the link in code. If you hide the application, you should add the try/catch, if I remove it from my code, I get an exception. And most of websites I visited were in try/catch block too. – mike27015 Jul 12 '13 at 11:53
  • First - I had an exception if the Excel interop and the installed Excel version doesn't match (it runs in any case). Second - I found that the hiding of the application has no effect, because Excel will be shown immediately after Open(...). A Visible=false after open shows the open and close of the Excel sheet. This is the behaviour which I want to avoide. – Harald Pitro Jul 30 '13 at 06:30