Solved

OLE with Word

Posted on 1997-08-29
7
309 Views
Last Modified: 2010-04-06

In one of my softwares, I use OLE to transfer data into a Word file.  Each time I use this code, I open a NEW Word window.  How can I insert the data in the ACTIVE DOCUMENT if Word is already open?
0
Comment
Question by:jpdupont
  • 4
  • 3
7 Comments
 
LVL 1

Expert Comment

by:sassas081597
ID: 1342899
I did not test the code bellow. That is why I write a comment. If I am correct - you can ask me to write the answer.

procedure TForm1.Button1.Click(S: TObject);
begin
    V:=CreateOleObject('Word.Basic');
    If v.documents.count>0 then
       v.documents[1].activate
    else
       v.FileNew('Normal');
    v.Insert('I want to insert something there');
end;
0
 

Author Comment

by:jpdupont
ID: 1342900
Your code give the error :
"'documents' method non supported by automation object"
(in french : "Méthode 'documents' non supportée par l'objet automation'"

I use DELPHI 3 in french
WORD97 french

My code :
=========
procedure TPHOTOTHEQUE.SpeedButton7Click(Sender: TObject);
var v:variant;
begin
   V := CreateOleObject('Word.Basic');
   V.AppShow;
   V.FileNew;
   if DataModule1.Table1NOMIMAGE.Value <>'' then
   begin     v.InsertPicture(REP_I_DATA+'\'+DataModule1.Table1NOMIMAGE.Value,true,false);
      V.Insert(#13+#13+DataModule1.Table1LEGENDE.Value);
   end;
==========
works fine but always open a NEW Word window. How can I insert the data in the ACTIVE DOCUMENT if Word is
                     already open?

0
 
LVL 1

Accepted Solution

by:
sassas081597 earned 50 total points
ID: 1342901
Here is what you need!

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls, OleAuto, ComObj, ActiveX;

procedure TPHOTOTHEQUE.SpeedButton7Click(Sender: TObject);
var v:variant;
  ClassID: TCLSID;
  Unknown: IUnknown;
  x: IDispatch;
begin
  try
    ClassID := ProgIDToClassID('Word.Basic');
    OleCheck(GetActiveObject(ClassID, nil, Unknown));
    OleCheck(Unknown.QueryInterface(IDispatch, x));
    V:=x;
    V.AppShow;
    V.Insert('Hallo from Delphi');{or what you really need}
  finally
     v:=Null;
  end;
end;

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jpdupont
ID: 1342902
Its OK when WORD is loaded.
Error if Word isn't loaded !!!

0
 

Author Comment

by:jpdupont
ID: 1342903
Its OK when WORD is loaded.
Error if Word isn't loaded !!!

0
 
LVL 1

Expert Comment

by:sassas081597
ID: 1342904
AS you see, I added TRY statement.
try
......
v.Insert('Hallo');
except
v:=CreateOleObject('Word.Basic');
.......
end;
v:=Null;
0
 

Author Comment

by:jpdupont
ID: 1342905
It's FINE !
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question