[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Adding Toolbar or Menu to Word embedded in WinForms via DSOFramer

Brian

7/14/2007 12:16:00 AM

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/librar...(VS.80).aspx
http://msdn2.microsoft.com/en-us/librar...(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);
}


2 Answers

wawang

7/16/2007 10:22:00 AM

0

Hi Brian,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

wawang

7/17/2007 6:10:00 AM

0

Hi Brian,

The DsoFramer is provided "AS IS" with no warranty or support from
Microsoft. Please see following KB for more information:

#Visual C++ ActiveX Control for hosting Office documents in Visual Basic or
HTML
http://support.microsoft.com...
<quote>
Note This sample is provided "AS IS" with no warranty or support from
Microsoft. It is a demonstration, provided for informational purposes only,
and has not been rigorously tested with all environments and ActiveX
document servers. It is up to you to make it "production ready" if you use
it in any development solution.
</quote>



The DsoFramer has complete source code provided, you might want to check
the source code to see why newly added toolbar isn't shown.


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.