Solved

Invalid typecase error while parsing autotext.

Posted on 2002-05-08
5
363 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 133
Delphi Mdi application Child forms get behind control 7 212
Reconfigure Delphi Install? 2 51
how can i search if string exist in array ? 3 58
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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