Adding a Toolbar or Menu to an Embedded Word

B

Brian

Hi!

We have a smart-client WinForms app that uses DSOFramer to embed MS Word as
a panel of our app.

We want to create a few commands that are specific to our app that act on
the Word document. The two most logical approaches for that are:
(1) Add a menu to the menubar for all our commands
(2) Add a toolbar with buttons for each of our commands

To do that, we've tried leveraging code from:
http://msdn2.microsoft.com/en-us/library/0batekf4(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/scff9c7c(VS.80).aspx

The code we are using is appended below. It seems to work when I invoke it
programmatically. But I can't seem to make either the menu or the toolbar
appear in the embedded Word so that our users can invoke it.

Any suggestions on how to get our custom menu to appear in the menubar?
Or any suggestions on how to get our custom toolbar to appear?
Either will be greatly appreciated.


Here's some more details...

We put in a call to both of these methods right after we have loaded a
custom template.dot file. Stepping through the code, it seems to execute
without error. All instantiated objects appear to be created properly.
Their visible property is getting set to true. At the end of the add test
methods, we added a call to "Execute" and can step into the event handlers.
So, we know that the command menus are getting created because it is able to
traverse its event delegates.

However, neither the menu item nor the toolbar are visible on our
DSOFramer-hosted Word document. Is there some other trick we need to do?

Also, just to be clear, this menu or toolbar should ONLY be visible in our
embedded Word instances... we do not want it showing up in normal Word that
might be used outside of our app. (We are currently assuming that
"temporary" means that the menu items will not be persistent when MS Word
documents are opened outside our app after our app is closed.)


Here is the relevant code:

(defined at top of class.)


#region Test Fields
Office.CommandBarButton menuCommand;
string menuTag = "A unique tag";
object missing = System.Reflection.Missing.Value;
Office.CommandBar commandBar;
Office.CommandBarButton firstButton;
Office.CommandBarButton secondButton;
#endregion

(test methods..)

#region Test Menues

// If the menu already exists, remove it.
private void CheckIfMenuBarExists()
{
try
{
Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
this.myDoc.Application.CommandBars.ActiveMenuBar.FindControl(
Office.MsoControlType.msoControlPopup,
System.Type.Missing, menuTag, true, true);

if (foundMenu != null)
{
foundMenu.Delete(true);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


// Create the menu, if it does not exist.
private void AddMenuBar()
{
try
{
this.CheckIfMenuBarExists();

Office.CommandBarPopup cmdBarControl = null;
Office.CommandBar menubar = (Office.CommandBar)
this.myDoc.Application.CommandBars.ActiveMenuBar;
int controlCount = menubar.Controls.Count;
string menuCaption = "&XFooBar";

// Add the menu.
cmdBarControl =
(Office.CommandBarPopup)menubar.Controls.Add(
Office.MsoControlType.msoControlPopup, missing, missing,
controlCount, true);

if (cmdBarControl != null)
{
cmdBarControl.Caption = menuCaption;

// Add the menu command.
menuCommand =
(Office.CommandBarButton)cmdBarControl.Controls.Add(
Office.MsoControlType.msoControlButton, missing,
missing, missing, true);

menuCommand.Caption = "&XNew FooBar";
menuCommand.Tag = "NewFooBar";
menuCommand.FaceId = 65;

menuCommand.Click += new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(
menuCommand_Click);

menuCommand.Visible = true;
menuCommand.Execute(); // test it
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}


// Add text to cell A1 when the menu is clicked.
private void
menuCommand_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool
CancelDefault)
{
MessageBox.Show("Boo!");
}

#endregion


#region Test ToolBar

private void AddToolbar()
{
object missing = this.optional;

commandBar = null;

try
{
commandBar = this.myDoc.Application.CommandBars["Test"];
}
catch
{
// Toolbar named Test does not exist so we should create it.
}

if (commandBar == null)
{
// Add a commandbar named Test.
commandBar = this.myDoc.Application.CommandBars.Add("Test",
1, missing, true);
}

try
{
// Add a button to the command bar and an event handler.
firstButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, missing);

firstButton.Style = Office.MsoButtonStyle.msoButtonCaption;
firstButton.Caption = "button 1";
firstButton.Tag = "button1";
firstButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);

// Add a second button to the command bar and an event
handler.
secondButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, missing);

secondButton.Style = Office.MsoButtonStyle.msoButtonCaption;
secondButton.Caption = "button 2";
secondButton.Tag = "button2";
secondButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(ButtonClick);

commandBar.Visible = true;

secondButton.Execute(); // test

}
catch (ArgumentException e)
{
MessageBox.Show(e.Message);
}
}


// Handles the event when a button on the new toolbar is clicked.
private void ButtonClick(Office.CommandBarButton ctrl, ref bool
cancel)
{
MessageBox.Show("You clicked: " + ctrl.Caption);
}
 

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