Solved

OLE with Word

Posted on 1997-08-29
7
305 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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