Solved

C# code to Hide toolbars in an excel document.

Posted on 2010-11-16
14
1,963 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:ScottParker
  • 9
  • 4
14 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34152740
xlApp.Windows(1).DisplayHeadings = false
Will kill the row and column headings.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34152743
Application.CommandBars("Worksheet Menu Bar").Enabled = False
Will kill the menu bar. Of course, setting either to true will put it back on.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34152746
Ach, xlApp.CommandBars of course.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34152766
xlsApp.Windows(1).DisplayGridlines = False for the gridlines if you want them gone.

Did I get them all?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34154443
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
 
LVL 3

Author Comment

by:ScottParker
ID: 34154480
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
 
LVL 3

Author Comment

by:ScottParker
ID: 34154493
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34157212
You could try
For Each cmdBar in xlApp.CommandBars
  cmdBar.Enabled = false
Next
0
 
LVL 3

Author Comment

by:ScottParker
ID: 34157962
no. that does not work.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34158060
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34158067
Upload failed. Here it is again.
CommandBars.xls
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 34158099
Something like
for(int i = 0; i < Application.CommandBars.Count; ++i)

  Application.CommandBars[i].Enabled = false

Open in new window

0
 
LVL 3

Author Comment

by:ScottParker
ID: 34187467
Tommy,

I tried that and I get error message..
Cannot apply indexing with [] to an expression type 'Microsoft.Office.Core.CommandBars'
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 500 total points
ID: 34190123
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now