How to connect to a running copy of Word

To connect to a running instance of Word use the Delphi command GetActiveOleObject. This will return an IDispatch variable which points to the Word Application. You can then query the return object using QueryInterface to get the pointer to the _Application object. GetActiveOleObject will raise an exception if an instance of the object does not exist in the Running Object Table (ROT) so make sure to wrap the call in a try..except block.

Sample Code

uses
  Word_TLB;

procedure StartWord(var WordApp: _Application);
var
  SaveChanges: OleVariant;
begin
  try
    GetActiveOleObject('Word.Application').QueryInterface(_Application, WordApp);
  except
    WordApp := nil;
  end;

  if Unassigned(WordApp) then
  begin
    try
      WordApp := CoApplication.Create;
      WordApp.Visible := True;
    except
      if Assigned(WordApp) then
      begin
        SaveChanges := wdDoNotSaveChanges;
        WordApp.Quit(SaveChanges, EmptyParam, EmptyParam);
      end;
    end;
  end;
end;