how to find out the host application of an embedded word document

Add-in Express™ Support Service
That's what is more important than anything else

how to find out the host application of an embedded word document
 
Wilson Wu




Posts: 14
Joined: 2007-09-11
Hi guys,

Our add-in project needs to work only if the active document is hosted inside Word application.

But a word document can be embeded inside other applications, e.g. Outlook email editor, Internet Explorer, or even any other ActiveX container applications. In these situations, how can I know that the document that my addin is processing is not embeded in another application?

Currently, I check the visibility of some Word standard tool bar in the document window activate event handler. This method solves the Outlook email editor case. But if the document is inside IE, the window activation event does not fire up.

So, the problem still comes back to how can I check whether the host application is Word or some ActiveX container? (The Application Property is always MS Word, even if it's actually embeded inside another App).

Thank you for any suggestions

Wilson Wu
Posted 18 Sep, 2009 00:48:42 Top
Ilir Berisha


Guest


Hi Wilson,

I have the same problem. Can you sent the code for the Outlook email editor.

Thank you.
Posted 18 Sep, 2009 05:18:27 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Hi Wilson,

You need to use the Container property of Word.Document. Here's an excerpt from the help refernce for the Word object model:

The Container property provides access to the specified document's container application if the document is embedded in another application as an OLE object. This property also provides a pathway into the object model of the container application if a Word document is opened as an ActiveX document ?Â?Ð?ä for example, when a Word document is opened in Microsoft Office Binder or Internet Explorer.


Does this help?


Andrei Smolin
Add-in Express Team Leader
Posted 18 Sep, 2009 06:00:03 Top
Ilir Berisha


Guest


Hi Andrei,

Can you write an example?

Best Regards
Ilir
Posted 18 Sep, 2009 08:13:35 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Hello Ilir,

Yes, I can. But the main code part in such a sample will be the following two lines of code:

        if Assigned(Doc) then
          if Assigned(Doc.Container) then


If you have any difficulties with this, please let me know and I'll create a sample project for you.


Andrei Smolin
Add-in Express Team Leader
Posted 18 Sep, 2009 08:59:17 Top
Ilir Berisha


Guest


Hi Andrei,

I tried many times, but I can't do it.

I tell you shortly what I have in my project.
- 1 TadxCommandBar with 2 Controls.

Now i have write this code to hide the commandbar, but I'm not sure, in which event i has to use this code.

Can you help me?

Code:

var
  i: integer;
  myCommandBar: CommandBar;
begin
  try
    if assigned(wordapp.ActiveDocument.Container) then
    begin
      for i:=0 to wordapp.CommandBars.Count-1 do
      begin
        if wordapp.CommandBars[i].Name='xy' then
        begin
          myCommandBar:=wordapp.CommandBars[i];
          break;
        end;
      end;
      myCommandBar.Set_Visible(False);
    end;
  except
    // do nothing
  end;
end;

Posted 18 Sep, 2009 11:04:43 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Hello Ilir,

I've tested the following code for a Word document embedded into an Excel workbook (Office 2003 SP3 + Windows XP SP3):

procedure TAddInModule.adxWordAppEvents1WindowActivate(ASender: TObject;
  const Doc: _Document; const Wn: Window);
begin
  OutputDebugString(PAnsiChar('!!! WindowActivate'));
end;

procedure TAddInModule.adxWordAppEvents1DocumentOpen(ASender: TObject;
  const Doc: _Document);
begin
  OutputDebugString(PAnsiChar('!!! DocumentOpen'));
end;

procedure TAddInModule.adxWordAppEvents1NewDocument(ASender: TObject;
  const Doc: _Document);
begin
  OutputDebugString(PAnsiChar('!!! NewDocument'));
end;

procedure TAddInModule.adxCOMAddInModuleAddInInitialize(Sender: TObject);
begin
  OutputDebugString(PAnsiChar('!!! AddInInitialize'));
  adxCommandBar1.Controls[0].AsButton.OLEUsage := msoControlOLEUsageNeither;
end;

procedure TAddInModule.adxCOMAddInModuleAddInStartupComplete(
  Sender: TObject);
begin
  OutputDebugString(PAnsiChar('!!! AddInStartupComplete ' + IntToStr(WordApp.Documents.Count)));
end;

procedure TAddInModule.adxWordAppEvents1DocumentChange(Sender: TObject);
var
  Doc : _Document;
begin
  OutputDebugString(PAnsiChar('!!! DocumentChange'));
  try
    Doc := WordApp.ActiveDocument;
  except
  end;
  if Assigned(Doc) then begin
    OutputDebugString(PAnsiChar('!!! ActiveDocument is assigned'));
    try
    if Assigned(WordApp.ActiveDocument.Container) then begin
      adxCommandBar1.Visible := false;
      OutputDebugString(PAnsiChar('!!! Container is assigned'));
    end
    except
    end;
    end
  else
    OutputDebugString(PAnsiChar('!!! No ActiveDocument'));
end;

procedure TAddInModule.adxWordAppEvents1DocumentBeforeClose(
  ASender: TObject; const Doc: _Document; var Cancel: WordBool);
begin
  OutputDebugString(PAnsiChar('!!! DocumentBeforeClose'));
end;

procedure TAddInModule.adxWordAppEvents1WindowDeactivate(ASender: TObject;
  const Doc: _Document; const Wn: Window);
begin
  OutputDebugString(PAnsiChar('!!! WindowDeactivate'));
end;

procedure TAddInModule.adxWordAppEvents1WindowSelectionChange(
  ASender: TObject; const Sel: WordSelection);
begin
  OutputDebugString(PAnsiChar('!!! WindowSelectionChange'));
end;


The output that I get in DebugView when activating the document for the first time is as follows:
00000000 3:12:31 PM [2356] !!! AddInInitialize
00000001 3:12:31 PM [2356] !!! AddInStartupComplete 0
00000002 3:12:32 PM [2356] !!! DocumentChange
00000003 3:12:32 PM [2356] !!! ActiveDocument is assigned
00000004 3:12:32 PM [2356] !!! Container is assigned


When I deactivate the document and activate it anew, no events occur. That is, it looks like there's no 100% solution for your task. In such a situation I have an only suggestion: use the Click event of the corresponding command bar control to check if the the current document is embedded into a container.


Andrei Smolin
Add-in Express Team Leader
Posted 21 Sep, 2009 07:28:25 Top
Ilir Berisha


Guest


Hi Andrei,

Thanks for the tests. I will unable the function in the onclick event of the buttons.

I have a question about the DocumentChange-Event.

Example
-Word Document is open

-Now when you open the first time a mail with embedded word, than the event DocumentChange loads, but "ActiveDocument.Container" is nil.

-Now change to the Word-Document(Alt-Tab) and then back to the Mail. Now the DocumentChange event start and "ActiveDocument.Container" is NOT nil.

Do you know, why the ActiveDocument.Container is the first time when I open a Mail = nil??

Best Regards
Ilir
Posted 22 Sep, 2009 03:28:47 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Hi Ilir,

Can you send me a screenshot of " a mail with embedded word" or describe me how to achieve this? Please see our support e-mail address in readme.txt.


Andrei Smolin
Add-in Express Team Leader
Posted 22 Sep, 2009 04:01:44 Top
Andrei Smolin


Add-in Express team


Posts: 18842
Joined: 2006-05-11
Hi Ilir,

I can't confirm this behavior in WinXP SP3 + Office 2003 SP3:

1. Starting Word
1:42:20 PM [2796] !!! AddInInitialize
1:42:20 PM [2796] !!! AddInStartupComplete 1
1:42:20 PM [2796] !!! DocumentChange
1:42:20 PM [2796] !!! ActiveDocument is assigned
2. Starting Outlook
1:42:26 PM [2796] !!! WindowDeactivate
3. Opening a new e-mail (Word is set as the default mail editor)
1:42:32 PM [2796] !!! WindowActivate
1:42:32 PM [2796] !!! DocumentChange
1:42:32 PM [2796] !!! ActiveDocument is assigned
1:42:32 PM [2796] !!! WindowActivate
4. Switching to Word
1:42:39 PM [2796] !!! WindowDeactivate
1:42:39 PM [2796] !!! WindowActivate
1:42:39 PM [2796] !!! DocumentChange
1:42:39 PM [2796] !!! ActiveDocument is assigned
5. Switching to Outlook
1:42:46 PM [2796] !!! WindowDeactivate
1:42:46 PM [2796] !!! WindowActivate
1:42:46 PM [2796] !!! DocumentChange
1:42:46 PM [2796] !!! ActiveDocument is assigned
6. Switching to Word again
1:42:49 PM [2796] !!! WindowDeactivate
1:42:49 PM [2796] !!! WindowActivate
1:42:49 PM [2796] !!! DocumentChange
1:42:49 PM [2796] !!! ActiveDocument is assigned


Andrei Smolin
Add-in Express Team Leader
Posted 22 Sep, 2009 05:53:20 Top