Alfredo
02-12-2004, 18:54:01
Hola a todos.........., estoy tratando de probar el siguiente codigo que tome
de http://www.djpate.freeserve.co.uk/AutoWord.htm#InsertText
Ejemplo de como usar mailmerge facilmente, pero no he podido hacer que funcione.... Tengo delphi 7, windows xp, office xp
al compilarlo se detiene en
Word.Quit(wdSaveChanges,EmptyParam, EmptyParam);
y el error es:
[Error] Unit1.pas(65): Types of actual and formal var parameters must be identical
Tendran alguna idea por favor:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Wordxp, StdCtrls, Db, DBTables;
type
TMainForm = class(TForm)
CreateMainDocBtn: TButton;
GetDataSourceBtn: TButton;
MergeBtn: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure CreateMainDocBtnClick(Sender: TObject);
procedure GetDataSourceBtnClick(Sender: TObject);
procedure MergeBtnClick(Sender: TObject);
private
Word: _Application;
Doc: _Document;
AliasPath: string;
function GetAliasPath: string;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
uses ComObj, ActiveX;
procedure TMainForm.FormCreate(Sender: TObject);
var
Unknown: IUnknown;
Result: HResult;
begin
// Delphi 5
Result := GetActiveObject(CLASS_WordApplication, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
Word := CoWordApplication.Create
else begin
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
end;
Word.Visible := True;
AliasPath := GetAliasPath;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
var
SaveChanges: OleVariant;
begin
SaveChanges := wdDoNotSaveChanges;
Word.Quit(wdSaveChanges,EmptyParam, EmptyParam); // ******** Presenta error
Doc := nil;
Word := nil;
end;
procedure TMainForm.CreateMainDocBtnClick(Sender: TObject);
var
R: Range;
Direction: OleVariant;
begin
Doc := Word.Documents.Add(EmptyParam, EmptyParam); // ****** presenta error
R := Doc.Range(EmptyParam, EmptyParam);
Direction := wdCollapseEnd;
R.InsertAfter('Dear ');
R.Collapse(Direction);
{ Insert a field with the name of the datasource field }
Doc.MailMerge.Fields.Add(R, 'Company');
R := Doc.Range(EmptyParam, EmptyParam);
R.Collapse(Direction);
R.InsertParagraphAfter;
R.InsertAfter('We have yet to receive payment for our invoice of ');
R.Collapse(Direction);
Doc.MailMerge.Fields.Add(R, 'LastInvoiceDate');
R := Doc.Range(EmptyParam, EmptyParam);
R.Collapse(Direction);
R.InsertAfter('.');
R.InsertParagraphAfter;
R.InsertAfter('Cough up or we''ll send the boys round.');
R.InsertParagraphAfter;
GetDatasourceBtn.Enabled := True;
end;
procedure TMainForm.GetDataSourceBtnClick(Sender: TObject);
var
TblName: string;
Connection, SQL: OleVariant;
begin
{ Open the customer table }
TblName := AliasPath + '\' + 'customer.db';
Connection := Format('DSN=Paradox Files;DBQ=%s;DefaultDir=%s;DriverId=538;MaxBufferSize=2048;PageTimeout=5;', [AliasPath, AliasPath]);
SQL := 'SELECT * FROM customer.db';
Doc.MailMerge.OpenDataSource(TblName, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, Connection,
SQL, EmptyParam);
MergeBtn.Enabled := True;
end;
function TMainForm.GetAliasPath: string;
const
DemoAlias = 'DBDEMOS';
var
AliasParams: TStrings;
begin
Result := 'C:\';
if Session.IsAlias(DemoAlias) then
begin
AliasParams := TStringList.Create;
try
Session.GetAliasParams(DemoAlias, AliasParams);
Result := AliasParams.Values['PATH'];
finally
AliasParams.Free;
end;
end;
end;
procedure TMainForm.MergeBtnClick(Sender: TObject);
var
Pause: OleVariant;
begin
Pause := False;
with Doc.MailMerge do
begin
Destination := wdSendToNewDocument;
Datasource.FirstRecord := wdDefaultFirstRecord;
Datasource.LastRecord := integer(wdDefaultLastRecord);
Execute(Pause);
end;
end;
end.
de http://www.djpate.freeserve.co.uk/AutoWord.htm#InsertText
Ejemplo de como usar mailmerge facilmente, pero no he podido hacer que funcione.... Tengo delphi 7, windows xp, office xp
al compilarlo se detiene en
Word.Quit(wdSaveChanges,EmptyParam, EmptyParam);
y el error es:
[Error] Unit1.pas(65): Types of actual and formal var parameters must be identical
Tendran alguna idea por favor:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Wordxp, StdCtrls, Db, DBTables;
type
TMainForm = class(TForm)
CreateMainDocBtn: TButton;
GetDataSourceBtn: TButton;
MergeBtn: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure CreateMainDocBtnClick(Sender: TObject);
procedure GetDataSourceBtnClick(Sender: TObject);
procedure MergeBtnClick(Sender: TObject);
private
Word: _Application;
Doc: _Document;
AliasPath: string;
function GetAliasPath: string;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
uses ComObj, ActiveX;
procedure TMainForm.FormCreate(Sender: TObject);
var
Unknown: IUnknown;
Result: HResult;
begin
// Delphi 5
Result := GetActiveObject(CLASS_WordApplication, nil, Unknown);
if (Result = MK_E_UNAVAILABLE) then
Word := CoWordApplication.Create
else begin
{ make sure no other error occurred during GetActiveObject }
OleCheck(Result);
OleCheck(Unknown.QueryInterface(_Application, Word));
end;
Word.Visible := True;
AliasPath := GetAliasPath;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
var
SaveChanges: OleVariant;
begin
SaveChanges := wdDoNotSaveChanges;
Word.Quit(wdSaveChanges,EmptyParam, EmptyParam); // ******** Presenta error
Doc := nil;
Word := nil;
end;
procedure TMainForm.CreateMainDocBtnClick(Sender: TObject);
var
R: Range;
Direction: OleVariant;
begin
Doc := Word.Documents.Add(EmptyParam, EmptyParam); // ****** presenta error
R := Doc.Range(EmptyParam, EmptyParam);
Direction := wdCollapseEnd;
R.InsertAfter('Dear ');
R.Collapse(Direction);
{ Insert a field with the name of the datasource field }
Doc.MailMerge.Fields.Add(R, 'Company');
R := Doc.Range(EmptyParam, EmptyParam);
R.Collapse(Direction);
R.InsertParagraphAfter;
R.InsertAfter('We have yet to receive payment for our invoice of ');
R.Collapse(Direction);
Doc.MailMerge.Fields.Add(R, 'LastInvoiceDate');
R := Doc.Range(EmptyParam, EmptyParam);
R.Collapse(Direction);
R.InsertAfter('.');
R.InsertParagraphAfter;
R.InsertAfter('Cough up or we''ll send the boys round.');
R.InsertParagraphAfter;
GetDatasourceBtn.Enabled := True;
end;
procedure TMainForm.GetDataSourceBtnClick(Sender: TObject);
var
TblName: string;
Connection, SQL: OleVariant;
begin
{ Open the customer table }
TblName := AliasPath + '\' + 'customer.db';
Connection := Format('DSN=Paradox Files;DBQ=%s;DefaultDir=%s;DriverId=538;MaxBufferSize=2048;PageTimeout=5;', [AliasPath, AliasPath]);
SQL := 'SELECT * FROM customer.db';
Doc.MailMerge.OpenDataSource(TblName, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, Connection,
SQL, EmptyParam);
MergeBtn.Enabled := True;
end;
function TMainForm.GetAliasPath: string;
const
DemoAlias = 'DBDEMOS';
var
AliasParams: TStrings;
begin
Result := 'C:\';
if Session.IsAlias(DemoAlias) then
begin
AliasParams := TStringList.Create;
try
Session.GetAliasParams(DemoAlias, AliasParams);
Result := AliasParams.Values['PATH'];
finally
AliasParams.Free;
end;
end;
end;
procedure TMainForm.MergeBtnClick(Sender: TObject);
var
Pause: OleVariant;
begin
Pause := False;
with Doc.MailMerge do
begin
Destination := wdSendToNewDocument;
Datasource.FirstRecord := wdDefaultFirstRecord;
Datasource.LastRecord := integer(wdDefaultLastRecord);
Execute(Pause);
end;
end;
end.