C# code to Hide toolbars in an excel document.

Hello,

I have a C# Winforms program that opens an excel document with the code below.
It works great but what I can not figure out how to do, is to turn off ALL menu's and toolbars.

The excel version I am using right now is 2003... But I will be upgrading to 2010 in the near future.
Any ideas?
//top of source...
using Excel = Microsoft.Office.Interop.Excel;

// Code inside a function...

// Get report and display it on the screen.
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();

xlWorkBook = xlApp.Workbooks.Open(strFileName, 0, true, 5, "", "", true,Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlApp.Visible = true;
xlApp.DisplayFullScreen = true;

 // Display the Document and then Sleep.
System.Threading.Thread.Sleep(timeToShowMilliseconds);

// Close the Excel report 
 xlWorkBook.Close(false, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

Open in new window

LVL 3
ScottParkerAsked:
Who is Participating?
 
TommySzalapskiConnect With a Mentor Commented:
Something like
for(int i = 0; i < Application.CommandBars.Count; ++i)
  Application.CommandBars[i].Enabled = false

Open in new window

0
 
TommySzalapskiCommented:
xlApp.Windows(1).DisplayHeadings = false
Will kill the row and column headings.
0
 
TommySzalapskiCommented:
Application.CommandBars("Worksheet Menu Bar").Enabled = False
Will kill the menu bar. Of course, setting either to true will put it back on.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
TommySzalapskiCommented:
Ach, xlApp.CommandBars of course.
0
 
TommySzalapskiCommented:
xlsApp.Windows(1).DisplayGridlines = False for the gridlines if you want them gone.

Did I get them all?
0
 
Rory ArchibaldCommented:
You could use xlApp.DisplayFullScreen = True

For 2010, manipulating commandbars won't work - you'll need to either alter the XML of the workbook to remove the ribbon, or use:

xlApp.ExecuteExcel4Macro "Show.ToolBar(""Ribbon"",False)"
0
 
ScottParkerAuthor Commented:
Sometimes I have toolbars that I dont know the name of show up..
Like there is a chart toolbar, and sometimes a refresh data toolbar, and sometimes a random toolbar that some other software has installed.

Correct me if I am wrong, but wouldn't I need to know the names of all of them if I used the
xlApp.CommandBars("Worksheet Menu Bar").Enabled = False; code?

0
 
ScottParkerAuthor Commented:
When I do use the
xlApp.CommandBars("Worksheet Menu Bar").Enabled = False;
code,
I get an error...
Non-invocable member 'Microsoft.Office.Interop.Excel._Application.CommandBars' cannot be used like a method.

and if I change it to...
xlApp.CommandBars["Worksheet Menu Bar"].Enabled = False;
Cannot apply indexing with [] to expression of type 'Microsoft.Office.Core.CommandBars'
0
 
TommySzalapskiCommented:
You could try
For Each cmdBar in xlApp.CommandBars
  cmdBar.Enabled = false
Next
0
 
ScottParkerAuthor Commented:
no. that does not work.
0
 
TommySzalapskiCommented:
Oh yeah. Sorry about the (). More people use VB here so I forgot we were in C#.
You can use the index.
Application.CommandBars[1] is the menu bar
(or perhaps it's CommandBars[0] I don't have my C# compiler to test it).
I attached a list of the numbers and names of all of them that I have. (You might have to subtract 1 since I used VBA and it's 1 indexed)

Does application.commandbars.count work? I expect it would. You could do a for loop through all 117 or so of them and just turn them all off.
0
 
TommySzalapskiCommented:
Upload failed. Here it is again.
CommandBars.xls
0
 
ScottParkerAuthor Commented:
Tommy,

I tried that and I get error message..
Cannot apply indexing with [] to an expression type 'Microsoft.Office.Core.CommandBars'
0
 
TommySzalapskiConnect With a Mentor Commented:
Hmm, now that I can test it I have no trouble at all running those lines.

Maybe you need to add a using directive. The Command Bars type is in Office.Core not Office.Interop.Excel
I used
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;

Open in new window

But Core seems to be missing from yours. Try adding it and I bet it'll work. Again, mine turns all the command bars off.
0
All Courses

From novice to tech pro — start learning today.