BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re-assigning a datasource to an MS Word mailmerge document

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation
View previous topic :: View next topic  
Author Message
Glenn Greatwood
Guest





PostPosted: Sat Jan 17, 2004 7:31 am    Post subject: Re-assigning a datasource to an MS Word mailmerge document Reply with quote



Hi All

I use code to open a pre-designed Word template that has been set-up as a
mail-merge document. It then merges to a new document and closes the
original template down, (this code is shown at the foot of this mail).

However it appears that Word 2003 now requires that the database name needs
to be re-set after opening the document (see my earlier posting of 15th Jan)
which means that the routine no longer works. In search of an answer, I have
used a macro to record in VB terms what is actually being done when I assign
the database to be used for the merge and achieved the following code, some
of which I need to add to my original code to re-assign the datasource:
----------------------------------------------------------------------
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:="C:KeyDataDueRent.dbf",
_
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="",
_
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False,
_
Format:=wdOpenFormatAuto, Connection:= _
"DSN=dBASE
Files;DBQ=C:KeyData;DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;" _
, SQLStatement:="SELECT * FROM DueRent.dbf", SQLStatement1:=""
ActiveDocument.MailMerge.EditMainDocument
-------------------------------------------------------------------------

It looks like the line I need is line 2...but how do I add this to my
original code, shown below?

Original code
--------------------------------------------
WordDocu:=CurrentLetter;
AppWasRunning := False;
Pause:=False;

{$IFDEF VER120} // Delphi 4
Result := GetActiveObject(CLASS_Application_, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
begin //If Word is not running then start it.....
Word := CoApplication_.Create;

FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, //and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end
{$ENDIF}

else begin //Else if Word is running then connect to it.....
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
//set the filename
FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, //and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
AppWasRunning := True;
end;
//does the mail merge stuff
Word.Visible := True;
MM:=Word.ActiveDocument.Mailmerge;
MM.Destination:=wdSendToNewDocument;
MM.Datasource.FirstRecord:=wdDefaultFirstRecord;
MM.DataSource.LastRecord:=integer(wdDefaultLastRecord);
MM.Execute(Pause);
//and closes down the original mail merge document without saving any
changes
Index := FileName;//Index is an OleVariant // we can also use Index := 1;
Word.Documents.Item(Index).Activate;
SaveChs := wdDoNotSaveChanges;
Word.ActiveDocument.Close(SaveChs, EmptyParam, EmptyParam);



ActiveDocument.MailMerge.OpenDataSource Name:="C:KeyDataDueRent.dbf",
--------------------------------------------------
Thank you for your time and any help is much appreciated.

Kind Regards

Glenn Greatwood
Key-Data Systems
www.key-data.co.uk
----------------------------------------------------------------------------
-----------

CONFIDENTIALITY NOTICE
This communication and the information it contains is intended for the
person or organisation to whom it is addressed. Its contents are
confidential and may be protected in law. Unauthorised use, copying or
disclosure of any of it may be unlawful. If you are not the intended
recipient, please contact us immediately.

Whilst Key-Data Systems have taken every reasonable precaution to minimise
the transmission of viruses through the contents of this communication or
any attachments to this e-mail, we cannot accept liability for any damage
which you may sustain as a result of software viruses. You should carry out
your own virus checking procedure before opening any attachment.


Back to top
a
Guest





PostPosted: Sat Jan 17, 2004 8:32 pm    Post subject: Re: Re-assigning a datasource to an MS Word mailmerge docume Reply with quote



Glenn,

I gave you an example:

MSWord.ActiveDocument.MailMerge.MainDocumentType := 0;
MSWord.ActiveDocument.MailMerge.OpenDataSource(
Name := '',
Connection := 'DSN=Merge Source;DriverId=533;FIL=dBase
IV;MaxBufferSize=512;' +
'PageTimeout=600;',
SQLStatement := 'SELECT * FROM MERGE.DBF',
SQLStatement1:='' );

After you open the document, set the MailMerge.MainDocumentType
property and call MailMerge.OpenDataSource -- replacing the parameters
of the OpenDataSource using your own values, i.e:

Word.ActiveDocument.MailMerge.MainDocumentType := 0
Word.ActiveDocument.MailMerge.OpenDataSource(
Name:='C:KeyDataDueRent.dbf',
ConfirmConversions:=False,
ReadOnly:=False,
LinkToSource:=True,
AddToRecentFiles:=False,
PasswordDocument:='',
PasswordTemplate:='',
WritePasswordDocument:='',
WritePasswordTemplate:='',
Revert:=False,
Format:=wdOpenFormatAuto,
Connection:= 'DSN=dBASE Files;DBQ=C:KeyData;' +
'DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;',
SQLStatement:='SELECT * FROM DueRent.dbf',
SQLStatement1:='' );
Word.ActiveDocument.MailMerge.EditMainDocument;

I'm sure you can skip most of the OpenDataSource parameters which are
'' or False.

You can pass parameters in by name during OLE calls using Delphi (in the
same
way as VB does it. You can look up the values VBA constants (such as
wdOpenFormatAuto) in the Word help or displaying their value in a VBA
message box.

Thanks,

Brett




"Glenn Greatwood" <nospam.help (AT) key-data (DOT) co.uk> wrote

Quote:
Hi All

I use code to open a pre-designed Word template that has been set-up as a
mail-merge document. It then merges to a new document and closes the
original template down, (this code is shown at the foot of this mail).

However it appears that Word 2003 now requires that the database name
needs
to be re-set after opening the document (see my earlier posting of 15th
Jan)
which means that the routine no longer works. In search of an answer, I
have
used a macro to record in VB terms what is actually being done when I
assign
the database to be used for the merge and achieved the following code,
some
of which I need to add to my original code to re-assign the datasource:
----------------------------------------------------------------------
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource
Name:="C:KeyDataDueRent.dbf",
_
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="",
_
WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False,
_
Format:=wdOpenFormatAuto, Connection:= _
"DSN=dBASE

Files;DBQ=C:KeyData;DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;" _
, SQLStatement:="SELECT * FROM DueRent.dbf", SQLStatement1:=""
ActiveDocument.MailMerge.EditMainDocument
-------------------------------------------------------------------------

It looks like the line I need is line 2...but how do I add this to my
original code, shown below?

Original code
--------------------------------------------
WordDocu:=CurrentLetter;
AppWasRunning := False;
Pause:=False;

{$IFDEF VER120} // Delphi 4
Result := GetActiveObject(CLASS_Application_, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
begin //If Word is not running then start it.....
Word := CoApplication_.Create;

FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, //and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end
{$ENDIF}

else begin //Else if Word is running then connect to it.....
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
//set the filename
FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, //and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
AppWasRunning := True;
end;
//does the mail merge stuff
Word.Visible := True;
MM:=Word.ActiveDocument.Mailmerge;
MM.Destination:=wdSendToNewDocument;
MM.Datasource.FirstRecord:=wdDefaultFirstRecord;
MM.DataSource.LastRecord:=integer(wdDefaultLastRecord);
MM.Execute(Pause);
//and closes down the original mail merge document without saving any
changes
Index := FileName;//Index is an OleVariant // we can also use Index := 1;
Word.Documents.Item(Index).Activate;
SaveChs := wdDoNotSaveChanges;
Word.ActiveDocument.Close(SaveChs, EmptyParam, EmptyParam);



ActiveDocument.MailMerge.OpenDataSource Name:="C:KeyDataDueRent.dbf",
--------------------------------------------------
Thank you for your time and any help is much appreciated.

Kind Regards

Glenn Greatwood
Key-Data Systems
www.key-data.co.uk
--------------------------------------------------------------------------
--
-----------

CONFIDENTIALITY NOTICE
This communication and the information it contains is intended for the
person or organisation to whom it is addressed. Its contents are
confidential and may be protected in law. Unauthorised use, copying or
disclosure of any of it may be unlawful. If you are not the intended
recipient, please contact us immediately.

Whilst Key-Data Systems have taken every reasonable precaution to minimise
the transmission of viruses through the contents of this communication or
any attachments to this e-mail, we cannot accept liability for any damage
which you may sustain as a result of software viruses. You should carry
out
your own virus checking procedure before opening any attachment.





Back to top
Glenn Greatwood
Guest





PostPosted: Sun Jan 18, 2004 6:29 am    Post subject: Re: Re-assigning a datasource to an MS Word mailmerge docume Reply with quote



Hi Brett

Yes I do appreciate you example and spent some time trying to get it to
work..,this is my fault as I don't fully understand what I'm doing Wink, but
trying to build in the code you supplied led to error messages.

The current state of play is I have added the lines;

Word.ActiveDocument.MailMerge.MainDocumentType := 0;

Word.ActiveDocument.MailMerge.OpenDataSource(Name:='C:KeyDataDueRent.dbf'
);

to my merge code but get the error message;

[Error] FrmMergeLetter.pas(75): Incompatible types: 'WideString' and
'procedure, untyped pointer or untyped parameter'

when I try to compile....

I am using Delphi V 4

Any ideas?

Thanks again for your time.

Regards

Glenn

"a" <blwatters (AT) shaw (DOT) ca> wrote

Quote:
Glenn,

I gave you an example:

MSWord.ActiveDocument.MailMerge.MainDocumentType := 0;
MSWord.ActiveDocument.MailMerge.OpenDataSource(
Name := '',
Connection := 'DSN=Merge Source;DriverId=533;FIL=dBase
IV;MaxBufferSize=512;' +
'PageTimeout=600;',
SQLStatement := 'SELECT * FROM MERGE.DBF',
SQLStatement1:='' );

After you open the document, set the MailMerge.MainDocumentType
property and call MailMerge.OpenDataSource -- replacing the parameters
of the OpenDataSource using your own values, i.e:

Word.ActiveDocument.MailMerge.MainDocumentType := 0
Word.ActiveDocument.MailMerge.OpenDataSource(
Name:='C:KeyDataDueRent.dbf',
ConfirmConversions:=False,
ReadOnly:=False,
LinkToSource:=True,
AddToRecentFiles:=False,
PasswordDocument:='',
PasswordTemplate:='',
WritePasswordDocument:='',
WritePasswordTemplate:='',
Revert:=False,
Format:=wdOpenFormatAuto,
Connection:= 'DSN=dBASE Files;DBQ=C:KeyData;' +
'DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;',
SQLStatement:='SELECT * FROM DueRent.dbf',
SQLStatement1:='' );
Word.ActiveDocument.MailMerge.EditMainDocument;

I'm sure you can skip most of the OpenDataSource parameters which are
'' or False.

You can pass parameters in by name during OLE calls using Delphi (in the
same
way as VB does it. You can look up the values VBA constants (such as
wdOpenFormatAuto) in the Word help or displaying their value in a VBA
message box.

Thanks,

Brett




"Glenn Greatwood" <nospam.help (AT) key-data (DOT) co.uk> wrote in message
news:4008e4cb (AT) newsgroups (DOT) borland.com...
Hi All

I use code to open a pre-designed Word template that has been set-up as
a
mail-merge document. It then merges to a new document and closes the
original template down, (this code is shown at the foot of this mail).

However it appears that Word 2003 now requires that the database name
needs
to be re-set after opening the document (see my earlier posting of 15th
Jan)
which means that the routine no longer works. In search of an answer, I
have
used a macro to record in VB terms what is actually being done when I
assign
the database to be used for the merge and achieved the following code,
some
of which I need to add to my original code to re-assign the datasource:
----------------------------------------------------------------------
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource
Name:="C:KeyDataDueRent.dbf",
_
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True,
_
AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="",
_
WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False,
_
Format:=wdOpenFormatAuto, Connection:= _
"DSN=dBASE


Files;DBQ=C:KeyData;DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;" _
, SQLStatement:="SELECT * FROM DueRent.dbf", SQLStatement1:=""
ActiveDocument.MailMerge.EditMainDocument

-------------------------------------------------------------------------

It looks like the line I need is line 2...but how do I add this to my
original code, shown below?

Original code
--------------------------------------------
WordDocu:=CurrentLetter;
AppWasRunning := False;
Pause:=False;

{$IFDEF VER120} // Delphi 4
Result := GetActiveObject(CLASS_Application_, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
begin //If Word is not running then start it.....
Word := CoApplication_.Create;

FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, //and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end
{$ENDIF}

else begin //Else if Word is running then connect to it.....
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
//set the filename
FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, //and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
AppWasRunning := True;
end;
//does the mail merge stuff
Word.Visible := True;
MM:=Word.ActiveDocument.Mailmerge;
MM.Destination:=wdSendToNewDocument;
MM.Datasource.FirstRecord:=wdDefaultFirstRecord;
MM.DataSource.LastRecord:=integer(wdDefaultLastRecord);
MM.Execute(Pause);
//and closes down the original mail merge document without saving any
changes
Index := FileName;//Index is an OleVariant // we can also use Index :=
1;
Word.Documents.Item(Index).Activate;
SaveChs := wdDoNotSaveChanges;
Word.ActiveDocument.Close(SaveChs, EmptyParam, EmptyParam);



ActiveDocument.MailMerge.OpenDataSource Name:="C:KeyDataDueRent.dbf",
--------------------------------------------------
Thank you for your time and any help is much appreciated.

Kind Regards

Glenn Greatwood
Key-Data Systems
www.key-data.co.uk

--------------------------------------------------------------------------
--
-----------

CONFIDENTIALITY NOTICE
This communication and the information it contains is intended for the
person or organisation to whom it is addressed. Its contents are
confidential and may be protected in law. Unauthorised use, copying or
disclosure of any of it may be unlawful. If you are not the intended
recipient, please contact us immediately.

Whilst Key-Data Systems have taken every reasonable precaution to
minimise
the transmission of viruses through the contents of this communication
or
any attachments to this e-mail, we cannot accept liability for any
damage
which you may sustain as a result of software viruses. You should carry
out
your own virus checking procedure before opening any attachment.







Back to top
a
Guest





PostPosted: Sun Jan 18, 2004 10:12 pm    Post subject: Re: Re-assigning a datasource to an MS Word mailmerge docume Reply with quote

Glenn,

Ensure that Word is a variant. I don't think you can get away with
the ( Param := ParamValue; ) calling syntax if you are using a type
library.

Try, either:

var MailMerge : Variant;

MailMerge = Word.ActiveDocument.MailMerge;

MailMerge.OpenDataSource( Name:='C:Key...' );

or just making your Word variable a variant.

Thanks,

Brett


"Glenn Greatwood" <nospam.help (AT) key-data (DOT) co.uk> wrote

Quote:
Hi Brett

Yes I do appreciate you example and spent some time trying to get it to
work..,this is my fault as I don't fully understand what I'm doing Wink,
but
trying to build in the code you supplied led to error messages.

The current state of play is I have added the lines;

Word.ActiveDocument.MailMerge.MainDocumentType := 0;


Word.ActiveDocument.MailMerge.OpenDataSource(Name:='C:KeyDataDueRent.dbf'
);

to my merge code but get the error message;

[Error] FrmMergeLetter.pas(75): Incompatible types: 'WideString' and
'procedure, untyped pointer or untyped parameter'

when I try to compile....

I am using Delphi V 4

Any ideas?

Thanks again for your time.

Regards

Glenn

"a" <blwatters (AT) shaw (DOT) ca> wrote in message
news:40099bf1$1 (AT) newsgroups (DOT) borland.com...
Glenn,

I gave you an example:

MSWord.ActiveDocument.MailMerge.MainDocumentType := 0;
MSWord.ActiveDocument.MailMerge.OpenDataSource(
Name := '',
Connection := 'DSN=Merge Source;DriverId=533;FIL=dBase
IV;MaxBufferSize=512;' +
'PageTimeout=600;',
SQLStatement := 'SELECT * FROM MERGE.DBF',
SQLStatement1:='' );

After you open the document, set the MailMerge.MainDocumentType
property and call MailMerge.OpenDataSource -- replacing the parameters
of the OpenDataSource using your own values, i.e:

Word.ActiveDocument.MailMerge.MainDocumentType := 0
Word.ActiveDocument.MailMerge.OpenDataSource(
Name:='C:KeyDataDueRent.dbf',
ConfirmConversions:=False,
ReadOnly:=False,
LinkToSource:=True,
AddToRecentFiles:=False,
PasswordDocument:='',
PasswordTemplate:='',
WritePasswordDocument:='',
WritePasswordTemplate:='',
Revert:=False,
Format:=wdOpenFormatAuto,
Connection:= 'DSN=dBASE Files;DBQ=C:KeyData;' +
'DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;',
SQLStatement:='SELECT * FROM DueRent.dbf',
SQLStatement1:='' );
Word.ActiveDocument.MailMerge.EditMainDocument;

I'm sure you can skip most of the OpenDataSource parameters which are
'' or False.

You can pass parameters in by name during OLE calls using Delphi (in the
same
way as VB does it. You can look up the values VBA constants (such as
wdOpenFormatAuto) in the Word help or displaying their value in a VBA
message box.

Thanks,

Brett




"Glenn Greatwood" <nospam.help (AT) key-data (DOT) co.uk> wrote in message
news:4008e4cb (AT) newsgroups (DOT) borland.com...
Hi All

I use code to open a pre-designed Word template that has been set-up
as
a
mail-merge document. It then merges to a new document and closes the
original template down, (this code is shown at the foot of this mail).

However it appears that Word 2003 now requires that the database name
needs
to be re-set after opening the document (see my earlier posting of
15th
Jan)
which means that the routine no longer works. In search of an answer,
I
have
used a macro to record in VB terms what is actually being done when I
assign
the database to be used for the merge and achieved the following code,
some
of which I need to add to my original code to re-assign the
datasource:
----------------------------------------------------------------------
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource
Name:="C:KeyDataDueRent.dbf",
_
ConfirmConversions:=False, ReadOnly:=False,
LinkToSource:=True,
_
AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="",
_
WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False,
_
Format:=wdOpenFormatAuto, Connection:= _
"DSN=dBASE



Files;DBQ=C:KeyData;DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;" _
, SQLStatement:="SELECT * FROM DueRent.dbf", SQLStatement1:=""
ActiveDocument.MailMerge.EditMainDocument


-------------------------------------------------------------------------

It looks like the line I need is line 2...but how do I add this to my
original code, shown below?

Original code
--------------------------------------------
WordDocu:=CurrentLetter;
AppWasRunning := False;
Pause:=False;

{$IFDEF VER120} // Delphi 4
Result := GetActiveObject(CLASS_Application_, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
begin //If Word is not running then start it.....
Word := CoApplication_.Create;

FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam,
//and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end
{$ENDIF}

else begin //Else if Word is running then connect to it.....
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
//set the filename
FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam,
//and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
AppWasRunning := True;
end;
//does the mail merge stuff
Word.Visible := True;
MM:=Word.ActiveDocument.Mailmerge;
MM.Destination:=wdSendToNewDocument;
MM.Datasource.FirstRecord:=wdDefaultFirstRecord;
MM.DataSource.LastRecord:=integer(wdDefaultLastRecord);
MM.Execute(Pause);
//and closes down the original mail merge document without saving any
changes
Index := FileName;//Index is an OleVariant // we can also use Index :=
1;
Word.Documents.Item(Index).Activate;
SaveChs := wdDoNotSaveChanges;
Word.ActiveDocument.Close(SaveChs, EmptyParam, EmptyParam);



ActiveDocument.MailMerge.OpenDataSource
Name:="C:KeyDataDueRent.dbf",
--------------------------------------------------
Thank you for your time and any help is much appreciated.

Kind Regards

Glenn Greatwood
Key-Data Systems
www.key-data.co.uk


--------------------------------------------------------------------------
--
-----------

CONFIDENTIALITY NOTICE
This communication and the information it contains is intended for the
person or organisation to whom it is addressed. Its contents are
confidential and may be protected in law. Unauthorised use, copying or
disclosure of any of it may be unlawful. If you are not the intended
recipient, please contact us immediately.

Whilst Key-Data Systems have taken every reasonable precaution to
minimise
the transmission of viruses through the contents of this communication
or
any attachments to this e-mail, we cannot accept liability for any
damage
which you may sustain as a result of software viruses. You should
carry
out
your own virus checking procedure before opening any attachment.









Back to top
Glenn Greatwood
Guest





PostPosted: Mon Jan 19, 2004 6:01 am    Post subject: Re: Re-assigning a datasource to an MS Word mailmerge docume Reply with quote

Brett

You're a star, that now works well.

Many, many thanks.

Glenn

"a" <blwatters (AT) shaw (DOT) ca> wrote

Quote:
Glenn,

Ensure that Word is a variant. I don't think you can get away with
the ( Param := ParamValue; ) calling syntax if you are using a type
library.

Try, either:

var MailMerge : Variant;

MailMerge = Word.ActiveDocument.MailMerge;

MailMerge.OpenDataSource( Name:='C:Key...' );

or just making your Word variable a variant.

Thanks,

Brett


"Glenn Greatwood" <nospam.help (AT) key-data (DOT) co.uk> wrote in message
news:400a27d4 (AT) newsgroups (DOT) borland.com...
Hi Brett

Yes I do appreciate you example and spent some time trying to get it to
work..,this is my fault as I don't fully understand what I'm doing Wink,
but
trying to build in the code you supplied led to error messages.

The current state of play is I have added the lines;

Word.ActiveDocument.MailMerge.MainDocumentType := 0;



Word.ActiveDocument.MailMerge.OpenDataSource(Name:='C:KeyDataDueRent.dbf'
);

to my merge code but get the error message;

[Error] FrmMergeLetter.pas(75): Incompatible types: 'WideString' and
'procedure, untyped pointer or untyped parameter'

when I try to compile....

I am using Delphi V 4

Any ideas?

Thanks again for your time.

Regards

Glenn

"a" <blwatters (AT) shaw (DOT) ca> wrote in message
news:40099bf1$1 (AT) newsgroups (DOT) borland.com...
Glenn,

I gave you an example:

MSWord.ActiveDocument.MailMerge.MainDocumentType := 0;
MSWord.ActiveDocument.MailMerge.OpenDataSource(
Name := '',
Connection := 'DSN=Merge Source;DriverId=533;FIL=dBase
IV;MaxBufferSize=512;' +
'PageTimeout=600;',
SQLStatement := 'SELECT * FROM MERGE.DBF',
SQLStatement1:='' );

After you open the document, set the MailMerge.MainDocumentType
property and call MailMerge.OpenDataSource -- replacing the parameters
of the OpenDataSource using your own values, i.e:

Word.ActiveDocument.MailMerge.MainDocumentType := 0
Word.ActiveDocument.MailMerge.OpenDataSource(
Name:='C:KeyDataDueRent.dbf',
ConfirmConversions:=False,
ReadOnly:=False,
LinkToSource:=True,
AddToRecentFiles:=False,
PasswordDocument:='',
PasswordTemplate:='',
WritePasswordDocument:='',
WritePasswordTemplate:='',
Revert:=False,
Format:=wdOpenFormatAuto,
Connection:= 'DSN=dBASE Files;DBQ=C:KeyData;' +
'DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;',
SQLStatement:='SELECT * FROM DueRent.dbf',
SQLStatement1:='' );
Word.ActiveDocument.MailMerge.EditMainDocument;

I'm sure you can skip most of the OpenDataSource parameters which are
'' or False.

You can pass parameters in by name during OLE calls using Delphi (in
the
same
way as VB does it. You can look up the values VBA constants (such as
wdOpenFormatAuto) in the Word help or displaying their value in a VBA
message box.

Thanks,

Brett




"Glenn Greatwood" <nospam.help (AT) key-data (DOT) co.uk> wrote in message
news:4008e4cb (AT) newsgroups (DOT) borland.com...
Hi All

I use code to open a pre-designed Word template that has been set-up
as
a
mail-merge document. It then merges to a new document and closes the
original template down, (this code is shown at the foot of this
mail).

However it appears that Word 2003 now requires that the database
name
needs
to be re-set after opening the document (see my earlier posting of
15th
Jan)
which means that the routine no longer works. In search of an
answer,
I
have
used a macro to record in VB terms what is actually being done when
I
assign
the database to be used for the merge and achieved the following
code,
some
of which I need to add to my original code to re-assign the
datasource:

----------------------------------------------------------------------
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource
Name:="C:KeyDataDueRent.dbf",
_
ConfirmConversions:=False, ReadOnly:=False,
LinkToSource:=True,
_
AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="",
_
WritePasswordDocument:="", WritePasswordTemplate:="",
Revert:=False,
_
Format:=wdOpenFormatAuto, Connection:= _
"DSN=dBASE




Files;DBQ=C:KeyData;DefaultDir=C:Key;DriverId=533;MaxBufferSize=2048;Page
Timeout=5;" _
, SQLStatement:="SELECT * FROM DueRent.dbf",
SQLStatement1:=""
ActiveDocument.MailMerge.EditMainDocument



-------------------------------------------------------------------------

It looks like the line I need is line 2...but how do I add this to
my
original code, shown below?

Original code
--------------------------------------------
WordDocu:=CurrentLetter;
AppWasRunning := False;
Pause:=False;

{$IFDEF VER120} // Delphi 4
Result := GetActiveObject(CLASS_Application_, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
begin //If Word is not running then start it.....
Word := CoApplication_.Create;

FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam,
//and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end
{$ENDIF}

else begin //Else if Word is running then connect to it.....
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
//set the filename
FileName := (WordDocu);//Sets the chosen file
Word.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam,
//and
opens it
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
AppWasRunning := True;
end;
//does the mail merge stuff
Word.Visible := True;
MM:=Word.ActiveDocument.Mailmerge;
MM.Destination:=wdSendToNewDocument;
MM.Datasource.FirstRecord:=wdDefaultFirstRecord;
MM.DataSource.LastRecord:=integer(wdDefaultLastRecord);
MM.Execute(Pause);
//and closes down the original mail merge document without saving
any
changes
Index := FileName;//Index is an OleVariant // we can also use Index
:=
1;
Word.Documents.Item(Index).Activate;
SaveChs := wdDoNotSaveChanges;
Word.ActiveDocument.Close(SaveChs, EmptyParam, EmptyParam);



ActiveDocument.MailMerge.OpenDataSource
Name:="C:KeyDataDueRent.dbf",
--------------------------------------------------
Thank you for your time and any help is much appreciated.

Kind Regards

Glenn Greatwood
Key-Data Systems
www.key-data.co.uk



--------------------------------------------------------------------------
--
-----------

CONFIDENTIALITY NOTICE
This communication and the information it contains is intended for
the
person or organisation to whom it is addressed. Its contents are
confidential and may be protected in law. Unauthorised use, copying
or
disclosure of any of it may be unlawful. If you are not the intended
recipient, please contact us immediately.

Whilst Key-Data Systems have taken every reasonable precaution to
minimise
the transmission of viruses through the contents of this
communication
or
any attachments to this e-mail, we cannot accept liability for any
damage
which you may sustain as a result of software viruses. You should
carry
out
your own virus checking procedure before opening any attachment.











Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.