word| Create | Dynamic Most companies have developed a detailed documentation program because they realize that undocumented work can be a stumbling block to progress. Each company defines its own set of document templates for different processes so that they can be used by staff at any time, for purchase requests or for vacations.
However, as the Internet becomes increasingly familiar and popular, more and more features are being ported to "open space" for better visibility and more efficient communication. For example, one might ask, "Can I log in to Internet/intranet, fill out a vacation application form and send it as a Word document to my manager in the company standard template style?" "The answer is yes, the following will show you how to implement it."
About this application
Our application has a sample form that is filled in by users who visit the Web site. Once committed, the ASP file collects the information and creates a Word document with its contents using a predefined template stored on the Web server. It then displays a link that allows the user to view or download the document.
We create a visual Basic COM component (an ActiveX DLL), call it through an ASP application, and pass the necessary parameters to it. Components essentially use the Microsoft Word Object Library to create a reference to a Word document that will pass parameters. All of this is done on the server because there are many advantages to this approach.
The most important of these is the Since.dll running within the program (in the same memory as the Web server), they run faster than programs that run outside of the program, such as CGI or Perl scripts, and use fewer resources, which create their own instances (such as replication) as separate programs each time they are invoked in a run , so you need to use a large amount of server memory. This also means that in order for an out-of-process component to run on the server (keyword: aspallowoutofproccomponents), you do not need to modify the metabase (a structure that stores Internet Information Server configuration settings, as with Windows registration, But less disk space is used).
On the other hand, the most obvious drawback of this approach may be that it runs in the same memory space as the network server, and that any DLL problem can cause the server to fail. Therefore, you need to be very careful when developing and executing applications within your program.
Procedural requirements and benefits
To complete the functions described in this article, the following conditions are required:
Visual Basic 5 or 6
NT Server or workstation with IIS 4, or any Windows 9.x with PWS
MS Word (part of the Office 97 suite)
This example can also be performed with Ms Word 20,001, but there are a few questions to be mentioned at the end of the article. Additional software is unnecessary, just make sure the default site http://localhost/is valid (clicking this hyperlink will take you to a personal Web server or Windows NT home page).
We'll store all the documents we create in the C:\Inetpub\scripts\documents folder, so it's easy to provide a link to it once the document is created (you can modify it as needed). Be sure to create this path, otherwise our example will not work. All other files are located in our script path (C:\Inetpub\scripts). Our DLLs will be as flexible as possible so that any modification to the template requires minimal code modification.
A deeper layer of technology
The template you want to design can be based on what a company wants to appear in their documents: login, appropriate header and footer information, basic text, and so on. In addition to the creation of the document, we want to include the tag where we see the user's special information (which makes the application dynamic). We'll look at these sections carefully as we review the code. Our. dll will contain a function called generatedocument (inside the class file) that requires passing 4 parameters to it, respectively:
A demarcation string for all tags (from a text template)
A boundary string for all corresponding values (from a table populated by a user on a Web browser)
The location of the template on the server
Where the generated document is stored on the server
Now we can go down.
Grouped together
Document templates
First create a sample of a Word template, assuming it is the standard document template for our company. We want to get the employee information for this example, we want the document to contain the following specific information: name, address, Email Id. Now that you are creating a template based on this information, be sure to include the appropriate tags in the document where you want to display the user information, such as. < Name, < address >).
Name the file Employeetemplate.dot (remember, select the document template in the file type list of the Save As dialog box and deposit it in C:\Inetpub\Scripts\Templates\.) Take a look at the documentation template samples that are included in the downloadable material section to give it a better understanding.
COM components
Now create a COM component with Visual Basic, follow these steps:
Start Visual Basic and select the ActiveX DLL as the project file type.
Change the class name to DocumentObject, and the project file name to Mydocumen (this is the information we want to use when we create an example of a COM component in an ASP page)
Next, click the Project File menu option to References.
Scroll down until you see "Microsoft Word n.0 Object Library" (n is a number that identifies the version of the Word object libraries installed on the server). Select this option and click the Click OK.
Please refer to the class module code in the download file at the end of this article. The Generatedocument () function uses 4 parameters that are passed to it from an ASP file.
It returns a string type, which you can see later:
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
The first task performed by this function is to open the template file from the specified source path (passed as a parameter from the ASP file). Refer to a new Word document based on the template created on the server:
WdApp.Documents.Open Ssourcepath
Then, all the tags obtained from the HTML form are placed in the arrtags sequence with the Split function. A comma is a delimiter that separates the tagged values in an ASP file:
Arrtags = Split (Stags, ",")
We deposit the corresponding user input value into the arrvalues sequence. The pipe character (|) is a delimiter that separates these values: arrvalues = Split (svalues, "|") code loops through Arrtags, finds the tag in the tag sequence with the Find and replace operation (visual Basic in Application script), and in the created W In the Ord document, replace them with the corresponding values in the Arrvalues series:
For iloop = 0 to UBound (arrtags) WdApp.ActiveDocument.Content.Find.Execute arrtags (Iloop), True,, _
,,,,, Arrvalues (Iloop), 2
Next Iloop
You see a bunch of commas that are different attributes of the Find-execute method and we have no settings. We are interested only in the settings selection for MatchWholeWord, ReplaceWith and ReplaceAll (represented by the number constant 2). Then, we save the document to the specified destination path and file name, and close the Word document object before exiting and releasing it:
WdApp.ActiveDocument.SaveAs Sdestpath
WdApp.ActiveDocument.Close
Wdapp.quit
Set Wdapp = Nothing
Returns a ' Success ' flag before exiting the function:
Generatedocument = "Success"
Exit Function
This is an error handler. If an error is encountered in the above application execution, 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, save the application (retention class and project file name) and compile to see if any errors have occurred. Then, open the File menu and click Make MyDocument.dll. Keep its name and save it in the same folder as the project file.
Then, register the DLL on the Web server as follows: Copy the MyDocument.dll to the Windows\System or Winnt\System32 path (depending on the operating system you are using).
At the command prompt, execute the following command C:\winnt\s