Board index » delphi » Word 97 MailMerge using a Textfile

Word 97 MailMerge using a Textfile

I am using D4 trying to setup a Word 97 mail merge component.

I can do almost everything with regarding setting up an OLE automation to
word except when I try to open a datasource it wont open. There is a work
around by manually opening the datasource and having the "select method"
ticked which brings up the option to select a "text" file. Now how the hell
do you do this via automation.

I'm using the following command to try to open the datasource

WordApp.ActiveDocument.MailMerge.OpenDataSource('MrgData.txt',,True);

If anyone knows what I'm doing wrong please help

Regards

John.

 

Re:Word 97 MailMerge using a Textfile


Example of workable code:
........
var
  oleOpenFormat, oleTrue, oleFalse: oleVariant;
begin
.....
  oleTrue := True;
  oleFalse := False;
  oleOpenFormat := wdOpenFormatText;
  WordDoc.MailMerge.OpenDatasource('MrgData.txt', oleOpenFormat, oleFalse,
    oleTrue, oleFalse, oleFalse, EmptyParam, EmptyParam, oleTrue,
EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam);
  WordDoc.MailMerge.Execute(oleTrue);
.....

The parameters have to be local variables, type oleVariant.

Steve

Quote
John Fraser <jlfra...@bigpond.com> wrote in message

news:7ottl2$elf6@forums.borland.com...
Quote
> I am using D4 trying to setup a Word 97 mail merge component.

> I can do almost everything with regarding setting up an OLE automation to
> word except when I try to open a datasource it wont open. There is a work
> around by manually opening the datasource and having the "select method"
> ticked which brings up the option to select a "text" file. Now how the
hell
> do you do this via automation.

> I'm using the following command to try to open the datasource

> WordApp.ActiveDocument.MailMerge.OpenDataSource('MrgData.txt',,True);

> If anyone knows what I'm doing wrong please help

> Regards

> John.

Re:Word 97 MailMerge using a Textfile


Here is a working example - I hope it helps.

{***************************************************************************
***}
procedure TWord7MergeForm.FormActivate(Sender: TObject);
{
Purpose:    Delphi4 OLE interface to MSWord7.
This is a crude example of how to create an OLE object that communicates
with
WORD 7 using WordBasic commands. This particular example performs a
wordmerge
of Master.doc and FlatDB.txt.
****************************************************************************
***}

var
OLEContainer : OLEVariant;
OK : Boolean;

begin
  Application.ProcessMessages;

  try                                               {Create an OLE Object}
    OLEContainer := CreateOLEObject('Word.Basic');
    OK := true;
  except on E : Exception do
    begin
      OK := false;
      if E.message = 'Invalid class string' then
        messagedlg('Unable to start Microsoft Word version 7,'+
        chr(13)+chr(10)+chr(13)+chr(10)+
        ' Please check that it is properly installed'+chr(13)+chr(10)+
        ' and that the version number is correct' , mtError , [mbOK] , 0)
      else messagedlg(E.message, mtConfirmation , [mbOK] , 0);
    end;                                              {Create an OLE Object}
  end;

  if OK then
  begin
    try                                               {open the Master
document}
      OLEContainer.FileOpen('H:\users\john\OLE_Example\Master.doc');
    except
      on E : Exception do   messagedlg(E.message, mtConfirmation , [mbOK] ,
0);
    end;

    try                            {open the named text file AS the data
source}

OLEContainer.MailMergeOpenDataSource('H:\users\john\OLE_Example\FlatDB.txt')
;
    except
      on E : Exception do
      begin
        messagedlg(E.message, mtConfirmation , [mbOK] , 0);
        OLEContainer.AppShow;
        OLEContainer.AppMaximize;
          {In Word 7 EOLEError messages are usually "Command failed" so
open }
          { OLE App as this sometimes gives clues. Also when visible some
errors}
          { prompt the user for solutions - eg "Field <<Surname>> does not }
          {exist - do you want to use <<Family Name>> instead"}
      end;
    end;

    try
      OLEContainer.MailMerge(1,0,0,0,0,1,true);
          {Creates a new word document containing all of the merged
documents
                                       separated by end-of-section
separators}
    except on E : Exception do
      begin
        messagedlg(E.message, mtConfirmation , [mbOK] , 0);
        OLEContainer.AppShow;
        OLEContainer.AppMaximize;
      end;
    end;

    try
      OLEContainer.FilePrint(0);
            {FilePrintDefault does background printing by default ?}
            {we have to wait for printing before closing Word so use
FilePrint}
            {Could also merge to the printer instead of a file}
    except on E : Exception do
      begin
        messagedlg(E.message, mtConfirmation , [mbOK] , 0);
        OLEContainer.AppShow;
        OLEContainer.AppMaximize;
      end;
    end;

    try
      OLEContainer.FileClose(2); {Close merge results}
    except on E : Exception do
        messagedlg(E.message, mtConfirmation , [mbOK] , 0);
    end;

    try
      OLEContainer.FileClose(2); {Close Master Document}
    except on E : Exception do
        messagedlg(E.message, mtConfirmation , [mbOK] , 0);
    end;
    Screen.Cursor := CrHourGlass;
    Application.ProcessMessages;
  end;
  Close;
end;

{OLE session terminated when OLEVariant destroyed}

Quote
John Fraser wrote in message <7ottl2$e...@forums.borland.com>...
>I am using D4 trying to setup a Word 97 mail merge component.

>I can do almost everything with regarding setting up an OLE automation to
>word except when I try to open a datasource it wont open. There is a work
>around by manually opening the datasource and having the "select method"
>ticked which brings up the option to select a "text" file. Now how the hell
>do you do this via automation.

>I'm using the following command to try to open the datasource

>WordApp.ActiveDocument.MailMerge.OpenDataSource('MrgData.txt',,True);

>If anyone knows what I'm doing wrong please help

>Regards

>John.

Re:Word 97 MailMerge using a Textfile


Warning: never use ''.txt''! Some Word configurations take it as DOS-text some
as Windows-text (depends f.e. wether ODBC is installed or not, no chance to
control, I had huge problems).
Call your DataSource ''MrgData.abc" f.e. (every Word configuration will
interprete it as Windows-text).
Then use:
op1 := WordApp.Options.ConfirmConversions;
WordApp.Options.ConfirmConversions := false;
WordApp.ActiveDocument.MailMerge.OpenDataSource(DataSource, 0, false, true,
false);
...
WordApp.Options.ConfirmConversions := op1;

Regards
Michael Meller

John Fraser schrieb:

Quote
> I am using D4 trying to setup a Word 97 mail merge component.

> I can do almost everything with regarding setting up an OLE automation to
> word except when I try to open a datasource it wont open. There is a work
> around by manually opening the datasource and having the "select method"
> ticked which brings up the option to select a "text" file. Now how the hell
> do you do this via automation.

> I'm using the following command to try to open the datasource

> WordApp.ActiveDocument.MailMerge.OpenDataSource('MrgData.txt',,True);

> If anyone knows what I'm doing wrong please help

> Regards

> John.

Other Threads