?
Solved

Invalid typecase error while parsing autotext.

Posted on 2002-05-08
5
Medium Priority
?
371 Views
Last Modified: 2007-11-27
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;
0
Comment
Question by:maatwerk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 6998151
listening
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 2000 total points
ID: 6998241
MSDN says:
AutoText:  Optional Variant. The name of the AutoText entry that includes the mailing label text.

You seem to supply an object, not a name. Therefore, using the name of your AutoText entry instead of the object should work just fine:

oApp.MailingLabel.CreateNewDocument(oLabel,EmptyParam,'MyLabelLayout',EmptyParam,EmptyParam);
0
 
LVL 3

Expert Comment

by:smurff
ID: 6998406
listening
0
 

Author Comment

by:maatwerk
ID: 6999150
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;
0
 

Author Comment

by:maatwerk
ID: 6999231
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;
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question