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 Create multiple documents at once using MailMerge|Documentation and Mail Merge and Reporting in C#|Aspose.Words for .NET for detailed information on executing templates.

You can also download our PHP SDK from GitHub - aspose-words-cloud/aspose-words-cloud-php: A PHP library for communicating with the Aspose.Words Cloud API 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

$strURI = '
[http://api.saaspose.com/v1.0/words/'.$fileName.'/executeTemplate&#39](http://api.saaspose.com/v1.0/words/'.%24fileName.'/executeTemplate&#39);;
$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.

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.

  1. 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.

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.

  1. 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 Create multiple documents at once using MailMerge|Documentation.

Following is the XML structure for this method.

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 https://github.com/aspose-words-cloud/aspose-words-cloud-php/blob/master/tests/Aspose/Words/ExecuteMailMergeTests.php 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: Create multiple documents at once using MailMerge|Documentation