I was testing my limited skills and ran across the fwrite example on php.net. I was able to output the test code and rcvd the msg that it wrote to the Word doc, but there's no text. The same code worked fine for .txt files and the text was actually there. Why doesn't MS Word take the updates? I even ran across this thread.

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
     echo "Cannot open file ($filename)";
     exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
    echo "Cannot write to file ($filename)";
    exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?> 

    The problem is that MS Word files are not flat text files; they are in a proprietary format of their own. You could write the text into a rich text format (.rtf) file, or if using the latest version of Office the new XML format supported by Word, but you still need to know the protocol for either of those formats (just like you do to create HTML or XML files).

      I have a program that exports to Word and it works great. There are headers/footers on the original report and I simply wanted to create a template with the headers/footers and update the document template with the data from my program. I tried RTF and XML, but neither of those worked. I guess I'll just search around to see if I can find out how the different protocols (as you mentioned) work. If there's a simpler way to do this, I'd love to hear it! I've searched all over looking for this information and cannot find it. Back to the drawing board! (unless someone can pick up the chalk and write out the solution) šŸ˜‰

        7 days later
        NogDog;10880771 wrote:

        The problem is that MS Word files are not flat text files; they are in a proprietary format of their own. You could write the text into a rich text format (.rtf) file, or if using the latest version of Office the new XML format supported by Word, but you still need to know the protocol for either of those formats (just like you do to create HTML or XML files).

        Some of you more seasoned PHP/MySQL veterans may laugh at this, but I thought I’d share since it worked for me. 😃

        I have a PHP/MySQL program that allows users to enter data and then export the information to a Word file. I finally figured out how to export all the data to a Word file where I can control the headers, footers, Table of Contents, page numbers, etc.!

        Word XML has too many variables and I’m sure some of you are familiar with all the garbled XML that’s mind-boggling to look at. So, I created an RTF (thanks to NogDog mentioning trying RTF) template file called RTF.rtf with the header, footer, Table of Contents, page numbers, etc. RTF doesn’t save macros, but that’s not a big deal. Maybe I’ll find something out later.

        I have a cover page, TOC, then section headers with Word’s Heading 1 (and Heading 2 where needed) set for each section. I modified them to look exactly like I wanted (colors, borders, etc.) After creating the sections, I updated my TOC. Under each section, I simply used Word’s INCLUDETEXT function:

        { INCLUDETEXT "http://websiteaddress.com/file_for_this_section.php?id=IDIDIDIDID" \c HTML }

        The IDIDIDIDID is just a generic name I gave as a placeholder. Use whatever you want. I saved the RTF.rtf file to the same folder as my code and did the following:

        $filename = "./RTF.rtf/"; // ./ for the current directory, in case some were wondering
        $handle = fopen($filename, "r+"); // r+ = Open for reading and writing; place the file pointer at the beginning of the file. (ala php.net)
        $contents = fread($handle, filesize($filename)); // it was in the example, but I don’t even know if I need this line
        $edit = preg_replace('/IDIDIDIDID/', $_GET['id'], $contents); // read the entire RTF.rtf file and replace the IDIDIDIDID with the id number you are trying to call up
        

        You cannot (at least to my limited knowledge… and I got sick of looking for how to do this) perform a ā€œfind and replaceā€ (i.e. preg_replace) on the XML file because of the Word binData (UUENCODED/Binary) information generated with each change of the XML file. You simply break the document and get error codes on the lines with the binData information.

        You’ll see references to $row_reports, but that was for calling up the information to get the report title, etc. After the connection script and getting the report information, I used this…

        header('Content-Type:application/msword');
        header('Content-Disposition: attachment; filename="'.$row_reports['title'].'.doc"'); // in order to place the actual report title from the database into the filename.doc that you’ll see when you click the link and get the Open/Save/Cancel box.  This code may be off since I removed the str_replace info I had in here for my own use, but you get the idea.
        header("Pragma: no-cache"); 
        header("Expires: 0"); 

        Then I pasted the newly edited code that contains the actual id number vs. the IDIDIDIDID.

        echo $edit;

        I click the link for ā€œprint.php?id=25ā€ or whatever id it is and I get the prompt to Open/Save/Cancel my Word document. All the formatting is there and I simply update the TOC.

        Just FYI…
        I haven’t run into the problem with exporting the Word doc, but when I was doing earlier testing before generating the Word doc, I sometimes had to clear my cache.

        I hope this works for you like it's working for me and I'd LOVE to hear how to improve this, cuz I'm sure it could be done. šŸ˜‰

          Write a Reply...