Hi,
I'm writing a c# app that uses the web browser control to display word documents. Although i had to build the menus myself by enumerating through the Word documents CommandBarControl's it looks pretty much perfect. However there seems to be a BIG problem with the web browser control (either using the wrapper shipped with .NET, or the COM library shdocvw.dll) as the moment you open either 2 instances of the application, or run Word itself, the the toolbars, taskpanes in the original instance of my app lock up!!! i.e. they no longer react to mouse events (over, click, doubleclick etc). You can still edit the document though!?!? When i the click one of the menu items that i've built to reflect those in Word i get the exception below. When i view more than one Word document in IE, IE doesn't lock up!?!? I thought it was doing pretty exactly what i'm doing!? I've pasted the code @ the bottom of this message, so if you add the COM web browser control to your form, add a reference to Word & copy the code, you should see what i mean.
I'd REALLY appreciate any help you can give as i've tried using other controls but they don't really do the job. This one does, but it does strange things!!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x800A01A8): Exception from HRESULT: 0x800A01A8
at Microsoft.Office.Core.CommandBarControl.get_Visible()
at WordMenus.Form1.MenuItemDropDownOpening(Object sender, EventArgs e) in F:\Docs\Dev\POC's\WordMenus\WordMenus\Form1.cs:line 138
at System.Windows.Forms.ToolStripDropDownItem.OnDropDownShow(EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnDropDownShow(EventArgs e)
at System.Windows.Forms.ToolStripDropDownItem.ShowDropDownInternal()
at System.Windows.Forms.ToolStripDropDownItem.ShowDropDown(Boolean mousePush)
at System.Windows.Forms.ToolStripMenuItem.OnMouseButtonStateChange(MouseEventArgs e, Boolean isMouseDown)
at System.Windows.Forms.ToolStripMenuItem.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseDown(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseDown(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.MenuStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
WordMenus
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///F:/Docs/Dev/POC's/WordMenus/WordMenus/bin/Debug/WordMenus.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Microsoft.Office.Interop.Word
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.Office.Interop.Word/11.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Word.dll
----------------------------------------
AxInterop.SHDocVw
Assembly Version: 1.1.0.0
Win32 Version: 1.1.0.0
CodeBase: file:///F:/Docs/Dev/POC's/WordMenus/WordMenus/bin/Debug/AxInterop.SHDocVw.DLL
----------------------------------------
Interop.SHDocVw
Assembly Version: 1.1.0.0
Win32 Version: 1.1.0.0
CodeBase: file:///F:/Docs/Dev/POC's/WordMenus/WordMenus/bin/Debug/Interop.SHDocVw.DLL
----------------------------------------
Accessibility
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
office
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///C:/WINDOWS/assembly/GAC/office/11.0.0.0__71e9bce111e9429c/office.dll
----------------------------------------
CustomMarshalers
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/CustomMarshalers/2.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Program.cs:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word Document (*.doc)|*.doc";
if (ofd.ShowDialog() == DialogResult.OK) Application.Run(new Form1(ofd.FileName));
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Form1.cs:
public partial class Form1 : Form
{
private const int WORD_MENUITEM_CHECKED = 32784;
public Form1(string sFileName)
{
InitializeComponent();
ApplicationClass app = new ApplicationClass();
docAxWebBrowser.Navigate(sFileName);
DocumentClass wdDoc = (DocumentClass)docAxWebBrowser.Document;
BuildMenus(wdDoc);
}
private void BuildMenus(DocumentClass doc)
{
Microsoft.Office.Core.CommandBarControls ctrls;
ctrls = doc.CommandBars.ActiveMenuBar.Controls;
foreach (Microsoft.Office.Core.CommandBarControl ctrl in ctrls)
{
ToolStripMenuItem item = CreateMenuItem(ctrl);
if (item != null)
{
wordMenuStrip.Items.Add(item);
BuildDropDownItems(item);
}
}
}
private void BuildDropDownItems(ToolStripMenuItem item)
{
Microsoft.Office.Core.CommandBarButton button = null;
Microsoft.Office.Core.CommandBarPopup popup = item.Tag as Microsoft.Office.Core.CommandBarPopup;
// this menu item doesn't have any dropdowns...
if (popup == null) return;
if (popup.Controls.Count == 0) return;
foreach (Microsoft.Office.Core.CommandBarControl ctrl in popup.Controls)
{
ToolStripMenuItem newItem = CreateMenuItem(ctrl);
button = ctrl as Microsoft.Office.Core.CommandBarButton;
if (button != null)
{
try
{
if (button.BeginGroup)
item.DropDownItems.Add(new ToolStripSeparator());
// copy menu item's icon to clipboard...
button.CopyFace();
newItem.Image = Clipboard.GetImage();
IDataObject data = Clipboard.GetDataObject();
newItem.ImageTransparentColor = Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(223)))), ((int)(((byte)(227)))));//224,223,227
}
catch (Exception e) { }
}
else
{
BuildDropDownItems(newItem);
}
if (newItem != null) item.DropDownItems.Add(newItem);
}
}
private ToolStripMenuItem CreateMenuItem(Microsoft.Office.Core.CommandBarControl ctrl)
{
if (ctrl == null) return null;
ToolStripMenuItem item = new ToolStripMenuItem(ctrl.Caption);
item.ToolTipText = ctrl.TooltipText;
item.Visible = ctrl.Visible;
item.Enabled = ctrl.Enabled;
item.Checked = (((int)ctrl.get_accState(null)) == WORD_MENUITEM_CHECKED) ? true : false;
item.ShowShortcutKeys = true;
item.ShortcutKeyDisplayString = ctrl.get_accKeyboardShortcut(null);
// how do we set shortcut key?
//item.ShortcutKeys = Keys
item.MergeIndex = ctrl.Index;
item.Click += new EventHandler(MenuItemClick);
item.DropDownOpening += new EventHandler(MenuItemDropDownOpening);
item.Tag = (object)ctrl;
return item;
}
private void MenuItemClick(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
Microsoft.Office.Core.CommandBarControl ctrl = item.Tag as Microsoft.Office.Core.CommandBarControl;
if (ctrl.get_accDefaultAction(null) == "Execute")
{
ctrl.Execute();
// update checked state...
item.Checked = (((int)ctrl.get_accState(null)) == WORD_MENUITEM_CHECKED) ? true : false;
}
}
void MenuItemDropDownOpening(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
foreach (ToolStripItem dropdown in item.DropDownItems)
{
Microsoft.Office.Core.CommandBarControl ctrl = dropdown.Tag as Microsoft.Office.Core.CommandBarControl;
if (ctrl != null)
{
dropdown.Visible = ctrl.Visible;
dropdown.Enabled = ctrl.Enabled;
ToolStripMenuItem menudropdown = dropdown as ToolStripMenuItem;
if (menudropdown != null)
menudropdown.Checked = (((int)ctrl.get_accState(null)) == WORD_MENUITEM_CHECKED) ? true : false;
}
}
}
}
I'm writing a c# app that uses the web browser control to display word documents. Although i had to build the menus myself by enumerating through the Word documents CommandBarControl's it looks pretty much perfect. However there seems to be a BIG problem with the web browser control (either using the wrapper shipped with .NET, or the COM library shdocvw.dll) as the moment you open either 2 instances of the application, or run Word itself, the the toolbars, taskpanes in the original instance of my app lock up!!! i.e. they no longer react to mouse events (over, click, doubleclick etc). You can still edit the document though!?!? When i the click one of the menu items that i've built to reflect those in Word i get the exception below. When i view more than one Word document in IE, IE doesn't lock up!?!? I thought it was doing pretty exactly what i'm doing!? I've pasted the code @ the bottom of this message, so if you add the COM web browser control to your form, add a reference to Word & copy the code, you should see what i mean.
I'd REALLY appreciate any help you can give as i've tried using other controls but they don't really do the job. This one does, but it does strange things!!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x800A01A8): Exception from HRESULT: 0x800A01A8
at Microsoft.Office.Core.CommandBarControl.get_Visible()
at WordMenus.Form1.MenuItemDropDownOpening(Object sender, EventArgs e) in F:\Docs\Dev\POC's\WordMenus\WordMenus\Form1.cs:line 138
at System.Windows.Forms.ToolStripDropDownItem.OnDropDownShow(EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnDropDownShow(EventArgs e)
at System.Windows.Forms.ToolStripDropDownItem.ShowDropDownInternal()
at System.Windows.Forms.ToolStripDropDownItem.ShowDropDown(Boolean mousePush)
at System.Windows.Forms.ToolStripMenuItem.OnMouseButtonStateChange(MouseEventArgs e, Boolean isMouseDown)
at System.Windows.Forms.ToolStripMenuItem.OnMouseDown(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseDown(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseDown(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.MenuStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
WordMenus
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///F:/Docs/Dev/POC's/WordMenus/WordMenus/bin/Debug/WordMenus.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Microsoft.Office.Interop.Word
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///C:/WINDOWS/assembly/GAC/Microsoft.Office.Interop.Word/11.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Word.dll
----------------------------------------
AxInterop.SHDocVw
Assembly Version: 1.1.0.0
Win32 Version: 1.1.0.0
CodeBase: file:///F:/Docs/Dev/POC's/WordMenus/WordMenus/bin/Debug/AxInterop.SHDocVw.DLL
----------------------------------------
Interop.SHDocVw
Assembly Version: 1.1.0.0
Win32 Version: 1.1.0.0
CodeBase: file:///F:/Docs/Dev/POC's/WordMenus/WordMenus/bin/Debug/Interop.SHDocVw.DLL
----------------------------------------
Accessibility
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
office
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///C:/WINDOWS/assembly/GAC/office/11.0.0.0__71e9bce111e9429c/office.dll
----------------------------------------
CustomMarshalers
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.42 (RTM.050727-4200)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/CustomMarshalers/2.0.0.0__b03f5f7f11d50a3a/CustomMarshalers.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Program.cs:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Word Document (*.doc)|*.doc";
if (ofd.ShowDialog() == DialogResult.OK) Application.Run(new Form1(ofd.FileName));
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Form1.cs:
public partial class Form1 : Form
{
private const int WORD_MENUITEM_CHECKED = 32784;
public Form1(string sFileName)
{
InitializeComponent();
ApplicationClass app = new ApplicationClass();
docAxWebBrowser.Navigate(sFileName);
DocumentClass wdDoc = (DocumentClass)docAxWebBrowser.Document;
BuildMenus(wdDoc);
}
private void BuildMenus(DocumentClass doc)
{
Microsoft.Office.Core.CommandBarControls ctrls;
ctrls = doc.CommandBars.ActiveMenuBar.Controls;
foreach (Microsoft.Office.Core.CommandBarControl ctrl in ctrls)
{
ToolStripMenuItem item = CreateMenuItem(ctrl);
if (item != null)
{
wordMenuStrip.Items.Add(item);
BuildDropDownItems(item);
}
}
}
private void BuildDropDownItems(ToolStripMenuItem item)
{
Microsoft.Office.Core.CommandBarButton button = null;
Microsoft.Office.Core.CommandBarPopup popup = item.Tag as Microsoft.Office.Core.CommandBarPopup;
// this menu item doesn't have any dropdowns...
if (popup == null) return;
if (popup.Controls.Count == 0) return;
foreach (Microsoft.Office.Core.CommandBarControl ctrl in popup.Controls)
{
ToolStripMenuItem newItem = CreateMenuItem(ctrl);
button = ctrl as Microsoft.Office.Core.CommandBarButton;
if (button != null)
{
try
{
if (button.BeginGroup)
item.DropDownItems.Add(new ToolStripSeparator());
// copy menu item's icon to clipboard...
button.CopyFace();
newItem.Image = Clipboard.GetImage();
IDataObject data = Clipboard.GetDataObject();
newItem.ImageTransparentColor = Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(223)))), ((int)(((byte)(227)))));//224,223,227
}
catch (Exception e) { }
}
else
{
BuildDropDownItems(newItem);
}
if (newItem != null) item.DropDownItems.Add(newItem);
}
}
private ToolStripMenuItem CreateMenuItem(Microsoft.Office.Core.CommandBarControl ctrl)
{
if (ctrl == null) return null;
ToolStripMenuItem item = new ToolStripMenuItem(ctrl.Caption);
item.ToolTipText = ctrl.TooltipText;
item.Visible = ctrl.Visible;
item.Enabled = ctrl.Enabled;
item.Checked = (((int)ctrl.get_accState(null)) == WORD_MENUITEM_CHECKED) ? true : false;
item.ShowShortcutKeys = true;
item.ShortcutKeyDisplayString = ctrl.get_accKeyboardShortcut(null);
// how do we set shortcut key?
//item.ShortcutKeys = Keys
item.MergeIndex = ctrl.Index;
item.Click += new EventHandler(MenuItemClick);
item.DropDownOpening += new EventHandler(MenuItemDropDownOpening);
item.Tag = (object)ctrl;
return item;
}
private void MenuItemClick(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
Microsoft.Office.Core.CommandBarControl ctrl = item.Tag as Microsoft.Office.Core.CommandBarControl;
if (ctrl.get_accDefaultAction(null) == "Execute")
{
ctrl.Execute();
// update checked state...
item.Checked = (((int)ctrl.get_accState(null)) == WORD_MENUITEM_CHECKED) ? true : false;
}
}
void MenuItemDropDownOpening(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
foreach (ToolStripItem dropdown in item.DropDownItems)
{
Microsoft.Office.Core.CommandBarControl ctrl = dropdown.Tag as Microsoft.Office.Core.CommandBarControl;
if (ctrl != null)
{
dropdown.Visible = ctrl.Visible;
dropdown.Enabled = ctrl.Enabled;
ToolStripMenuItem menudropdown = dropdown as ToolStripMenuItem;
if (menudropdown != null)
menudropdown.Checked = (((int)ctrl.get_accState(null)) == WORD_MENUITEM_CHECKED) ? true : false;
}
}
}
}