Problem to Open Word Document via COM (VBA) with unvisible window

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

Problem to Open Word Document via COM (VBA) with unvisible window
Simple Code in VBA dont work, when the Word Addin is installed (TadxWordDocumentModule) 
Ilir Berisha


Guest


Word tested: 2000, 2010
Using Add-In-Express 2008 for Microsoft Office (VCL).

Hi, some of our customers have the problem in their VBA Code, when they try to open a word document with parameter visible = False, via COM. The error appears only when the Word Addin (TadxWordDocumentModule) is installed.

Here is an example of the VBA code part:
Sub OpenWord()
Dim w
Set w = CreateObject("Word.Application")
' Open Document.. dont works, when word Addin installed and visible = false
Call w.Documents.Open(FileName:="c:\xy.doc", ReadOnly:=True, Visible:=False)
end sub

I have also tested this VBA code with a new blank Word Addin and the error appears again. Do someone has an idea/solution/workaround?
Posted 30 Dec, 2010 09:02:44 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Ilir,

Thank you for the detailed description.

I have just reproduced the issue you described above on my PC with Word 2010 32-bit. This is definitely a bug in our code. I have filed it on our bug-tracking system under #2254. We will try to fix the issue in the upcoming build of Add-in Express 2010 for Office and VCL.
Posted 03 Jan, 2011 11:07:50 Top
Ilir Berisha


Guest


Hi Dmitry,

Thx for your reply.

I had time now to take a look in the code and debug it. I have done some changes and it works now, but you will be able to say, if these changes has no risk. I checked the visibility of the Word Application, because the error seams to appear, when Word is invisible and trying to get ActiveDocument.

There are the changes:
File: adxAddin.pas
Change1 in procedure TadxAddin.WordReconnectControls
Row: 10028
...
if WordApp.Visible then
begin
if CheckDocumentProps(WordApp.ActiveDocument.CustomDocumentProperties) then
...

Change2 in proceudre TadxAddin.OnStartupComplete
Row: 10855
...
if WordApp.Visible and (WordApp.ActiveDocument <> nil) and (WordApp.ActiveWindow <> nil) then
...
Posted 19 Jan, 2011 11:57:17 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Ilir,

I would suggest another approach, because even if the WordApp.Visible condition is met, there may be no open document and consequently the attempt to access the ActiveDocument property will generate an exception. So, I would change the WordReconnectControls method like this:
 
    // ...
    if WordApp.Documents.Count = 0 then Exit;
    if (WordApp.Visible) and (Assigned(WordApp.ActiveDocument)) then begin
      if CheckDocumentProps(WordApp.ActiveDocument.CustomDocumentProperties) then begin
      // ...


What do you think?
Posted 20 Jan, 2011 11:33:47 Top
Ilir Berisha


Guest


Hi Dmitry,

Thanks for the reply.
I think, that the access of WordApp.ActiveDocument will raise an error, if no ActiveDocument is available, so the code Assigned(WordApp.Activedocument) doesn't work.

I have done a function in Unit adxAddin.pas:

function HasActiveDocument(WordApp: Word2000.TWordApplication): boolean;
begin
  result := True;
  try
    WordApp.ActiveDocument;
  except
    result := False;
  end;
end;


I've used this function before accessing WordApp.ActiveDocument in the Rows (10028, 10855) and deleted the conditional of WordApp.Visible. Thanks for the help.
Posted 26 Jan, 2011 04:04:55 Top
Dmitry Kostochko


Add-in Express team


Posts: 2875
Joined: 2004-04-05
Hi Ilir,

Thank you for sharing your solution!
Posted 26 Jan, 2011 04:19:18 Top