![]() Join Up! 96814 members and counting! |
|
|||
Creating Word Documents on the Fly
Khairul Amri Yunus
Introduction
Printing HTML pages directly from browsers usually does not give the best printing
output, compared to output from word processors such as Microsoft Word. For
web-based systems that generate letters, reports and documents, an output that
looks authentic and original is very important. Furthermore, printing output
directly from browsers is usually dependant on the browser's configuration. Therefore
web-based applications may face difficulties in giving the best printing output
especially when dealing with large user base.
This article provides a guideline on how to generate documents and letters in
MS Word format using a pre-defined template. My customer (a government agency)
required for Reminder Letters to be generated automatically in MS Word format
from the Loan Management System that I delivered to them. Using this approach,
they can generate a Reminder Letter for each loan which then can be printed
nicely and saved for future reference.
This solution can be accomplished using COM extension. Please note that the
server running this code MUST have MS Word installed.
Overview
Lets start with the big picture. This is how it works:
Preparing the MS Word Template
This step is actually simple, all we need is a MS Word document (*.doc) that
contains the sample output and Bookmarks.
To create a Bookmark in a MS Word document, click on Insert/Bookmark. You can
highlight a word or sentence and make them a Bookmark. In this code, we will
substitute the Bookmark with the actual value, therefore the word you highlighted
and saved as a Bookmark will be substituted.
A Bookmark name can only contain letters, numbers and underscores. To see all of
bookmarks in the document, click on Tools/Options and check Bookmarks under Show
group in the View tab.
The code we use in the next section just substitutes a specified bookmark with text,
therefore the formatting, images, tables and etc will not be lost.
Substituting with the actual value
The following code opens the specified MS Word template document, substitutes
the value and saves it as a new file. We defined a Bookmark named "TODAYDATE"
in the document, which will be replaced with today's date.
That's it. Open the new document (c:/reminder_new.doc) and you will see today's
date at the bookmark's location.
Improving the code
The above code will return an error if the specified bookmark is not found in
the document. To check whether the bookmark exists in the template document,
use this:
The approach above replaces Bookmarks with the actual value, therefore the word
highlighted and defined as the bookmark will be deleted. If you want to insert
text before the bookmark, use the following:
Alternatively, to insert text after the bookmark, use this:
To select all of the text within the template document, use the following:
To automatically load the document, use header functions (add this to the end of
the PHP file):
Alternatively, to prompt Open/Save dialog box, use:
Note that the above two examples may not work on certain browsers and/or versions.
You may want to terminate/kill WINWORD.EXE instance if the code triggers an
error or terminates unexpectedly. Run Windows Task Manager and end WINWORD.EXE
instance.
That's it. Feel free to share your thoughts and improved versions of the above
code.
Khairul Amri Yunus
Kometsoft (M) Sdn. Bhd. |