Solved

Invalid typecase error while parsing autotext.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 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