Passing parameters from grid to ms word document (.docx)

Hello,
I need to pass parameters from a grid (some fields) to Microsoft Word document (.docx), inside this document i have a table with some fields that i replaced with variables, i tried to make a blank application with some code but no success, any ideas?

I found the following code here in this forum, thank you @steventrs

$filelocation1 = ‘file1.rtf’;
$firstname = [firstname];
$lastname = [lastname];

$output = file_get_contents($filelocation1);
$output = str_replace(’[%FIRSTNAME%]’, $firstname, $output);
$output = str_replace(’[%LASTNAME%]’, $firstname, $output);

I created a blank application and passed parameters from grid to the blank application and changed my file from .docx to .rtf
I deployed my applications and put the .rtf file in the same directory of blank application.

UNFORTUNATELY NO SUCCESS :frowning:

Hi,

​You try to echo your rtf file and check the variable that use in the rtf. I think it is not [%FIRSTNAME%], there might be some characters in between. I also experience this before which I am not sure why.

I tried use firstname1 in the rtf.
​[LEFT][SIZE=13px]$output = str_replace(‘firstname1’, $firstname, $output);

​If cannot I will copy the text and re-paste as “Keep Text Only” in the rtf.[/SIZE][/LEFT]

Hi, thank you so much again, I tried many ways still no luck. Please help as this is very important.

My client has some images inside the document, here .rtf will not work. I need to pass parameters to ms word. Any suggestions?

There is another problem, when i put the docx file in this folder: C:\Program Files\NetMake\v9\wwwroot\scriptcase\app\abusin\blank
and run the application the file deleted immediately.

Create a blank application and put the rtf in the same folder as the blank application folder.
The blank application contains the following code. It will prompt to download the rtf file which the variable in the rtf will be already replaced with the value.

[LEFT][SIZE=13px]Hope it helps.[/SIZE][/LEFT]

$filelocation1 = ‘file1.rtf’;
$firstname = [firstname];
$lastname = [lastname];
$filename=‘myfilename.rtf’;

$output = file_get_contents($filelocation1);
$output = str_replace(‘firstname1’, $firstname, $output);
$output = str_replace(‘lastname1’, $firstname, $output);
header(‘Content-Type: application/octet-stream’);
header(‘Content-Disposition: attachment; filename="’.$filename.’"’);
header(‘Expires: 0’);
header(‘Cache-Control: must-revalidate’);
header(‘Pragma: public’);

echo $output;

Dear @steventrs
Thank you so much for your help, i really appreciate it.
In the RTF file i put the following:

First Name: [firstname].
Last Name: [lastname].

When i run the application it prompt to download the rtf file, but opening it get me the above code and the variables still there and not replaced, maybe i did something wrong but i am sure that i copied your code and just replace the rtf file name.

I am wondering if the problem in the $filelocation1 = ‘file1.rtf’ here i change the location with the RTF file name without “/” or “” i do not know if this correct.