To get started, create a new project and reference Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office.
To create a new project
To create a reference to MSXML 5.0
First, you must build the initial form used by the XML Extractor application. Add the following controls to the form.
For your application form to work with the sample code provided in the next section, you must set the properties in the following table.
Control | Property | Setting |
---|---|---|
Form | Caption | "Medical bills (XML Extractor) Example" |
Label1 | Caption | "File name:" |
Text1 | (Name)
Text |
txtFilename
"invoices.xml" |
Command1 | (Name)
Caption |
cmdStart
"Start" |
Command2 | (Name)
Caption |
cmdExit
"Exit" |
After you modify the property settings, resize the controls and arrange them on the form until your user interface looks like the following:
The following shows the complete code for the main form. To run the code as you read, select all the text, then copy it and paste it into the form of your own Microsoft® Visual Basic® project.
Option Explicit Private builder As MXXMLWriter50 Private stylesheet As DOMDocument50 Private Sub cmdExit_Click() End End Sub Private Sub cmdStart_Click() Dim reader As New SAXXMLReader50 Dim filter As New MyExtractor Dim builder As New MXXMLWriter50 builder.output = New DOMDocument50 Set stylesheet = New DOMDocument50 stylesheet.Load App.Path & "\" & "invoices.xsl" Set filter.IVBSAXXMLFilter_parent = reader Set filter.SAXXMLReader50_contentHandler = builder filter.cutElement = "invoice" On Error GoTo Uh_Oh reader.parseURL App.Path & "\" & txtFilename.Text Exit Sub Uh_Oh: MsgBox "*** Error while processing invoices *** " + Err.Description & " (in " & Err.Source & ")" End Sub Public Sub processInvoice(dom As MSXML2.DOMDocument50, sInvNumber As String) Dim s As String s = dom.transformNode(stylesheet) saveToFile s, App.Path & "\invoice" & sInvNumber & ".html" frmInvoice.txtAsXml = dom.xml frmInvoice.txtAsHtml = s frmInvoice.webPreviewPane.Navigate App.Path & "\invoice" & sInvNumber & ".html" frmInvoice.Show vbModal End Sub Private Sub saveToFile(text As String, fname As String) Open fname For Output As #5 Print #5, text Close #5 End Sub
Next, add the secondary invoice preview form used by the XML Extractor application.
To add a new form to the project
The preview form should allow the user to step through and preview the invoices as output in a Web browser. To support this feature, use the custom WebBrowser control. This is a custom ActiveX control provided as part of the Microsoft Internet Controls library. Therefore, you must add a reference in your project to this component library.
To create a reference to Microsoft Internet Controls
The Web Browser control will now appear in the Toolbox, allowing you to add it to the new form.
You can now add the following controls to the form.
For your application form to work with the sample code provided in the next section, you must set the properties in the following table.
Control | Property | Setting |
---|---|---|
Form | (Name)
Caption |
frmInvoice
"Invoice" |
Text1 | (Name)
MultiLine ScrollBars |
txtAsXml
True 2 - Vertical |
Text2 | (Name)
MultiLine ScrollBars |
txtAsHtml
True 2 - Vertical |
WebBrowser1 | (Name) | webPreviewPane |
Command1 | Name
Caption |
cmdNext
"Next invoice" |
Command2 | Name
Caption |
cmdBreakAndExit
"Break and exit" |
After you modify the property settings, resize the controls and arrange them on the form until your user interface looks like the following:
The following shows the complete code for the secondary form. To run the code as you read, select all the text, then copy it and paste it into the form of your own Microsoft® Visual Basic® project.
Private Sub cmdBreakAndExit_Click() End End Sub Private Sub cmdNext_Click() Hide End Sub
Extract Data From a Large Document | Overview of the XML Extractor Application | Sample Files (XML Extractor) | MyExtractor Class (XML Extractor) | Run the Application (XML Extractor) | How the XML Extractor Application Works