Does anyone have some sample code they are willing to share showing how to perform a Label Mail Merge with Word Automation and, say, client addresses?
This was an example distributed by Navision in 1999 called “discount letter”. At that time I sent it to text and have imported it to start a few projects since. Communication between Navision and Word “Fields” is dog slow. I got excited about using this method to create a quote and it took about 2 minutes on a fast box to create the document…oh well. OBJECT Codeunit 70000 DiscountLetter { OBJECT-PROPERTIES { Date=11/11/98; Time=10:07:56 PM; Version List=; } PROPERTIES { TableNo=18; OnRun=BEGIN CALCFIELDS(“Sales ($)”); IF (“Sales ($)” < 2500) THEN BEGIN MESSAGE(’%1 does not meet the criteria for a discount.\Sales (LCY) are not > 2500.’,Name); EXIT; END; CompanyInfo.FIND; UserInfo.GET(USERID); CREATE(wdApp); TemplateName := ‘C:\TMP\Discount.dot’; wdDoc := wdApp.Documents.AddOld(TemplateName); wdApp.ActiveDocument.Fields.Update; wdRange := wdApp.ActiveDocument.Fields.Item(1).Result; wdRange.Text := Name; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(2).Result; wdRange.Text := Contact; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(3).Result; wdRange.Text := Address; wdRange.Bold := 0; //added by cb wdRange := wdApp.ActiveDocument.Fields.Item(4).Result; wdRange.Text := “Address 2”; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(5).Result; wdRange.Text := City; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(6).Result; wdRange.Text := State; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(7).Result; wdRange.Text := “ZIP Code”; wdRange.Bold := 0; // wdRange := wdApp.ActiveDocument.Fields.Item(8).Result; wdRange.Text := FORMAT(WORKDATE); wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(9).Result; wdRange.Text := Contact; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(10).Result; wdRange.Text := Name; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(11).Result; wdRange.Text := FORMAT(“Sales ($)”,0,’<Decimals,3>’); wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(12).Result; wdRange.Text := FORMAT(ROUND(“Sales ($)” * 0.03),0,’<Decimals,3>’); wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(13).Result; wdRange.Text := UserInfo.Name; wdRange.Bold := 0; wdRange := wdApp.ActiveDocument.Fields.Item(14).Result; wdRange.Text := CompanyInfo.Name; wdRange.Bold := 0; wdApp.Visible := TRUE; wdApp.ActiveDocument.Fields.Unlink; END; } CODE { VAR wdApp : Automation “{00020905-0000-0000-C000-000000000046} 8.0:{000209FF-0000-0000-C000-000000000046}:Unknown Automation Server.Application”; wdDoc : Automation “{00020905-0000-0000-C000-000000000046} 8.0:{00020906-0000-0000-C000-000000000046}:Unknown Automation Server.Document”; wdRange : Automation “{00020905-0000-0000-C000-000000000046} 8.0:{0002095E-0000-0000-C000-000000000046}:Unknown Automation Server.Range”; TemplateName : Text[250]; CompanyInfo : Record 79; UserInfo : Record 2000000002; BEGIN END. } }
Thanks Mark, To speed the process up I used a text file rather than populating the data directly into the word document. Example code is shown below in case anyone has to do this in the future, as it’s a bit fiddly. // CODE //MailMerge - Performs a label mail merge from a set of customers // Setup Constants cFalse := FALSE; cTrue := TRUE; cZero := 0; cCarriageReturn := 13; wdCell := 12; wdCollapseStart := 1; wdStory := 6; wdExtend := 1; // Display Progress Window ProgText := ‘Extracting Customer Details’; win.OPEN (’#1#####################################’,ProgText); // Get Temporary Area TempDir := ENVIRON(‘TEMP’); FileName := TempDir + ‘labels.txt’; // Create Text File OutputFile.WRITEMODE(TRUE); OutputFile.TEXTMODE(TRUE); OutputFile.CREATE(FileName); // Create File Header OutputLine := ‘Name,Address,Address2,Address3,Address4,PostCode’; OutputFile.WRITE(OutputLine); // Loop through Customers and add to text file Cust.RESET; Cust.SETCURRENTKEY(“Team Code”); Cust.SETFILTER(“Team Code”,‘T022’); IF (Cust.FIND(’-’) = TRUE) THEN REPEAT OutputLine := ‘"’ + Cust.Name + ‘","’ + Cust.Address + ‘","’ + Cust.“Address 2” + ‘","’ + Cust.“Address 3” + ‘","’ + Cust.City + ‘","’ + Cust.“Post Code” + ‘"’; OutputFile.WRITE(OutputLine); UNTIL (Cust.NEXT = 0); // Close File OutputFile.CLOSE; ProgText := ‘Creating Word Label Document’; win.UPDATE; // Create Blank Word Document for labels CREATE(MSWord); MSWord.Visible(TRUE); WordDoc := MSWord.Documents.Add(); // Create document with default template Merge := WordDoc.MailMerge; Merge.MainDocumentType := 1; // wdMailingLabels Merge.OpenDataSource (FileName); // Link to customer file ProgText := ‘Setting up Label Format’; win.UPDATE; // Type of Labels LabelType := ‘L7160’; // Avery A4 Lables (7 x 3 = 21 labels) MSWord.MailingLabel.CreateNewDocument (LabelType); // Insert Merge fields into first label Selectn := MSWord.Selection; FOR Lp := 1 TO 6 DO BEGIN CASE Lp OF 1 : aString := ‘Name’; 2 : aString := ‘Address’; 3 : aString := ‘Address2’; 4 : aString := ‘Address3’; 5 : aString := ‘Address4’; 6 : aString := ‘PostCode’; END; aRange := Selectn.Range; Merge.Fields.Add(aRange,aString); IF (Lp <> 6) THEN Selectn.TypeText(FORMAT(cCarriageReturn)); END; // Select all of the first cell and copy Selectn.HomeKey(wdStory,wdExtend); // Unit:=wdStory, Extend:=wdExtend Selectn.Copy; // Paste into each of the remaining 20 cells FOR Lp := 2 TO 21 DO BEGIN Selectn.MoveRight(wdCell); // Move to next cell IF ((Lp-1) MOD 3 <> 0) THEN // Move to next cell where there is a buffer cell Selectn.MoveRight(wdCell); Selectn.Paste; // Paste fields Selectn.Collapse(wdCollapseStart); // Put cursor at start of selection aRange := Selectn.Range; Merge.Fields.AddNext(aRange); // Add next record field END; // VARIABLES Name DataType Subtype Length aRange Automation ‘Microsoft Word 10.0 Object Library’.Range aString Text 30 cCarriageReturn Char cFalse Boolean cTrue Boolean cZero Integer Cust Record Customer FileName Text 250 LabelType Text 30 Lp Integer MailLabel Automation ‘Microsoft Word 10.0 Object Library’.MailingLabel Merge Automation ‘Microsoft Word 10.0 Object Library’.MailMerge MSWord Automation ‘Microsoft Word 10.0 Object Library’.Application OutputFile File OutputLine Text 250 ProgText Text 80 Selectn Automation ‘Microsoft Word 10.0 Object Library’.Selection TempDir Text 250 win Dialog WordDoc Automation ‘Microsoft Word 10.0 Object Library’.Document wdCell Integer wdCollapseStart Integer wdStory Integer wdExtend Integer