/    Sign up×
Community /Pin to ProfileBookmark

MS word PHP Mysql

HI

Has anyone found
a tutorial that show how to immigrate mysql data into a Microsoft word document.

I want to give users the option to open a template that will populate certain parts or a word document, I.E mailing address and forwarding address.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinAug 29.2006 — have you ever opened a word document as a plaintext file?

I onced created a VB application that would write data into an rtf document.

it did take some learning to create rtf codes, but once I knew how to create then, I could write a full document that when opened in word was perfectly formatted with different styles.

save your word doc as rtf format and open it up to see if you can find where you want your data inserted, then see what path you need to follow to get to that place.
Copy linkTweet thisAlerts:
@sitehatcheryAug 30.2006 — You can write everthing to a text file and then change the extension to .doc. Not sure if you can easily directly write to a word file without opening up the annoying splash page. What's the advantage of writing to a word file vs a text file? Curious.
Copy linkTweet thisAlerts:
@PineSolPirateAug 30.2006 — I found this site a while back when we were trying to get around mail merge and just create the docs ourselves. Not that bad really, but never figured out how to start a new page.

http://pixelated-dreams.com/pages/dynamic_word_docs.html
Copy linkTweet thisAlerts:
@AelfwineAug 31.2006 — It's possible to create an MS Word document using the PHP COM funtions. It's complicated and unfortunately I don't know of any good tutorials for the technique. Maybe the O'Reilly book 'Programming PHP', which includes a section on using a PHP generated webform to fill out a Word document, would help you?
Copy linkTweet thisAlerts:
@sitehatcheryAug 31.2006 — kproc, if you only need to populate certain parts of the word document, you can use the Word mail merge feature to set up areas that will read data from a database. Then, you only need to populate the database. You can do this through Word itself, or if you prefer, PHP. I've only used an Access database with it though.
Copy linkTweet thisAlerts:
@kprocauthorAug 31.2006 — My Goal is to offer a .doc file on my web site that users can open and certain parts of it will populate with information from mysql, I have yet to find a tutorial to show how to do this. I'm starting to think that its not possible
Copy linkTweet thisAlerts:
@AelfwineSep 01.2006 — Google comes up with quite a list of articles on using [URL=http://www.google.co.uk/search?q=MS+Word+mail+merge+MySQL+source&start=0&ie=utf-8&oe=utf-8]Mail Merge with MySQL[/URL]. If that won't do for your purposes, it IS possible to directly complete a Word document from PHP. Unfortunately, there don't seem to be any tutorials out there on doing this. Guess it's up to me. ?

Here's what we want to do.

[INDENT][list]
  • [*]Start with your framework .doc file.

  • [*]Put a bookmark in each location where you want to insert data. (Insert -> Bookmark. Name it and press Add.)

  • [*]Go to your bookmark.

  • [*]Enter your data.

  • [*]Repeat for each bookmark.

  • [*]Save a copy of the completed document

  • [*]Generate a link to the new document. (I'll leave this up to you.)

  • [/list]
    [/INDENT]

    Now the process:

  • 1. Create your .doc file and insert the bookmarks.


  • 2. Create a Word macro to find each bookmark.
    [CODE]Sub BkmkNumber1()
    [INDENT]Selection.GoTo What:=wdGoToBookmark, Name:="number1"[/INDENT]
    End Sub[/CODE]

    The name of the bookmark here is 'number1'. You need a new macro for each bookmark.


  • 3. The PHP script.
    [CODE]
    <?php
    // The skeletal Word doc with macros
    $userfile="C:/temp/userfile.doc";
    // This script modifies the original but I suspect you'll want to add code
    // to make a copy, give it a unique id and modify that instead.

    // Fake parameters for inserting - you'll draw these from MySQL
    $first="Fake data 1"
    $second="Fake data 2"
    $third="Fake data 3"

    // Start Word
    $word=new COM("Word.Application") or die("Cannot start MS Word");
    // Choose whether you want Word visible or not
    $word->visible = 1 ;
    $word->Documents->Open($userfile);

    // Fill in fields
    $word->Application->Run("BkmkNumber1");
    $word->Selection->TypeText($first);
    $word->Application->Run("BkmkNumber2");
    $word->Selection->TypeText($second);
    $word->Application->Run("BkmkNumber3");
    $word->Selection->TypeText($third);

    // Save the modified document
    $word->Application->ActiveDocument->Saved=True;

    // Close the application and release the COM object
    $word->Quit();
    $word->Release();
    $word = null;
    ?>
    [/CODE]


    Because of the many vesions of word, and PHP's evolving COM support, this example may or may not work for you. If it doesn't, you can research Word objects in the MSDN library, and look at the code of VBScript macros to expose OLE objects and parameters to be passed with the [URL=http://www.php.net/manual/en/ref.com.php]PHP COM functions[/URL] .
  • Copy linkTweet thisAlerts:
    @kprocauthorSep 07.2006 — HI.

    I'm giving this a try and I get

    fatal error: uncaught exception 'com_exception' with message 'unable to lookup Document Unknown name.

    The file that I'm trying to open is letter.doc and its saved in a folder called formtemplate.

    if I type the address in firefox it opens up asking to download. is there anything special I need to do to the file name

    I have the variable set like this

    do I need to put the dive is c:/ I hope not my goal is to store the files in a folder on my webserver

    $userfile = "formtemplates/letter.doc";
    Copy linkTweet thisAlerts:
    @kprocauthorSep 07.2006 — Hi

    below is the code taht is giving me the error message. I posted the complete error message this time.

    I believe it has to do with my file loaction but, Its saved in a folder on on the service.

    I tried setting the file address as c: etc

    but get the same error


    error

    Fatal error: Uncaught exception 'com_exception' with message 'Unable to lookup `Document': Unknown name. ' in C:MyServerxampphtdocsfamilyclickWordTest.php:8 Stack trace: #0 C:MyServerxampphtdocsfamilyclickWordTest.php(8): unknown() #1 {main} thrown in C:MyServerxampphtdocsfamilyclickWordTest.php on line 8

    <?PHP

    [code=php]
    $word=new COM("word.application") or die ("cannot start MS Word");
    $userfile = "familyclickformtemplatesletter.doc";

    $word->visible=1;
    $word->Document->open($userfile);

    $word->Application->Run("bkfirstname");
    $word->selection->TypeText($data1);

    $word->Application->Run("bklastname");
    $word->selection->TypeText($data2);

    $word->Application->Run("bkcity");
    $word->selection->TypeText($data3);

    $word->Application->Run("street_address");
    $word->selection->TypeText($data4);

    $word->Application->Run("other_address");
    $word->selection->TypeText($data4);

    $word->Application->Run("postalcode");
    $word->selection->TypeText($data5);

    ?>

    [code=php]
    ×

    Success!

    Help @kproc spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...