Board index » delphi » Wordbasic from Delphi (2.0)

Wordbasic from Delphi (2.0)

Hi all

I am trying to figure out how to use wordbasic from a delphi program.
I cannot figure out how the general rule for passing parameters work.

  WORD:=CreateOleObject('word.basic');

The statement MailMergeMainDocumentType 0 looks like this in Delphi:
  WORD.MailMergeMainDocumentType(0);

Everything is fine this far, but what happens when I want to run the statement
that looks like this in the word macro editor:
MailMergeOpenDataSource .Name = "C:\Fil", .Readonly = 0 and several other
parameters, but how to pass it throgh to word? The only thing that goes through
without an error from Word is:
  WORD.MailMergeOpenDataSource('C:\Fil');
It seems to me that Word only expects a file name when I call this from
delphi, I cannot specify parameters with .Name=, .ReadOnly= and so on.

Where can I look up how these calls are supposed to look, since they
obviously cannot look the same as in macro editor?
I am using  the book "Microsoft Word Developers kit" (MSPress), but the
syntax here is generally VB-oriented.
I also cannot run function/procedures with the $ in the name, my book
proposes for instance mark = WORD.[GetBookmark$]("Address") in VB which
I cannot figure out in Pascal.

Any idea how to get on from here? Help for helping myself further would be
appreciated. Thanks!

Morten Steinvik
--
           Disk not in drive C:  (A)bort   (R)etry   (S)uicide
        The world is but one country and the people its citizens
        Jogger@IRC        http://www.colargol.idb.hist.no/~mortenst/

 

Re:Wordbasic from Delphi (2.0)


Quote
Morten Steinvik wrote:

> Hi all

> I am trying to figure out how to use wordbasic from a delphi program.
> I cannot figure out how the general rule for passing parameters work.

>   WORD:=CreateOleObject('word.basic');

> The statement MailMergeMainDocumentType 0 looks like this in Delphi:
>   WORD.MailMergeMainDocumentType(0);

> Everything is fine this far, but what happens when I want to run the statement
> that looks like this in the word macro editor:
> MailMergeOpenDataSource .Name = "C:\Fil", .Readonly = 0 and several other
> parameters, but how to pass it throgh to word? The only thing that goes through
> without an error from Word is:
>   WORD.MailMergeOpenDataSource('C:\Fil');
> It seems to me that Word only expects a file name when I call this from
> delphi, I cannot specify parameters with .Name=, .ReadOnly= and so on.

> Where can I look up how these calls are supposed to look, since they
> obviously cannot look the same as in macro editor?
> I am using  the book "Microsoft Word Developers kit" (MSPress), but the
> syntax here is generally VB-oriented.
> I also cannot run function/procedures with the $ in the name, my book
> proposes for instance mark = WORD.[GetBookmark$]("Address") in VB which
> I cannot figure out in Pascal.

> Any idea how to get on from here? Help for helping myself further would be
> appreciated. Thanks!

> Morten Steinvik
> --
>            Disk not in drive C:  (A)bort   (R)etry   (S)uicide
>         The world is but one country and the people its citizens
>         Jogger@IRC        http://www.colargol.idb.hist.no/~mortenst/

Hello,

Here is some code done for Delphi 2.0 to Word 95.  I tested a little bit
when Word 97 came out and a few changes were required -- didn't actually
convert to Word 97 because at the time it proved to be significantly
slower than Word 95.  The SendKeys were used out of desperation.

Function TAutoMerge.ProcessMerge(FSource, FData, FOutput : string) :
boolean;
var
   MSWord  : Variant;
   i, NumDocs : integer;
   Found : boolean;
   s, LastOLECommand : string;
begin
   ProcessMerge := False;
   try
     LastOLECommand := 'Creating OLE Object.';
     MSWord := CreateOLEObject('Word.Basic');
     LastOLECommand := 'Show MS Word.';
     MSWord.AppShow;
     Application.ProcessMessages;

     LastOLECommand := 'Open document file >' + FSource + '<.';
     MSWord.FileOpen(Name := FSource, ConfirmConversions := 0,
                    ReadOnly := 1, AddToMru := 0, PasswordDoc := '',
                    PasswordDot := '', Revert := 0, WritePasswordDoc :=
'',
                    WritePasswordDot := '');
     LastOLECommand := 'Screen updating = false.';
     MSWord.ToolsOptionsSpelling(AutomaticSpellChecking := 0);    
     LastOLECommand := 'Set background printing to off.';
     MSWord.ToolsOptionsPrint(Background := 0);

     Application.ProcessMessages;
     LastOLECommand := 'Open Data file >' + FData + '<.';
     MSWord.MailMergeOpenDataSource(Name := FData, ConfirmConversions :=
0,
                                    ReadOnly := 1, LinkToSource := 1,
AddToMru := 0,
                                    PasswordDoc := '', PasswordDot :=
'',
                                    WritePasswordDoc := '',
WritePasswordDot := '',
                                    Connection := '', SQLStatement :=
'', SQLStatement1 := '',
                                    Revert := 1);

     LastOLECommand := 'Start the Mail Merge.';
     MSWord.MailMerge(CheckErrors := 2, Destination := 1, MergeRecords
:= 0,
                      From := '', To := '', Suppression := 0,
MailSubject := '',
                      MailAsAttachment := 0, MailAddress := '');

     LastOLECommand := 'Set up for SendKeys to select printer.';
     Application.ProcessMessages;
     MSWord.AppShow;
     s := '{home}%l{enter}{home}%n' + FOutput +
'{tab}{enter}{home}{enter}';
                           // sdd 1.1
     MSWord.SendKeys(s, -1);
     MSWord.MailMergeToPrinter;
     Application.ProcessMessages;

     ProcessMerge := True;
     LastOLECommand := 'All done with merge.';
   except
     on EOleException do
       begin
         inc(TotalOLEErrors);
         lblStatus.caption := LastOLECommand;
         if (TotalOLEErrors >= TOTALOLEERRORS_MAX) then
            begin
              s := 'There has been at least one OLE error(' +
IntToStr(TotalOLEErrors) +
                   '), the last one was >' + LastOLECommand + '<.';
              ShowMessage(s);
            end;
       end
   end;
end;

Other Threads