JenniferStites

My code adds a custom menu to Word documents in the WindowActivate method. My problem is that when a Word document is opened through Internet Explorer, the WindowActivate command is not called. Does anyone know what command is available for when a Word document is activated after opening it through IE I have found this documentation so far but nothing to resolve it...

----------------------------------------------------------

http://support.microsoft.com/kb/828087

Events are not raised in documents hosted in other documents or ActiveX containers

Office documents contained in other documents using OLE might not raise events when they are activated or opened. Likewise, if an Office document is hosted in some ActiveX containers, it might not raise events. The only ActiveX container that is supported for hosting documents with managed code extensions is Microsoft Internet Explorer 6.x.

If you open an Office document in Internet Explorer 6.x, the behavior for events raised in your managed code extension will be the same as those raised in a VBA project when the document is hosted in Internet Explorer. Due to the nature of ActiveX document hosting, some commands in the Excel or Word applications will be unavailable. Similarly, certain events are expected not to be raised when a document or workbook is contained in an ActiveX document host such as Internet Explorer (for example: in Word, mail merge events and Window events such as WindowActivate are not available; in Excel, you cannot cancel the close with the BeforeClose event handler).

----------------------------------------------------------


Re: Visual Studio Tools for Office WindowActivate not being called

Ji Zhou – MSFT

Hi Jennifer,

The behavior in KB article you mentioned above is a Known Issue in VSTO. We cannot work around that except we do not open the document in IE. Instead, I think we can uncheck the CheckBox Browse in same window (Start->Run->Control Panel->Folder Option->File Type, then choose DOCX and click Advanced Button).

Thanks

Ji






Re: Visual Studio Tools for Office WindowActivate not being called

JenniferStites

Ji,

I have the box you talked about unchecked already. Let me just ask you one more question.

Since that box is unchecked, when I click a link within IE to open a Word document, the Word document opens in an instance of Microsoft Word. However, this still does not call my "WindowActivate" event in my VB code. Is this normal behavior/the "known issue" still or am I doing something wrong

My code puts up a custom window during the WindowActivate event and since opening a document through IE is not calling WindowActivate, it does not put up the custom menu. If there is another event that I could use that you know of, please let me know.

Thanks for your help.

Jennifer





Re: Visual Studio Tools for Office WindowActivate not being called

Ji Zhou – MSFT

Hi Jennifer,

I test it in Word 2007 Add-in developed by Visual Studio SE, no problem exists. If I open document from Website in a new instance of Word, the WindowActive event will be raised. It seems you are working on Word 2003, right I will try to find some test machine with Office 2003 to do another test.

By the way, I think the Startup event will always be triggered. We can add our custom button both in Startup and WindowActive event. However every time we add the button, we should have a test if the button is already existed. My codes are like these:

Code Snippet

Office.CommandBar cb = null;

Office.CommandBarButton btn = null;

int btnID = -1;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

AddMenu();

this.Application.WindowActivate += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(Application_WindowActivate);

}

void Application_WindowActivate(Microsoft.Office.Interop.Word.Document Doc, Microsoft.Office.Interop.Word.Window Wn)

{

AddMenu();

}

bool AddMenu()

{

foreach (Office.CommandBar cmb in this.Application.CommandBars)

{

if (cmb.Name == "Test")

{

foreach (Office.CommandBarControl control in cmb.Controls)

{

if (control.Id == btnID)

return false;

}

}

}

cb = this.Application.CommandBars.Add("Test", Office.MsoBarPosition.msoBarTop, false, true);

cb.Visible = true;

btn = cb.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing,

missing, true) as Office.CommandBarButton;

btn.Caption = "Test";

btn.Tag = "Test";

btn.Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonCaption;

btn.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(btn_Click);

btnID = btn.Id;

return true;

}

void btn_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)

{

MessageBox.Show("Hello World");

}

Thanks

Ji






Re: Visual Studio Tools for Office WindowActivate not being called

JenniferStites

Thanks for your research Ji.

I have Office 2000 and office 2007 on my machine as well as a test machine with 2000, 2003, and 2007 on it. However, it does not seem to matter which office I am using, my menu still does not get put up nor does the windowactivate command get called. Maybe the difference is my code is in VB6 with COM addins Could that be the issue





Re: Visual Studio Tools for Office WindowActivate not being called

Ji Zhou – MSFT

Hi Jennifer,

Sorry for coming back so later, I just now installed Word 2003 on my home machine and tested it.

It proved that, if we change focus to another window first , and then change it back, the windowactive event will be raised. But the windowactive event is not triggered when the Word starts up. In fact, this is the default behavior in Word 2007 too. So as I suggested in my pre-post, I think adding the menu both in Start-up and Window-Active event handle will resolve this issue.

Thanks

Ji