Solved

Invalid typecase error while parsing autotext.

Posted on 2002-05-08
5
361 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
5 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 6998151
listening
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now