Browse DevX
Sign up for e-mail newsletters from DevX


Compare Word Documents Through ActiveX in Visual Basic : Page 3

ActiveX is a highly flexible and extensible benefit to Visual Basic, permitting vastly expanded functionality across all Windows-based development environments, including Active Server Pages. ActiveX components allow Visual Basic programmers to encapsulate complex functionality in small, reusable packages. Learn how to use ActiveX to handle out-of-date and duplicated documents in a content-management intranet.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Testing and Debugging the DLL
Being a good programmer, you know you need to test code before releasing it. But how do you test a DLL when it has no user interface and no input or output screens? It's not like a conventional VB application where you just execute a .exe file.

What you need is a simple driver program that quickly passes data to the DLL and allows its methods to be used. This will be a separate projecta standard Windows exe. The simplest thing to do is use File/Add Project (rather than New Project) to create a project group, containing both the DLL and the driver .exe projects. This allows both projects to be managed from the one project group.

Figure 1: This is the user-interface of the driver program that tests the DLL.

Listing 2 contains the code and form definition for a driver program. This program prompts for two input files and provides both a "Compare" button to perform the comparison and a "View" button to display the results.

The user can browse for two existing Word documents via the CommonDialog control. The View button opens Word and displays the resultant output document. A Label control displays status messages back to the user.

The driver program first declares an Object to make an instance of the class:

Dim objWDComp As Object

This object is then instantiated on the Form_Load:

Set objWDComp = CreateObject("DWLib.WordComp")

The user is able to locate and specify two input documents. The Compare button event-handler then passes the names of these to the object, and calls its compare method. The code for this is extremely simple:

Private Sub cmdCompare_Click() objWDComp.InFile1 = tPath1.Text objWDComp.InFile2 = tPath2.Text objWDComp.Compare lblStatus.Caption = "Created output file " & _ objWDComp.OutFile End Sub

Figure 2: Clicking the Compare button invokes the ActiveX DLL, performs a comparison, and returns the name of a generated output file with the results of the comparison.

Figure 2 shows the driver program in action.

Building an ASP Page
Now that the control has been tested, you can make an ASP page to achieve the same results. The DLL itself does not need to be modified.

Listing 3 contains a simple HTML form to accept filenames from the user. These filenames must be meaningful for the server and not just from the user's own hard disk. The DLL is invoked and used in the ASP page once the user has submitted two filenames. The ASP code in Listing 4 receives fields passed in from the form and then performs a comparison. Again, the code to achieve this is very simple:

<% Set objWDComp = CreateObject("DWLib.WordComp") ObjWDComp.InFile1 = Request.Form("InFile1") ObjWDComp.InFile2 = Request.Form("InFile2") ObjWDComp.Compare %>

Figure 3: Opening the generated output file in Word shows the revision marks where the source documents vary, allowing all differences to be seen clearly.

Enterprise-enable this DLL
When building an ActiveX DLL, the Visual Basic IDE registers the DLL with Windows so that it can be used from other programs. If the DLL is simply copied to the hard disk of another computer, the version of Windows on that computer will have no knowledge of the DLL and will not be able to use it.

To fix this, use the command-line tool REGSVR32 on the second PC to register the DLL with Windows. REGSVR32 can be found in the Windows/WinNT system directory. Professional deployment tools like the VB deployment and packaging wizard or InstallShield will register the component for you at the time of installation.

David M. Williams is an Australian I.T. writer, trainer, and consultant. He is the author of Win32 Font Lister and other shareware programs. He and his wife, Alison, are expecting their first child in March! Reach him by e-mail at ap_dmw@bigpond.com.
Thanks for your registration, follow us on our social networks to keep up-to-date