In this post, I'm going to explain how to create a mail merge report and send it to customer using word automation.
Pre-Requisites:
steps to Create a mail merge document template:
Pre-Requisites:
- Word Automation should be installed.
- Word Application, Document and Range variables should be used.
- Preferably, a separate code unit should be used for word automation.
- Mail merge template (Preferably in .DOT extension).
- SMTP mail setup should be used for mailing.
- Suitable action item from the respective card page should be used to call this code unit from current record.
- In case of server implementation, use a shared folder to save the mapped word document and attach it in the mail.
steps to Create a mail merge document template:
- Open word application.
Go to Insert tab =>quick parts => Merge field => select mail merge.T - ype the name of the merge field and press OK.
- Repeat the steps to create multiple merge fields as per your requirement.
- Type the letter content as per your requirement.
- Save the document as <DOCNAME>.DOT
Where to Place Automation Code:
You put all code in a separate code unit that is called from a menu item on the page.
It is recommended that you isolate the code that uses Automation in separate codeunits since Automation server must be installed on the computer, which compiles an object that uses Automation. You need to recompile and modify the entire object if the Automation server is not installed on your computer.It is best that the Automation code be placed in a separate codeunit than be defined as a function in any other codeunit.
Performance can be an issue if extra work is needed to create an Automation server with the CREATE system call. If the Automation server is to be used repeatedly, then you will gain better performance by designing your code so that the server is created only once instead of making multiple CREATE and CLEAR calls.
Variables
Name DataType Subtype Length
WdApp Automation 'Microsoft Word 14.0 Object Library'.Application
WdDoc Automation 'Microsoft Word 14.0 Object Library'.Document
WdRange Automation 'Microsoft Word 14.0 Object Library'.Range
CompInfo Record Company Information
TemplateName Text 250
FileName Text 250
SMTPMail Codeunit SMTP Mail
SMTPSetup Record SMTP Mail Setup
Cust Record Customer
Mail Codeunit Mail
Sales&RecvSetup Record Sales & Receivables Setup
WdApp Automation 'Microsoft Word 14.0 Object Library'.Application
WdDoc Automation 'Microsoft Word 14.0 Object Library'.Document
WdRange Automation 'Microsoft Word 14.0 Object Library'.Range
CompInfo Record Company Information
TemplateName Text 250
FileName Text 250
SMTPMail Codeunit SMTP Mail
SMTPSetup Record SMTP Mail Setup
Cust Record Customer
Mail Codeunit Mail
Sales&RecvSetup Record Sales & Receivables Setup
SalesHeader Record Sales Header
Before starting,go to the codeunit's properties and set the codeunit's table No. to the associated page that you need to send mail from.
Sample Code :
//Word Automation Begin
IF CREATE(WdApp,TRUE,TRUE) THEN BEGIN // Initialize word application
FileName := FORMAT("Sales&RecvSetup"."Server Destination")+FORMAT(Description)+'TenderDocument.Doc';
"Template Name" := "Sales&RecvSetup"."Template Location";
WdDoc := WdApp.Documents.Add("Template Name"); // this enables word application to use the template
WdApp.ActiveDocument.Fields.Update; // Initialize the document
WdRange := WdApp.ActiveDocument.Fields.Item(1).Result; // This sets the range to the particular merge field using array Index
WdRange.Text := "Bill-to Name"; // returns the customer name from the current record.
WdRange.Bold := 1;
WdApp.Visible := TRUE;
WdApp.ActiveDocument.Fields.Unlink; // this will map the table records to the word document
WdApp.ActiveDocument.SaveAs(FileName);
WdApp.Quit;
CLEAR(WdApp);
//Word Automation End
IF CREATE(WdApp,TRUE,TRUE) THEN BEGIN // Initialize word application
FileName := FORMAT("Sales&RecvSetup"."Server Destination")+FORMAT(Description)+'TenderDocument.Doc';
"Template Name" := "Sales&RecvSetup"."Template Location";
WdDoc := WdApp.Documents.Add("Template Name"); // this enables word application to use the template
WdApp.ActiveDocument.Fields.Update; // Initialize the document
WdRange := WdApp.ActiveDocument.Fields.Item(1).Result; // This sets the range to the particular merge field using array Index
WdRange.Text := "Bill-to Name"; // returns the customer name from the current record.
WdRange.Bold := 1;
WdApp.Visible := TRUE;
WdApp.ActiveDocument.Fields.Unlink; // this will map the table records to the word document
WdApp.ActiveDocument.SaveAs(FileName);
WdApp.Quit;
CLEAR(WdApp);
//Word Automation End
//SMTP Mail Begin
SMTPSetup.GET;
IF Cust.GET("Bill-to Customer No.") THEN BEGIN
Cust.TESTFIELD(Cust."E-Mail");
SMTPMail.CreateMessage(COMPANYNAME,CompInfo."E-Mail",Cust."E-Mail",'Sales Invoice','',TRUE);
SMTPMail.AppendBody('Dear Sir / Madam,');
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody('Please Find the attached Sales Invoice.');
SMTPMail.AppendBody('<br><br>');
SMTPSetup.GET;
IF Cust.GET("Bill-to Customer No.") THEN BEGIN
Cust.TESTFIELD(Cust."E-Mail");
SMTPMail.CreateMessage(COMPANYNAME,CompInfo."E-Mail",Cust."E-Mail",'Sales Invoice','',TRUE);
SMTPMail.AppendBody('Dear Sir / Madam,');
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody('Please Find the attached Sales Invoice.');
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody('Thanks for doing business with us.');
SMTPMail.AppendBody('Regards,');
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody(CompInfo.Name);
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody('<HR>');
SMTPMail.AppendBody('******This is a system generated mail. Please do not reply to this email ID.******');
SLEEP(4000);
SMTPMail.AddAttachment(FileName);
SLEEP(4000);
SMTPMail.Send;
MESSAGE(Text0001);
END;
CLEAR(WdApp);
END;
SMTPMail.AppendBody('Regards,');
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody(CompInfo.Name);
SMTPMail.AppendBody('<br><br>');
SMTPMail.AppendBody('<HR>');
SMTPMail.AppendBody('******This is a system generated mail. Please do not reply to this email ID.******');
SLEEP(4000);
SMTPMail.AddAttachment(FileName);
SLEEP(4000);
SMTPMail.Send;
MESSAGE(Text0001);
END;
CLEAR(WdApp);
END;
//SMTP Mail End
**Note:
1. Use sleep function before and after calling add attachment function because it takes few seconds for the system to save your document and then attach the same. use CLEAR system calls if you plan to use this function multiple times.
2. Use a shared folder from the server in case if you are planning to implement this over a server. since the add attachment will look for the file from the server, it will throw an error that the file is missing or not available. if you are using this from your local machine's database, then it wouldn't be a problem.
Finishing Touch:
You can attach this code unit to an appropriate card page in a menu item from which the mail needs to be sent. By calling the codeunit, you'll have to pass the current record REC as a parameter.
Note:
A much simpler way to use MS-Word for reports is available in NAV 2015. This process is the primitive method to connect to word application from NAV using Automation variables.
Please leave your queries in the comment section.
No comments:
Post a Comment