ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Formatting the excel window (https://www.excelbanter.com/excel-programming/397379-formatting-excel-window.html)

Asim

Formatting the excel window
 
Hi,

I am opening an excel file within a windows form. I want the user to
only play around with the excel area. The user should not create or open an
excel file. So I want to hide the menu bar and disable the New and Open
standard toolbar buttons. I use the following C# code to achieve my goal:




Code Snippet// xlApp is the instance of Excel.Application

xlApp.WindowState = Excel.XlWindowState.xlMaximized;

// Hide the Menu bar

int counter = xlApp.ActiveWindow.Application.CommandBars.Count;

for (int i = 1; i <= counter; i++)

{

try

{

string nm = xlApp.ActiveWindow.Application.CommandBars[i].Name;

if (nm == "Standard")

{

int count_control =
xlApp.ActiveWindow.Application.CommandBars[i].Controls.Count;

for (int j = 1; j <= 2; j++)

{

xlApp.ActiveWindow.Application.CommandBars[i].Controls[j].Enabled = false;

}

}

if (nm == "Worksheet Menu Bar")

{

xlApp.ActiveWindow.Application.CommandBars[i].Enabled = false;

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

}

// Show the necessary command bars

xlApp.CommandBars["Standard"].Visible = true;

xlApp.CommandBars["Standard"].Position = Office.MsoBarPosition.msoBarTop;

xlApp.CommandBars["Visual Basic"].Visible = true;

xlApp.CommandBars["Visual Basic"].Position = Office.MsoBarPosition.msoBarTop;

xlApp.CommandBars["Formatting"].Visible = true;

xlApp.CommandBars["Formatting"].Position = Office.MsoBarPosition.msoBarTop;






The problem now is that at times this code works fine and I see only the
necessary buttons and toolbars but at times I dont see any toolbar button.
Can somebody please explain this strange behaviour? Is there a standard code
to hide/show excel buttons/toolbars?

(I am using C# with Office 2000)



Regards,

Asim.




All times are GMT +1. The time now is 05:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com