Free Support Forum - aspose.com

How to execute mail merge template using Saaspose.Words with PHP

Hi! I have a Docx document with the following variable “nome_clie​nte” and I want to use the executeTem​plate in this Docx. I’m using PHP and I don’t know how the JSON or de XML will be formated. Thanks!


Submitted from: http://saaspose.com/

Hi,

Please have a look at http://saaspose.com/docs/display/words/Execute+Mail+Merge+Template+%28PHP+REST%29 and http://saaspose.com/docs/display/words/SaaSpose.Words+Templates+1.0+Features for detailed information on executing templates.

You can also download our PHP SDK from https://github.com/saaspose/Saaspose.SDK-for-PHP and use ExecuteTemplate method from MailMerge.php.

Please feel free to contact us in case you have further comments or questions.

Best Regards,
Muhammad Ijaz
Support Developer, Saaspose Sialkot Team
http://www.saaspose.com

SaasposeApp::$OutPutLocation = $outPutLocation;
$strURI = '
http://api.saaspose.com/v1.0/words/'.$fileName.'/executeTemplate';
$signedURI = Utils::Sign($strURI);

$xml = simplexml_load_file("/home/projects/tests/xml.xml");
$responseStream = Utils::processCommand($signedURI, "POST", "",
$xml);
debug($responseStream);

$jsonResponseStream = json_decode($responseStream);
$strURI = 'http://api.saaspose.com/v1.0/storage/file/' .
$jsonResponseStream->Document->FileName;

$signedURI = Utils::Sign($strURI);
$responseStream = Utils::processCommand($signedURI, "GET", "", "");

$outputFile = SaasposeApp::$OutPutLocation .
$jsonResponseStream->Document->FileName;
Utils::saveFile($responseStream, $outputFile);

That's is the code!

Hi,

You can use simple mail merge for your case. I have used ExecuteMailMerege SDK method from MailMerge.php in the following example:

SaasposeApp::$OutPutLocation = $outPutLocation;

$mainDocumentFile = "C:\\manha-masculino-online.docx";

$mainDocument = basename($mainDocumentFile);
$xml = simplexml_load_file("c:\\xml.xml");

//upload main document
echo "Uploading main document...
";
$folder = new Folder();
$folder->UploadFile($mainDocumentFile, "");
echo "Main document uploaded
";

echo "Executing mail merge...
";
//create WordMailMerge object
$doc = new WordMailMerge();

$result = $doc->ExecuteMailMerege($mainDocument, $xml->asXML());

PHP REST example is not available yet but we are adding it and let you know once it is added in the documentation.

Best Regards,
Muhammad Ijaz
Support Developer, Saaspose Sialkot Team
http://www.saaspose.com

Hi Rafael,

After going through your last chat session at live support, I found you are still facing some issues with execute mail merge/template. There are three methods in MailMerge.php file of our PHP SDK and following are the details about each method:

1. ExecuteMailMerege

If you have simple merge fields (e.g. «field1» and «field2» etc.) in your Word template and there are no "«TableStart:TableName»«TableEnd:TableName»" type merge fields and no " " type structures in your Word document then you can use this method to execute simple mail merge without regions and following is the XML structure for this type of mail merge.

<?xml version="1.0" encoding="utf-8" ?>

Value1
Value2

You can select any text for CollectionName in your XML but field names must match the names of the merge fields in your Word document.

2. ExecuteMailMeregewithRegions

If your merge fields are within "«TableStart:TableName»«TableEnd:TableName»" region, then you may choose this method to execute mail merge with regions. Following XML can be used for this type of mail merge.

<?xml version="1.0" encoding="utf-8"?>
http://www.w3.org/2001/XMLSchema-instance">

Value1
Value2


Value1
Value2

You can select any text for CollectionName in your XML but ItemName must match TableStart and TableEnd region name (e.g. «TableStart:ItemName»«TableEnd:ItemName» in this case) and field names must match the names of the merge fields in your Word document.

3. ExecuteTemplate

If you have simple fields like "" and "" in your Word document and all those fields are within " " block then you can use this method to execute template. Please note that this method will not work with merge fields (e.g. «field1») and it will only work with simple fields (e.g. ) as you can see at http://saaspose.com/docs/display/words/SaaSpose.Words+Templates+1.0+Features.

Following is the XML structure for this method.

<?xml version="1.0" encoding="utf-8"?>
http://www.w3.org/2001/XMLSchema-instance">

Value1
Value2


Value1
Value2

You can select any text for CollectionName in your XML but ItemName must match " " block name and field names must match the names of the fields in your Word document. You can also use foreach blocks within another foreach block; please check http://saaspose.com/docs/display/words/Execute+Mail+Merge+Template+%28PHP+REST%29 for a more detailed example.

I had tested "" at first page and "" at 10th or 20th page but did not find any issue in executing template. If you still find any issue in this regard, please share your sample document and XML for further analysis.

Best Regards,
Muhammad Ijaz
Support Developer, Saaspose Sialkot Team
http://www.saaspose.com

Hi, buddy!

Thanks for helping me! This explanation was excellent! I recommend you to
append this to your mailmerge documentation.

Att,

Hi Rafael,

Thanks a lot for your feedback. We will definitely move these guidelines to live docs.

Best Regards,
Muhammad Ijaz
Support Developer, Saaspose Sialkot Team
http://www.saaspose.com

Note for reference only: This issue was resolved - Ticket # 34. Mail Merge methods information has been shared in the following documentation: http://www.aspose.com/docs/display/wordscloud/executeMailMerge+vs+executeTemplate