template.saved misbehaves

A

Alex

In my C# Word add-in, I use a custom template to hold the customization context.

In certain cases, after I add/remove controls, the template is marked "dirty" and setting template.Saved = true does not clear it.

Here's the offending function:

private void CustomizeUI()
{
// Save customization context
object customizationContext = app.CustomizationContext;

try
{
// Set the template as the new customization context
app.CustomizationContext = template;
SetupCommandBars();
}

finally
{
// Mark the template as saved and restore customization context
app.CustomizationContext = customizationContext;
template.Saved = true;
}
}


If I comment out the call to SetupCommandBars(), I do not get the prompt to save the template on exit.
However, I explicitly set it's Saved status.

I also tried putting "template.Saved = true" in other places in the code with no effect.

What can be the problem?
 
A

Alex

Found a workaround.

I was deleting and re-creating a CommandBar that previously created.
Once I changed the code to remove all controls from it instead of deleting, the prompt disappeared.

I do not know why it works but it does...

Can anyone tell me what happens?


I anso encountered another problem:

In my code I call a macro to delete all of my buttons from all the menus and toolbars:

Public Sub DeleteMyButtons(name As String, tpl As Template)
Dim old As Object
Set old = CustomizationContext
CustomizationContext = tpl

Dim length As Integer
length = Len(name)

Dim bar As CommandBar
Dim ctl As CommandBarControl
For Each bar In CommandBars
For Each ctl In bar.Controls
If ctl.BuiltIn = False And Left(ctl.Tag, length) = name Then
ctl.Delete
End If
Next ctl
Next bar

CustomizationContext = old
tpl.saved = True
End Sub

As it executes, I see my toolbar lose its buttons.

However, once I try re-adding the buttons to the cached toolbar object, all the buttons that were previously deleted suddenly reappear and I get two copies of the buttons.

So, I have to delete them again:

if (myToolbar == null)
myToolbar = commandBars.Add(TOOLBAR_TAG, MsoBarPosition.msoBarTop, false, false);
else
{
// The macro deleted these controls but they reappear...
foreach (CommandBarControl ctl in myToolbar.Controls)
ctl.Delete(missing);
}

What's going on?


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi

Firstly, I think you may try to set the last paramater to true to indicate
the button is added temporary.
myToolbar = commandBars.Add(TOOLBAR_TAG, MsoBarPosition.msoBarTop,
false, false);

From the VBA Helper document,
CustomizationContext
Returns or sets a Template or Document object that represents the template
or document in which changes to menu bars, toolbars, and key bindings are
stored. Corresponds to the value of the Save in box on the Commands tab in
the Customize dialog box (Tools menu). Read/write.

But your buttons add/remove may affect the status of the Normal.dot in
addition to your template, so you may also try to set all the template's
Saved to true.

Also can you reproduce the problem, if you do all the add/remove job (your
addin does) in VBA Macro. Because it is possible that the button's COM
reference is not released timely and orderly.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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