Board index » delphi » D5 COM object calling Word hangs

D5 COM object calling Word hangs

I have a COM object which instantiates MSWord:

var
  MSWord:TWordApplication;
  TempDocument:_Document;
  WorkingDocument:TWordDocument;
begin
  MSWord := TWordApplication.Create(nil);
  with MSWord do
  begin
    Connect;        //uses the default ckRunningOrNew
    Visible := True;
    TempDocument := Documents.Open(OpenFileName, etc...)     //I've
previously checked that OpenFileName exists

    WorkingDocument := TWordDocument.Create(nil);
    WorkingDocument.ConnectTo(TempDocument);            //<--this is where
it hangs
...
  end;
end;

It is written in D5, using Word97.pas.  I have this COM object running in
debug, under DLLHOST.

When I execute the "WorkingDocument.ConnectTo" statement the application
goes into limbo.  I've tried wrapping each statement in try..excepts - no
exceptions are raised.  The client and MSWord show the busy cursor, waiting
for the COM to finish I guess.  The task manager does not show excessive CPU
usage as it would if the system were looping.

If MSWord is already running when I execute the COM object it works.  If I
change the MSWord.ConnectKind to ckNewInstance then it hangs, even if Word
is already running.

Also in the Evaluate/Modify window of in the Watch List window I can get an
[Evaluating...] message and, again, it hangs.

I've looked in various news groups and compared my code against downloaded
samples and I can't see where I'm going wrong.  Any advice would be
appreciated,

Michael Green
michael.gr...@engn.com
+44 (0)28 9448 0000

 

Re:D5 COM object calling Word hangs


you don't have to create word docs, so your func should look like this

 var
   MSWord:TWordApplication;
//   TempDocument : WordDocument; don't need it really in this func
 begin
   MSWord := TWordApplication.Create(nil);
   with MSWord do
   begin
     Connect;        //uses the default ckRunningOrNew
     Visible := True;
     Documents.Open(opendialog.filename, EmptyPARAm, etc);
   end;
 end;

then yuo could use msword.activedocument to do all your stuff with the
active doc or if you would like you could also:

index := 1;
tempdocument = msword.documents.item(index)

and work with tempdocument again

Quote
"Michael Green" <michael.gr...@engn.com> wrote in message

news:3bb31661_2@dnews...
Quote
> I have a COM object which instantiates MSWord:

> var
>   MSWord:TWordApplication;
>   TempDocument:_Document;
>   WorkingDocument:TWordDocument;
> begin
>   MSWord := TWordApplication.Create(nil);
>   with MSWord do
>   begin
>     Connect;        //uses the default ckRunningOrNew
>     Visible := True;
>     TempDocument := Documents.Open(OpenFileName, etc...)     //I've
> previously checked that OpenFileName exists

>     WorkingDocument := TWordDocument.Create(nil);
>     WorkingDocument.ConnectTo(TempDocument);            //<--this is where
> it hangs
> ...
>   end;
> end;

> It is written in D5, using Word97.pas.  I have this COM object running in
> debug, under DLLHOST.

> When I execute the "WorkingDocument.ConnectTo" statement the application
> goes into limbo.  I've tried wrapping each statement in try..excepts - no
> exceptions are raised.  The client and MSWord show the busy cursor,
waiting
> for the COM to finish I guess.  The task manager does not show excessive
CPU
> usage as it would if the system were looping.

> If MSWord is already running when I execute the COM object it works.  If I
> change the MSWord.ConnectKind to ckNewInstance then it hangs, even if Word
> is already running.

> Also in the Evaluate/Modify window of in the Watch List window I can get
an
> [Evaluating...] message and, again, it hangs.

> I've looked in various news groups and compared my code against downloaded
> samples and I can't see where I'm going wrong.  Any advice would be
> appreciated,

> Michael Green
> michael.gr...@engn.com
> +44 (0)28 9448 0000

Re:D5 COM object calling Word hangs


Tim,
Thanks for your help.

I added in the references to TWordDocument after looking at a tutorial
download. Unfortunately it had the components on a regular Delphi window. Of
course, it works perfectly.

Not using TWordDocument only delays the problem until I make the first
reference of a document a little later:

Procedure ObjectType.XYZ(const MSWord:TWordApplication);
var
 BookMarkNameWide:WideString;
 TrueFalse:WordBool;
begin
 with MSWord do
 begin
  ...
  BookMarkNameWide := 'SomeBookmark';
  TrueFalse := ActiveDocument.Bookmarks.Exists(BookmarkNameWide);   //<--
hangs here
  ...
 end;
end;

Any ideas, anyone?

Michael Green
michael.gr...@engn.com

Quote
"Tim Verhoeven" <t...@basix.co.za> wrote in message news:3bb317cf_2@dnews...
> you don't have to create word docs, so your func should look like this

>  var
>    MSWord:TWordApplication;
> //   TempDocument : WordDocument; don't need it really in this func
>  begin
>    MSWord := TWordApplication.Create(nil);
>    with MSWord do
>    begin
>      Connect;        //uses the default ckRunningOrNew
>      Visible := True;
>      Documents.Open(opendialog.filename, EmptyPARAm, etc);
>    end;
>  end;

> then yuo could use msword.activedocument to do all your stuff with the
> active doc or if you would like you could also:

> index := 1;
> tempdocument = msword.documents.item(index)

> and work with tempdocument again

> "Michael Green" <michael.gr...@engn.com> wrote in message
> news:3bb31661_2@dnews...
> > I have a COM object which instantiates MSWord:

> > var
> >   MSWord:TWordApplication;
> >   TempDocument:_Document;
> >   WorkingDocument:TWordDocument;
> > begin
> >   MSWord := TWordApplication.Create(nil);
> >   with MSWord do
> >   begin
> >     Connect;        //uses the default ckRunningOrNew
> >     Visible := True;
> >     TempDocument := Documents.Open(OpenFileName, etc...)     //I've
> > previously checked that OpenFileName exists

> >     WorkingDocument := TWordDocument.Create(nil);
> >     WorkingDocument.ConnectTo(TempDocument);            //<--this is
where
> > it hangs
> > ...
> >   end;
> > end;

> > It is written in D5, using Word97.pas.  I have this COM object running
in
> > debug, under DLLHOST.

> > When I execute the "WorkingDocument.ConnectTo" statement the application
> > goes into limbo.  I've tried wrapping each statement in try..excepts -
no
> > exceptions are raised.  The client and MSWord show the busy cursor,
> waiting
> > for the COM to finish I guess.  The task manager does not show excessive
> CPU
> > usage as it would if the system were looping.

> > If MSWord is already running when I execute the COM object it works.  If
I
> > change the MSWord.ConnectKind to ckNewInstance then it hangs, even if
Word
> > is already running.

> > Also in the Evaluate/Modify window of in the Watch List window I can get
> an
> > [Evaluating...] message and, again, it hangs.

> > I've looked in various news groups and compared my code against
downloaded
> > samples and I can't see where I'm going wrong.  Any advice would be
> > appreciated,

> > Michael Green
> > michael.gr...@engn.com
> > +44 (0)28 9448 0000

Re:D5 COM object calling Word hangs


the only thing i can think of is that you did not create msword before
calling the method

here's my method which works a 100%

var WordApp : WordApplication;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WordApp := CreateComObject(CLASS_WordApplication) as WordApplication;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Filename : OleVariant;
    TrueFalse : WordBool;
begin
  if ( OpenDialog1.Execute ) then
  begin
    Filename := OpenDialog1.Filename;

    WordApp.Documents.Open(Filename, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam);

    TrueFalse :=
WordApp.ActiveDocument.Bookmarks.Exists('BK_AA_CompanyName'); //just testing
your scenario

    if ( WordApp.ActiveDocument.Bookmarks.Exists('BK_AA_CompanyName') ) then
      ShowMessage('It Exists');

    WordApp.Visible := TRUE;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var SaveChanges : OleVariant;
begin
  SaveChanges := wdDoNotSaveChanges;
  WordApp.Quit(SaveChanges, EmptyParam, EmptyParam);
end;

Quote
"Michael Green" <michael.gr...@engn.com> wrote in message

news:3bb362e8$1_1@dnews...
Quote
> Tim,
> Thanks for your help.

> I added in the references to TWordDocument after looking at a tutorial
> download. Unfortunately it had the components on a regular Delphi window.
Of
> course, it works perfectly.

> Not using TWordDocument only delays the problem until I make the first
> reference of a document a little later:

> Procedure ObjectType.XYZ(const MSWord:TWordApplication);
> var
>  BookMarkNameWide:WideString;
>  TrueFalse:WordBool;
> begin
>  with MSWord do
>  begin
>   ...
>   BookMarkNameWide := 'SomeBookmark';
>   TrueFalse := ActiveDocument.Bookmarks.Exists(BookmarkNameWide);   //<--
> hangs here
>   ...
>  end;
> end;

> Any ideas, anyone?

> Michael Green
> michael.gr...@engn.com

> "Tim Verhoeven" <t...@basix.co.za> wrote in message

news:3bb317cf_2@dnews...

- Show quoted text -

Quote
> > you don't have to create word docs, so your func should look like this

> >  var
> >    MSWord:TWordApplication;
> > //   TempDocument : WordDocument; don't need it really in this func
> >  begin
> >    MSWord := TWordApplication.Create(nil);
> >    with MSWord do
> >    begin
> >      Connect;        //uses the default ckRunningOrNew
> >      Visible := True;
> >      Documents.Open(opendialog.filename, EmptyPARAm, etc);
> >    end;
> >  end;

> > then yuo could use msword.activedocument to do all your stuff with the
> > active doc or if you would like you could also:

> > index := 1;
> > tempdocument = msword.documents.item(index)

> > and work with tempdocument again

> > "Michael Green" <michael.gr...@engn.com> wrote in message
> > news:3bb31661_2@dnews...
> > > I have a COM object which instantiates MSWord:

> > > var
> > >   MSWord:TWordApplication;
> > >   TempDocument:_Document;
> > >   WorkingDocument:TWordDocument;
> > > begin
> > >   MSWord := TWordApplication.Create(nil);
> > >   with MSWord do
> > >   begin
> > >     Connect;        //uses the default ckRunningOrNew
> > >     Visible := True;
> > >     TempDocument := Documents.Open(OpenFileName, etc...)     //I've
> > > previously checked that OpenFileName exists

> > >     WorkingDocument := TWordDocument.Create(nil);
> > >     WorkingDocument.ConnectTo(TempDocument);            //<--this is
> where
> > > it hangs
> > > ...
> > >   end;
> > > end;

> > > It is written in D5, using Word97.pas.  I have this COM object running
> in
> > > debug, under DLLHOST.

> > > When I execute the "WorkingDocument.ConnectTo" statement the
application
> > > goes into limbo.  I've tried wrapping each statement in try..excepts -
> no
> > > exceptions are raised.  The client and MSWord show the busy cursor,
> > waiting
> > > for the COM to finish I guess.  The task manager does not show
excessive
> > CPU
> > > usage as it would if the system were looping.

> > > If MSWord is already running when I execute the COM object it works.
If
> I
> > > change the MSWord.ConnectKind to ckNewInstance then it hangs, even if
> Word
> > > is already running.

> > > Also in the Evaluate/Modify window of in the Watch List window I can
get
> > an
> > > [Evaluating...] message and, again, it hangs.

> > > I've looked in various news groups and compared my code against
> downloaded
> > > samples and I can't see where I'm going wrong.  Any advice would be
> > > appreciated,

> > > Michael Green
> > > michael.gr...@engn.com
> > > +44 (0)28 9448 0000

Re:D5 COM object calling Word hangs


Michael have you tried reversing the polarity on your main plasma conduits,
I'm sure that if you tried increasing the density of your tacion emmissions
those nano-word-docs might just achive escape velocity and sub-space
equilibrium could be restored.  This is a volunteers only mission,
remember - come home safe.

Good Luck Soldier.
Space Command.

Quote
"Michael Green" <michael.gr...@engn.com> wrote in message

news:3bb362e8$1_1@dnews...
Quote
> Tim,
> Thanks for your help.

> I added in the references to TWordDocument after looking at a tutorial
> download. Unfortunately it had the components on a regular Delphi window.
Of
> course, it works perfectly.

> Not using TWordDocument only delays the problem until I make the first
> reference of a document a little later:

> Procedure ObjectType.XYZ(const MSWord:TWordApplication);
> var
>  BookMarkNameWide:WideString;
>  TrueFalse:WordBool;
> begin
>  with MSWord do
>  begin
>   ...
>   BookMarkNameWide := 'SomeBookmark';
>   TrueFalse := ActiveDocument.Bookmarks.Exists(BookmarkNameWide);   //<--
> hangs here
>   ...
>  end;
> end;

> Any ideas, anyone?

> Michael Green
> michael.gr...@engn.com

> "Tim Verhoeven" <t...@basix.co.za> wrote in message

news:3bb317cf_2@dnews...

- Show quoted text -

Quote
> > you don't have to create word docs, so your func should look like this

> >  var
> >    MSWord:TWordApplication;
> > //   TempDocument : WordDocument; don't need it really in this func
> >  begin
> >    MSWord := TWordApplication.Create(nil);
> >    with MSWord do
> >    begin
> >      Connect;        //uses the default ckRunningOrNew
> >      Visible := True;
> >      Documents.Open(opendialog.filename, EmptyPARAm, etc);
> >    end;
> >  end;

> > then yuo could use msword.activedocument to do all your stuff with the
> > active doc or if you would like you could also:

> > index := 1;
> > tempdocument = msword.documents.item(index)

> > and work with tempdocument again

> > "Michael Green" <michael.gr...@engn.com> wrote in message
> > news:3bb31661_2@dnews...
> > > I have a COM object which instantiates MSWord:

> > > var
> > >   MSWord:TWordApplication;
> > >   TempDocument:_Document;
> > >   WorkingDocument:TWordDocument;
> > > begin
> > >   MSWord := TWordApplication.Create(nil);
> > >   with MSWord do
> > >   begin
> > >     Connect;        //uses the default ckRunningOrNew
> > >     Visible := True;
> > >     TempDocument := Documents.Open(OpenFileName, etc...)     //I've
> > > previously checked that OpenFileName exists

> > >     WorkingDocument := TWordDocument.Create(nil);
> > >     WorkingDocument.ConnectTo(TempDocument);            //<--this is
> where
> > > it hangs
> > > ...
> > >   end;
> > > end;

> > > It is written in D5, using Word97.pas.  I have this COM object running
> in
> > > debug, under DLLHOST.

> > > When I execute the "WorkingDocument.ConnectTo" statement the
application
> > > goes into limbo.  I've tried wrapping each statement in try..excepts -
> no
> > > exceptions are raised.  The client and MSWord show the busy cursor,
> > waiting
> > > for the COM to finish I guess.  The task manager does not show
excessive
> > CPU
> > > usage as it would if the system were looping.

> > > If MSWord is already running when I execute the COM object it works.
If
> I
> > > change the MSWord.ConnectKind to ckNewInstance then it hangs, even if
> Word
> > > is already running.

> > > Also in the Evaluate/Modify window of in the Watch List window I can
get
> > an
> > > [Evaluating...] message and, again, it hangs.

> > > I've looked in various news groups and compared my code against
> downloaded
> > > samples and I can't see where I'm going wrong.  Any advice would be
> > > appreciated,

> > > Michael Green
> > > michael.gr...@engn.com
> > > +44 (0)28 9448 0000

Other Threads