Solved

OLE with Word

Posted on 1997-08-29
7
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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!

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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