Input On Best Way to Create Word Doc on Server RRS feed

  • Question

  • I am looking to see if anyone has done something similar to this and any direction they can provide.


    I have a Word addin that manages the creation of numerous documents (quotes, schedules, work orders, reports ..etc).  The user enters data into forms in Word, it is saved into a database and docs can be produced from the info.  There is interest in having this run on Macs and tablets.  Obviously VBA does not run on Macs or tablets.  However, Macs and tablets can view Word documents.  I have recreated one of the Word forms (tabbed panel with about 9 forms) using HTML, CSS3, Javascript, AJAX and PHP.  It seems to work great.  I am up to the part of creating Word documents.  I plan to have the web form send a POST to the server specifying what document to create.  Then there appears to be 2 ways to do create doc:

    1. Have PHP kick off Word with VBA addin with instructions on what to create.  Addin would read db and fill in Word template and save file on server.  PHP would pass back file location.  Advantages is I know VBA and have most of the code.

    2. Use C# and Microsoft.Office.Interop.Word and have it read db and fill in Word template.  I have some experience with C# and none with Microsoft.Office.Interop.Word.

    Does anyone have experience with this?
    Wednesday, November 20, 2013 5:01 PM

All replies

  • Just to let you know I created a VBA addin that creates the requested Word doc.  The webpage Javascript submits an AJAX POST request that contains info about what doc to create.  PHP receives the request and creates a job text file with the info and starts word using a shell command.  I did have a weird problem that Word would open the file and lock up.  I got around this by copying the file and having Word open that file. 

    The addin uses AutoOpen, reads the file and performs the requested cmd.  Most of the times a .docx and pdf are created.  PHP passes back a JSON object that has the info on which doc was created.  The Javascript reads the JSON object and updates the appropriate links.  Since this is async and some docs may take longer than others you need to determine which request is which.  I tried this on my Android tablet and it works.  This means that devices that can't run Word VBA can still get Word docs created using VBA.

    To help debug the addin I create a menu if the addin is started normally.  This way I can create the same command and doc using a form.  Really helped with debug and users with VBA can use the addin directly.

    PHP Code:

            $t = time();
            $job = 'C:\temp\Job' . $t . ".txt";
            $fp = fopen($job, 'w');
            fwrite($fp, $cmd);
            $job2 = 'C:\temp\Job' . $t . "a.txt";  // copy gets around some strange problem with Word opening file.
            copy($job, $job2) or die("Unable to copy $old to $new.");
            $runWord = '"C:\Program Files\Microsoft Office\Office14\Winword.exe" "' . $job2 . '" "C:\userdata\tripq\trip-web\trip-web.dotm"';
            $WshShell1 = new COM("WScript.Shell");
            $oExec1 = $WshShell1->Run($runWord, 7, true);  //opens Word minimized.

    • Edited by mogulman52 Monday, November 25, 2013 10:31 PM
    Monday, November 25, 2013 10:29 PM