[fpc-pascal] Automation problem

Bart bartjunk64 at gmail.com
Wed Feb 4 13:46:01 CET 2009


Hi,

I'm trying to interact with MS Word.
I started with the example from the wiki, but I'm getting errors and problems.

Here's my code:

procedure TForm1.GridToWordTable;
const
  ServerName = 'Word.Application';
var
  Server: Variant;
begin
  DebugLn('GridToWordTable A');
  DebugLn('  Assigned(InitProc) = ',DbgS(Assigned(InitProc)));
  if Assigned(InitProc) then TProcedure(InitProc);
  Try
    Try
      Server := GetActiveOleObject(ServerName);
      DebugLn('  Found a running MS Word object');
    Except
      DebugLn('  Dit not find a running MS Word object, trying to
create one ...');

      Server := CreateOleObject(ServerName);
    end;
  Except
    DebugLn('  Error creating ',ServerName);
    Exit;
  end;

  Try
    DebugLn('  Found or created ',ServerName);
    Server.Visible := True;


    Server := Unassigned;
  Except
    On E: Exception do
    begin
      DebugLn('GridToWordTable raised exception:');
      DebugLn('  Class = ',E.ClassName);
      DebugLn('  Message = ',E.Message);
    end;
  end;

  DebugLn('GridToWordTable End');
end;

If I run this it always ouputs this, wether or not I have an active MS
Word running at the time.

F:\LazarusProjecten\UurCodes>uurcodes
GridToWordTable A
  Assigned(InitProc) = False
  Dit not find a running MS Word object, trying to create one ...
  Found or created Word.Application
GridToWordTable raised exception:
  Class = EOleException
  Message = Visible is geen verwijzingseigenschap.
GridToWordTable End

The end result is a non visible MS Word object:
ProcessID  Naam
FFC7052B E:\PROGRAMMA'S\OFFICE 2000\OFFICE\WINWORD.EXE

which can only be closed by killing it (in TaskManager)
(In taskmanager on XP it is listed as a process, not as an
application, not sure if this is relevant)

If I run the same code in Delphi 3 it runs just fine.
If Word is running it says it has found it, if not it creates and makes visible.

I compiled with fpc 2.2.2 / Lazarus 0.9.27 (SVN)
I have MS Word 2000 and I've tested my code on WinMe and XP, both
behave the same.

So I have two questions:
1) why does GetActiveOleObject not "find" a running MS Word instance?
2) why can't i do Server.Visible := true in fpc?

I'm really not looking forward to having to develop this (small and
simple) app with Delphi 3, because I've grown fond of the Lazarus
IDE's capabilities.

Bart



More information about the fpc-pascal mailing list