Link to home
Start Free TrialLog in
Avatar of maatwerk
maatwerk

asked on

Invalid typecase error while parsing autotext.

I am trying to create a mailmerge procedure in Delphi 6. But i always get an invalid typecase error, when i try to pas "autotext" as an parameter. If i leave this parameter empty no error occurs. But i need this parameter in order to parse the mergefields...

Please Help!


procedure TForm1.Button1Click(Sender: TObject);
var
oLabel,oAutotext,oAdres,oEtiket,oTimeout,opause:OleVariant;
begin
oEtiket:='xxxx';
olabel:='8389';
oAdres:='Address:=';
oTimeout:=30;
with oApp.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam).MailMerge.Fields do Begin
Add(Application.Selection.Range,'Contact_Name');
Application.Selection.TypeParagraph;
Add(Application.Selection.Range,'oAddress');
Application.Selection.TypeParagraph;
Add(Application.Selection.Range,'City');
Application.Selection.TypeText('');
Add(Application.Selection.Range,'Postal_Code');
Application.Selection.TypeText('---');
Add(Application.Selection.Range,'Country');
end;
oAutotext:=oApp.NormalTemplate.AutoTextEntries.Add('MyLabelLayout',oApp.ActiveDocument.Content);
ShowMessage(oAutotext);
oApp.ActiveDocument.Content.Delete(EmptyParam,EmptyParam);
oDoc.MailMerge.MainDocumentType := wdMailingLabels;
oDoc.MailMerge.OpenDataSource('C:\data.txt',EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
oApp.MailingLabel.CreateNewDocument
// ERROR HAPPENS OVERHERE TYPECAST ERROR!
(oLabel,EmptyParam,oAutotext,EmptyParam,EmptyParam);
//Execute the mail merge to generate the labels.
oDoc.MailMerge.Destination:=wdSendToNewDocument;
opause:=True;
oDoc.MailMerge.Execute(opause);
oAutotext.Delete;
oDoc.Close;
oApp.Visible;
oApp.NormalTemplate.Saved:=True;
end;
Avatar of ginsonic
ginsonic
Flag of Romania image

listening
ASKER CERTIFIED SOLUTION
Avatar of AvonWyss
AvonWyss
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smurff
smurff

listening
Avatar of maatwerk

ASKER

I finally got it working. I translated this from VB sourcecode see:  

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q258512

The error got caused by the fact that i indeed passed a object as a parameter. So thanks. I have noticed that this subject also interest others so here the sourcecode it work on my machine... Iam using delphi 6 and Office 2000.

procedure TForm1.Button1Click(Sender: TObject);
var
oLabel,oAutotext,oAdres,oEtiket,oTimeout,oFalse,oAutoTxt:OleVariant;
begin
oEtiket:='xxxx';
olabel:='8389';
oAdres:='Address';
oTimeout:=30;
// odoc is tworddocument oApp is twordapplication  Office 2000
with odoc.MailMerge.Fields do Begin
Add(Application.Selection.Range,'Contact_Name');
Application.Selection.TypeParagraph;
Add(Application.Selection.Range,'Address');
Application.Selection.TypeParagraph;
Add(Application.Selection.Range,'City');
Application.Selection.TypeText('');
Add(Application.Selection.Range,'Postal_Code');
Application.Selection.TypeText('---');
Add(Application.Selection.Range,'Country');
end;
oAutotext :='MyLabelLayout';
oAutoTxt:=oApp.NormalTemplate.AutoTextEntries.Add(oAutotext,oDoc.Content);
odoc.Content.Delete(EmptyParam,EmptyParam);
//Merge fields in document no longer needed now
//that the AutoText entry for the label layout
//has been added so delete it.
odoc.MailMerge.MainDocumentType := wdMailingLabels;
//Set up the mail merge type as mailing labels and use
//a tab-delimited text file as the data source.
//Create the new document for the labels using the AutoText entry
//you added -- 5160 is the label number to use for this sample.
//You can specify the label number you want to use for the output
//in the Name argument.
odoc.MailMerge.OpenDataSource('C:\data1.txt',EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
oApp.MailingLabel.CreateNewDocument(oLabel,oAdres,oAutotext,EmptyParam,EmptyParam);
//Execute the mail merge to generate the labels.
odoc.MailMerge.Destination:=wdSendToNewDocument;
oFalse:=False;
//Execute the mail merge to generate the labels.
oDoc.MailMerge.Execute(EmptyParam);
oAutoTxt.Delete;
//Close the original document and make Word visible so that
//the mail merge results are displayed
odoc.Close(oFalse);
oApp.Visible:=True;
//Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved:=True;
end;
I finally got it working. I translated this from VB sourcecode see:  

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q258512

The error got caused by the fact that i indeed passed a object as a parameter. So thanks. I have noticed that this subject also interest others so here the sourcecode it work on my machine... Iam using delphi 6 and Office 2000.

procedure TForm1.Button1Click(Sender: TObject);
var
oLabel,oAutotext,oAdres,oEtiket,oTimeout,oFalse,oAutoTxt:OleVariant;
begin
oEtiket:='xxxx';
olabel:='8389';
oAdres:='Address';
oTimeout:=30;
// odoc is tworddocument oApp is twordapplication  Office 2000
with odoc.MailMerge.Fields do Begin
Add(Application.Selection.Range,'Contact_Name');
Application.Selection.TypeParagraph;
Add(Application.Selection.Range,'Address');
Application.Selection.TypeParagraph;
Add(Application.Selection.Range,'City');
Application.Selection.TypeText('');
Add(Application.Selection.Range,'Postal_Code');
Application.Selection.TypeText('---');
Add(Application.Selection.Range,'Country');
end;
oAutotext :='MyLabelLayout';
oAutoTxt:=oApp.NormalTemplate.AutoTextEntries.Add(oAutotext,oDoc.Content);
odoc.Content.Delete(EmptyParam,EmptyParam);
//Merge fields in document no longer needed now
//that the AutoText entry for the label layout
//has been added so delete it.
odoc.MailMerge.MainDocumentType := wdMailingLabels;
//Set up the mail merge type as mailing labels and use
//a tab-delimited text file as the data source.
//Create the new document for the labels using the AutoText entry
//you added -- 5160 is the label number to use for this sample.
//You can specify the label number you want to use for the output
//in the Name argument.
odoc.MailMerge.OpenDataSource('C:\data1.txt',EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
oApp.MailingLabel.CreateNewDocument(oLabel,oAdres,oAutotext,EmptyParam,EmptyParam);
//Execute the mail merge to generate the labels.
odoc.MailMerge.Destination:=wdSendToNewDocument;
oFalse:=False;
//Execute the mail merge to generate the labels.
oDoc.MailMerge.Execute(EmptyParam);
oAutoTxt.Delete;
//Close the original document and make Word visible so that
//the mail merge results are displayed
odoc.Close(oFalse);
oApp.Visible:=True;
//Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved:=True;
end;