COM and ASP used to create dynamic Word document

Author:Anonymous    Updated:2008-2-26 22:10:04
Our application procedures, a sample forms, visit web site by the user filled out. Once submitted, ASP document of the information collected, stored on the web server using pre-defined templates, and its contents to create a Word document. Then display a link that allows users to view or download the document.

We create a Visual Basic COM components (ActiveX DLL), through ASP applications call it, pass it to the necessary parameters. Components from the essence is to use the Microsoft Word object to create one pairs will be to convey the parameters of Word documents are cited. All of these are done on the server, because this method has many advantages.

One of the most important procedures in the operation of since.dll (network server in the same memory), than they operate outside the procedures (such as CGI or Perl script) to run faster, use fewer resources, the latter in Operation of each is called, will create their own examples (such as copying) as a separate process, a large number of servers use swap memory. This also means that in order to make components in the server process, running on (keyword: ASPAllowOutOfProcComponents), you do not need to amend Metabase (a storage Internet Information Server configuration settings structure, and Windows Registry same, but uses less disk space).

On the other hand, this method may be the most obvious shortcomings because it is the network server in the same memory space operations, any DLL problems are likely to server failure. Therefore, in the development and implementation of procedures for the application program needs very careful.

Procedural requirements and benefits

Completion of the functions mentioned in this paper, requires the following conditions:
● Visual Basic 5 or 6
● with IIS 4 NT server or workstation, or have any Windows 9.x PWS
● MS Word 97 (part of the Office 97 suite)

And the cases can be implemented with MS Word 2000, but there will be some problems, the last to mention in the article. Other additional software is unnecessary, and only need to ensure that the default is effective http://localhost/ site (click on the ultra-Lan Ya currency reef  files that the greatest skin or Windows NT server eb page).
We will create all the files are stored in the C: \ Inetpub \ scripts \ documents folder, so once the document is created, we provide a link to it is very easy (can be amended). Must establish this path, or we can not work on the case. All other documents are in our script path (C: \ Inetpub \ scripts). Dll we will be as flexible as possible, so that any template changes only need at least modify the code.

Deeper technology

To design the template can be based on a company in the hope that the contents of a document: Sign, the appropriate header and footer information, basic text, and so on. Another document creation, we would like to see in which areas of user specific information to join the marker (which makes this application is a dynamic process). As we look back at the code will be a close look at these parts. Us. Dll will include a function called GenerateDocument (in the category of internal), which requires it to transfer four parameters, namely:
● one for all marked with the boundaries of the string (from the text template)
● a corresponding values used for all the boundaries of the string (from the web browser on the user fill the form)
● templates on the server location
● documents generated on the server are stored position

Now we can carry out the down.

Combined
Document templates
First create a template word samples, it is our assumption that the standard document templates. We want to get this case staff of the information, and we hope that the document contains the following specific information: name, address, Email Id. Now create a template based on this information must be displayed in the document which will be the local user information to the appropriate markers (eg. <Name>, <Address ").

Document will be named EmployeeTemplate.dot (Remember, in the Save As dialog box file type list, select your document template, and its deposit C: \ Inetpub \ Scripts \ Templates \. Look at some materials can be downloaded contained document templates samples, it has to a better understanding.
 

COM component
Now use Visual Basic to create COM components, according to the following steps:
● start Visual Basic, select works as a ActiveX DLL file types.
● class name will be changed to DocumentObject, engineering MyDocumen changed the name of the paper (which is what we are created ASP pages COM components must show one case of the use of the information)
● Then, click on the File menu of options, References.
● Scroll down until see "Microsoft Word n.0 Object Library" (n is a recognition installed on the server object library version of the word). Selected this option, click click OK.

Please refer to the end of this document can be downloaded from the class module code. GenerateDocument () function from ASP to use it to transfer files of the four parameters.
It returns a string type, can be seen behind:
Option Explicit
Declare a New word application Object
Dim wdApp As New Word.Application
Public Function GenerateDocument (sTags, sValues, sSourcePath, sDestPath) _
As String
On Error GoTo ErrHandler
Dim arrTags () As String, arrValues () As String, iLoop As Integer

Implementation of this function is the first mission from the specified source path (as a parameter from the ASP document transmission from) open template files. Based on the server to create a template, use a new Word document:
WdApp.Documents.Open sSourcePath

Then, from the HTML form access to all the markings with Split function Add arrTags series. Comma is a boundary at the ASP documents concerning the value of markers separated:
ArrTags = Split (sTags, "and")

We will value the corresponding user input into arrValues series. Pipe character (|) at the boundary, to segregate these values: arrValues = Split (sValues, "|") code in arrTags in the circle, with search and replace operations (applications used Visual Basic script) from the series of markings found Marker, in the creation of the Word document, arrValues series with the corresponding values to replace them:

For iLoop = 0 To UBound (arrTags) wdApp.ActiveDocument.Content.Find.Execute arrTags (iLoop), True, _
,,,,, ArrValues (iLoop), 2
Next iLoop

You see is a string of comma-Execute method Find different attributes, we have not been provided. We only MatchWholeWord, ReplaceWith and ReplaceAll (Digital representative constants 2) Select an interest. Then, we will file in designated purpose path and file name, before the withdrawal and the release of this word closure Document Object:

WdApp.ActiveDocument.SaveAs sDestPath
WdApp.ActiveDocument.Close
WdApp.Quit
Set wdApp = Nothing
Withdraw from the function before the return of a Success signs:
GenerateDocument = "Success"
Exit Function
This is a wrong process. If the above applications encountered in the implementation of the wrong, it returns an error message.
ErrHandler:
Quit and release the word document object
WdApp.Quit
Set wdApp = Nothing
Build the Error Message, and pass it back
Dim ErrMsg As String
ErrMsg = "Error Number:" & & Err.Number "<BR> <BR>"
ErrMsg ErrMsg & = "Error Source:" & & Err.Source "<BR> <BR>"
ErrMsg ErrMsg & = "Error Description:" Err.Description & & "<BR> <BR>"
GenerateDocument = ErrMsg
Exit Function
End Function
Private Sub Class_Terminate ()
Release the reference
Set wdApp = Nothing
End Sub

In Visual Basic, preservation applications (such reservations and engineering documents), the compiler to see whether they had any mistakes. Then, open the File menu, click the Make MyDocument.dll. Retain its name, works in the file folders in preserving it.

Then, in the web server registered dll, as follows: ● MyDocument.dll will be copied to the windows \ system or winnt \ system32 path (depending on the operating system).
● At the command prompt implementation of the following orders under C: \ winnt \ system32> regsvr32 MyDocument.dll
● You will see a successful information: DllRegisterServer in MyDocument.dll.
The above completed the major part of applications. We will now proceed to the next step.

HTML pages

Now need to create HTML pages (EmployeeForm.html), in which user input will be filled to a new Word document value. Below we use the HTML page sample:
<HTML>
<TITLE> Employee Registration Page </ TITLE>
<BODY BGCOLOR = # ECECEC>
<CENTER> <FONT FACE = arial SIZE = 1>
<FONT SIZE = 5> Employee Registration Page </ FONT>
<HR ALIGN = center COLOR = black> <BR>
Please complete your details as per the Registration Form ... <BR> <BR>
<TABLE CELLSPACING CELLPADDING = 5 = 1 BGCOLOR = "# 000000" BORDER = 0 ALIGN = center>
<FORM ACTION = post CustomDoc.asp METHOD =>
<TR> <TD BGCOLOR = # 00BCA8 COLSPAN = 2 ALIGN = center>
<B>
<FONT FACE = arial SIZE = 1 COLOR = black> Employee Details
</ TD>
</ TR>
<TR> <TD BGCOLOR = # C4C2C2>
<B> <FONT FACE = arial SIZE = 1 COLOR = black> Your Name:
</ TD>
<TD BGCOLOR = # E3E1E1> <INPUT TYPE = Text NAME = "Name" SIZE = 20 MAXLENGTH = 25> </ TD>
</ TR>
<TR> <TD BGCOLOR = # C4C2C2>
<B> <FONT FACE = arial SIZE = 1 COLOR = black> Address: </ TD>
<TD BGCOLOR = # E3E1E1>
<INPUT TYPE = Text NAME = "Address" SIZE = 40 MAXLENGTH = 40>
</ TD> </ TR TR> <>
<TD BGCOLOR = # C4C2C2>
<B> <FONT FACE = arial SIZE = 1 COLOR = black> City, State: </ TD>
<TD BGCOLOR = # E3E1E1> <INPUT TYPE = Text NAME = "City" SIZE = 20 MAXLENGTH = 20>
<INPUT TYPE = Text NAME = "State" SIZE = 20 MAXLENGTH = 20>
</ TD> </ TR>
<TR> <TD BGCOLOR = # C4C2C2>
<B> <FONT FACE = arial SIZE = 1 COLOR = black> Zip, Country: </ TD>
<TD BGCOLOR = # E3E1E1>
<INPUT TYPE = Text NAME = "Zip" SIZE = 20 MAXLENGTH = 20>
<INPUT TYPE = Text NAME = "Country" SIZE = 20 MAXLENGTH = 20>
</ TD> </ TR TR> <> <TD BGCOLOR = # C4C2C2>
<B> <FONT FACE = arial SIZE = 1 COLOR = black> Email: </ TD>
<TD BGCOLOR = # E3E1E1> <INPUT TYPE = Text NAME = "Email" SIZE = 40 MAXLENGTH = 40>
</ TD> </ TR TR> <>
<TD BGCOLOR = # 00BCA8 COLSPAN = 2>
<INPUT TYPE = Submit VALUE = "Save and Generate Profile Document">
<INPUT TYPE = Reset VALUE = "Clear Fields">
</ TD> </ TR>
</ FORM> </ TABLE>
</ BODY> </ HTML>
Document will be deposited in the C: \ Inetpub \ scripts \, it is necessary to determine the paths in the script or personal IIS Web server in read and the implementation of competence.


ASP pages
ASP pages (CustomDoc.asp) the actual completion of the following tasks: obtaining user input, using COM components to create Word documents,
It will be handed back to users. Below is with Notes code:
<%
Dim sTags, sValues, sDestPath, sSourcePath, resValue
Get all the User Input values from the Form
SName = Request ( "Name")
SAddress = Request ( "Address")
SCity = Request ( "City")
SState = Request ( "State")
SZip = Request ( "Zip")
SCountry = Request ( "Country")
SEmail = Request ( "Email")
Now sDate = () Create a list of all the tags as defined in the Word Template by You
STags = "<Name>, <Address>, <City>, <State>, <Zip>, <Country>" & _
"<Email>, <Date>"
Gather up all the User Input values into one delimited string
SValues = sName & "|" & sAddress & "|" & sCity & "|" & & _ sState
"|" & SZip & "|" & sCountry & "|" & sEmail & "|" & sDate

After completion, the need to identify the source document (Template) and the target document (Document) position. APPL_PHYSICAL_PATH CustomDoc.asp return to the path of the document. We will file folders and file names attached together, it is the negative space of the user name.

SSourcePath = Request.ServerVariables ( "APPL_PHYSICAL_PATH") & _
"Templates \" & "EmployeeTemplate.dot"
SDestPath = Request.ServerVariables ( "APPL_PHYSICAL_PATH") & _
"Documents \" & Replace (sName, "","") &." Doc "

MyDocument now completed the common object of the show and call GenerateDocument.doc in return value on the basis of proper operation.
Set myDocObj = Server.CreateObject ( "MyDocument.DocumentObject")
Call the GenerateDocument function while passing the required
Parameters retValue = myDocObj.GenerateDocument (sTags, sValues, sSourcePath, _
SDestPath)
Take appropriate action based on the returned value
If retValue = "Success" Then
Msg = "Successfully generated your Document:" & Replace (sName, _
"", "") &. "Doc"
Response.Write ( "<font face = arial size =- 1 color = Green> <br>" & Msg)
Response.Write ( "<br> <br> <a href = Documents /" & _
Replace (sName, "","") &." Doc "&"> Here it is! </ A> ")
Else Response.Write ( "<font face = arial size =- 1 Red color => <br>" & retValue) End If
%> Documents will be deposited in the C: \ Inetpub \ scripts \. On the trip. Now type in the browser
Http://localhost/scripts/EmployeeForm.html, executing applications on the importation of all the domain, click
Save and Generate Profile Document. We can now see how the method is effective and the appearance.

Office 2000 for the issue of

If your machine installed Word 2000, and used Microsoft word 9.0 object library to create dll, it would encounter some problems. Seem to work in the component itself is good, there is no error, but the call from the ASP page will be operational overtime, or have an ActiveX can not create the wrong target. This is perhaps the Office 2k design characteristics, or perhaps was a mistake (Bug). Below explain why these problems occur. Generally speaking, this is because security can not credit all of the objects in the show were continuing. When a call. Asp page as IUSR account (the default web server on the internet customer account) continued. When you call us to create the COM component, was also IUSR credit transfer.

COM in turn give rise to this office COM (a target) call, but can not transfer credit. To overcome this problem, we need to do the following:
● Open Component Services console, and then right click the COM + Applications.
● New Application choice, follow the COM application wizard named an empty app.

● open up new applications, in the Components Right click on.
● choose to create a new component has been incorporated into select components.
 
● next list of the components of the dialog box, select MyDocument.DocumentObject, click finish. Application procedures now
Is likely to work.
MTS through COM input, we allow the continued COM for specific accounts, it will be transmitted to the call COM (in the previous operation seems to have occurred). I guess this is because Microsoft only allows a credit continued safety, I do not doubt the credit can be sustained.

Observations and conclusions we have essentially completed. Development and application procedures still need some attention.
● If you carefully observe the components of the design, you will be watching even if you have to modify the templates, add, delete or marked text, it does not need to amend the code. It can be re-used. Only through ASP document transmission parameters, you can quickly create companies with the tastes of a Word document.
● You can customize it according to need. Only need to ensure that in the template file, your marker is the only (in the context of two different not to use the same marker).
● also can be enhanced application capabilities, once created, can be based on the input of the Email ID (or from the database) email documents directly to users, in order to achieve interactive improvements.
● in ASP document on the marking and the string value, it should be mutual synchronization, despite the template in the document they will not necessarily in sync.
 
Previous:Tomcat JSP classic example of configuration
Next:JSP enhance the unique skills of the seven applications
User Reviews
Site Search
Related Articles
Recommended article
AD