Code leaves item in normal.dot template

M

Michael R

I have disabled a Word built-in menu item using C# code. When the app
closes the normal.dot template has increased in size by 512 bytes. However,
the same code in VB.Net does not cause this problem. The code is below.
Any ideas as to what VB.Net hides from me that I can use in the C# code to
prevent this problem? Thanks.

VB.Net code

Private Sub HideMenu(ByVal HideMenu As Boolean)
'oWordApp is the Word.Application object
Dim oCmdBars As Office.CommandBars
Dim oPopUp As Office.CommandBarPopup

oCmdBars = oWordApp.CommandBars
oPopUp = oCmdBars("Menu Bar").Controls("F&ormat")
oPopUp.Visible = Not HideMenu
oPopUp.Enabled = Not HideMenu
End Sub


C# Code
private static void HideMenu(bool HideMenu)
{
CommandBars oCmdBars = null;
CommandBarPopup oPopUp = null;
try
{
'oWordApp is the Word.Application object
oCmdBars = oWordApp.CommandBars;
oPopUp = (CommandBarPopup)oCmdBars["Menu Bar"].Controls["F&ormat"];

//True to hide, false to show menus
oPopUp.Visible = !HideMenu;
oPopUp.Enabled = !HideMenu;
}
catch { }
finally
{
if ( oPopUp != null ) Marshal.ReleaseComObject(oPopUp);
if ( oCmdBars != null ) Marshal.ReleaseComObject(oCmdBars);
}
}

P.S. If I comment out the oPopUp.Visible = !HideMenu; and oPopUp.Enabled =
!HideMenu; in the C# code I do not have the problem!
 
C

Cindy Meister -WordMVP-

Hi Michael,

My guess is that the problem is because you've neglected to set the
CustomizationContext for the change you're making to the toolbar. In VB.NET
the change is probably not being made in Normal.dot, so the file size isn't
affected.

CustomizationContext is a property of the Word.Application object. It
determines where commandbar and keybinding changes should be saved. Possible
containers include:
- the Document object
- the NormalTemplate object
- the document's AttachedTemplate object
- any globally loaded template (member of the Templates collection)
I have disabled a Word built-in menu item using C# code. When the app
closes the normal.dot template has increased in size by 512 bytes. However,
the same code in VB.Net does not cause this problem. The code is below.
Any ideas as to what VB.Net hides from me that I can use in the C# code to
prevent this problem?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
M

Michael R

Hi Cindy,
Thanks for the response. I believe you have helped me solve the
problem.
In fact, I set the CustomizationContext to normal.dot in the C# app but do
not set it in the VB app. I thought I had read where the default
CustomizationContext was normal.dot. If this is true then it appears that
vb sets the CustomizationContext to the document??
Whatever the case, I can set it to the document and not affect normal.dot.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top