The Anchorage
Personal website of Gregory K. Maxey, Commander USN (Retired)
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!
This Microsoft Word Tips & Microsoft Word Help page demonstrates how you can populate a userform listbox or combobox from an external XML data source. For other more traditional methods, see my: Populate Userform Listbox
For this demonstration I created a simple XML file containing contact names, addresses, and phone numbers and saved the file on my PC hard drive as D:\Data Sources\Userform Data.xml.
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <FormData> <Contact> <Name>Bob Smith</Name> <Address>123 Miller Street Carlisle, PA 17013</Address> <Phone>828-123-4567</Phone> </Contact> <Contact> <Name>Bill Jones</Name> <Address>123 Smith Court Hamilton, OH 45013</Address> <Phone>123-123-4877</Phone> </Contact> <Contact> <Name>Harry Miller</Name> <Address>123 Jones Road Andrews, NC 28901</Address> <Phone>828-567-4567</Phone> </Contact> </FormData>
In the Word template, I added a standard project module and a userform module. The project module contains the code necessary to validate the XML source file and display the userform:
Option Explicit 'Declare variable and define the source XML file Public Const Source As String = "F:\Data Stores\Userform Data.xml" Sub CallUF() Dim oFrm As Userform1 If LoadDataPass Then Set oFrm = New Userform1 oFrm.Show Unload oFrm Set oFrm = Nothing End If lbl_Exit: Exit Sub End Sub Function LoadDataPass() As Boolean 'This function checks to ensure the XML document is valid Dim xmlDoc As New MSXML2.DOMDocument30 xmlDoc.validateOnParse = True xmlDoc.async = False If xmlDoc.Load(Source) Then LoadDataPass = True Else LoadDataPass = False MsgBox "The XML source file contains one or more parsing errors." End If lbl_Exit: Exit Function End Function
The userform contains a listbox, combobox, and two command buttons. The listbox and combobox controls store the XML data for each contact in three columns. The .ColumnWidth property of the controls is used to hide column 2 and 3 data from view. The userform for this demonstration is shown below:
Code in the userform initializes the form, and loads the XML data into the listbox and combobox. When a selection is made in either the listbox or combobox and the corresponding command button is pressed, the complete contact information is inserted into the document.
Option Explicit 'Declare variables Dim strTemp As String Dim oRng As Word.Range Private Sub Userform_Initialize() Set oRng = Selection.Range 'Assign Userform control properties Me.cmdInsertLB.Enabled = False Me.cmdInsertCB.Enabled = False With Me.ListBox1 .ColumnCount = 3 '0 values in columns 2 and 3 hides these columns from view. .ColumnWidths = "40;0;0" End With With Me.ComboBox1 .ColumnCount = 3 .ColumnWidths = "40;0;0" 'Require valid entry from list .MatchRequired = True .MatchEntry = fmMatchEntryComplete End With 'Call procedure that loads data from XML Source LoadData lbl_Exit Exit Sub End Sub Sub LoadData() 'Refer to the "Retrieving Information from an XML Document section _ of the article at http://msdn.microsoft.com/en-us/library/aa468547.aspx Dim xmlDoc As New MSXML2.DOMDocument30 xmlDoc.validateOnParse = True xmlDoc.async = False xmlDoc.Load (Source) GetNodeValues xmlDoc.ChildNodes lbl_Exit: Exit Sub End Sub Sub GetNodeValues(ByRef Nodes As MSXML2.IXMLDOMNodeList) Dim xmlnode As MSXML2.IXMLDOMNode For Each xmlnode In Nodes If xmlnode.NodeType = NODE_TEXT Then 'Load the nodeValues for named nodes into the listbox and combobox Select Case xmlnode.ParentNode.nodeName Case "Name" With Me.ListBox1 .AddItem .Column(0, Me.ListBox1.ListCount - 1) = xmlnode.NodeValue End With With Me.ComboBox1 .AddItem .Column(0, Me.ComboBox1.ListCount - 1) = xmlnode.NodeValue End With Case "Address" With Me .ListBox1.Column(1, Me.ListBox1.ListCount - 1) = xmlnode.NodeValue .ComboBox1.Column(1, Me.ComboBox1.ListCount - 1) = xmlnode.NodeValue End With Case "Phone" With Me .ListBox1.Column(2, Me.ListBox1.ListCount - 1) = xmlnode.NodeValue .ComboBox1.Column(2, Me.ComboBox1.ListCount - 1) = xmlnode.NodeValue End With End Select End If If xmlnode.HasChildNodes Then GetNodeValues xmlnode.ChildNodes End If Next xmlnode lbl_Exit: Exit Sub End Sub Private Sub ComboBox1_Change() 'Set state of command button If Me.ComboBox1.ListIndex <> -1 Then Me.cmdInsertCB.Enabled = True Else Me.cmdInsertCB.Enabled = False End If lbl_Exit: Exit Sub End Sub Private Sub ListBox1_Click() 'Set state of command button If Me.ListBox1.ListIndex <> -1 Then Me.cmdInsertLB.Enabled = True Else Me.cmdInsertLB.Enabled = False End If lbl_Exit: Exit Sub End Sub Private Sub cmdInsertCB_Click() 'Build data string With Me.ComboBox1 strTemp = .Column(0, Me.ComboBox1.ListIndex) & vbCr _ & .Column(1, Me.ComboBox1.ListIndex) & vbCr _ & "Phone: " & .Column(2, Me.ComboBox1.ListIndex) End With 'Insert data string With oRng .Text = strTemp .Collapse wdCollapseEnd .Select End With Me.Hide lbl_Exit: Exit Sub End Sub Private Sub cmdInsertLB_Click() With Me.ListBox1 strTemp = .Column(0, Me.ListBox1.ListIndex) & vbCr _ & .Column(1, Me.ListBox1.ListIndex) & vbCr _ & "Phone: " & .Column(2, Me.ListBox1.ListIndex) End With With oRng .Text = strTemp .Collapse wdCollapseEnd .Select End With Me.Hide lbl_Exit: Exit Sub End Sub
See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.
That's it! I hope you have found this tips page useful and informative. You can download the demonstration file and XML I used to create this tips page here: Populate Userform With XML Demo Pack
The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.
However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.
If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!