Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Compare Word Documents Through ActiveX in Visual Basic : Page 2

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.

Properties, Methods, and Events
Objects use properties, methods, and events to communicate with their external (i.e. calling) environment. Properties act like simple variables—they can be read and have values assigned to them. However, a property may not necessarily be implemented as a simple variable. It can also be implemented by complex calculations.

Methods are the functions and routines the class exposes to implement its functionality. When an instance of a class is defined, the public methods can be called from whichever COM-enabled language is being used, whether VB, ASP, or something else.

Events are a way of reporting back to a calling program from within a class. Events fire when something important happenslike a command button is clicked on, or a timer control counts down to zero. Events allow a method to be called back in the controlling program whenever something of interest has occurred. The controlling program can then respond to this event in an appropriate fashion.

So, properties, methods, and events, or "PME," are the programming building-blocks used to allow others to interact with an ActiveX DLL.

In the Word document comparison project, three properties are needed: InFile1, InFile2, and OutFile. These hold the two source documents to be compared and the resultant output file which is generated. Only the compare method is necessary.

Listing 1 contains the code for a class module to implement these properties and method. It first defines local variables for working storage space and then declares some API calls. When creating an output file, you want to be sure a name is not used that already exists on the hard diskotherwise, it will be overwritten. The safest way to achieve this is to use the Windows API to request a temporary filename that is guaranteed to be unique.

Let and Get methods are implemented for each property. These are called whenever the calling program tries to assign a value to (Let) or read (Get) a property.

The class module next has a Class_Initialize sub that is much like a VB form's Form_Load method. This is automatically executed whenever a class is instantiated. This is a good point to initialize all variables.

The class module next defines the compare method, which in many ways is the "real guts" of the DLL. It's where the real work is being performed to achieve the purpose of comparing Word documents.

Microsoft Word provides this functionality for you. Simply create an instance of a Word application and then use it to perform the comparison. To achieve this, the ActiveX DLL project uses the Microsoft Word Object Library. Click Project/References to add this library to the project references.

Here's the code:

' Actual function to do the work. Public Sub Compare() On Error Resume Next Dim oDoc As Word.Application ' Load first text file Set oDoc = CreateObject("Word.Application") oDoc.Documents.Open FileName:=strInPath2 ' If no output filename has been specified, then make a ' temporary one up If strOutFile = "" Then strOutFile = CreateTempFile("DWWC") & ".doc" End If With oDoc.ActiveDocument .SaveAs FileName:=strOutFile .TrackRevisions = True .ShowRevisions = True .PrintRevisions = True .Compare Name:=strInPath1 .Close wdSaveChanges, wdWordDocument, False End With Set oDoc = Nothing End Sub

Use the File menu to compile and build the DLL.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date