Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Methods

From: "Greg Maxey" <gmaxey@--------------------.-------------->
To: NULL
Date: 12/18/2008 3:15:00 PM
Hi,

I am a newbie to this group and just beginning to dabble with XML.  Most of 
my experience to date has been with writing some basic XML to customize the 
Word Ribbon UI.

Today I decided to see if I could figure out how to use an XML document as 
source data for populating a Word UserForm ListBox and ComboBox.  I have 
managed to get it working, but wanted to post my method here to see if an 
expert might provide some clarification or tips for improvements.

First I created the XML document that contains the source data:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<FormData>
  <Contact>
      <Name>Bob Smith</Name>
      <Address>123 Miller Street&#13;Carlise, PA 17013</Address>
      <Phone>828-123-4567</Phone>
    </Contact>
    <Contact>
      <Name>Bill Jones</Name>
      <Address>123 Smith Court&#13;Hamilton, OH 45013</Address>
      <Phone>123-123-4877</Phone>
   </Contact>
   <Contact>
     <Name>Harry Miller</Name>
     <Address>123 Jones Road&#13;Andrews, NC 28901</Address>
     <Phone>828-567-4567</Phone>
   </Contact>
</FormData>

I saved this XML as "C:\UserForm Data.xml"

Next I opened Word and created a new project module and userform module:

The project module uses a procedure and function to check if the XML source 
is valid and if so it displays the userform:

Public Const Source As String = "C:\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
End Sub

Function LoadDataPass() As Boolean
'Check if XML 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 a parsing error."
End If
End Function

The userform has a listbox and a combobox that I populate with data from the 
XML source.  Command button code is then used to insert the data selected in 
the listbox or combobox into the document.

Dim pStr As String
Dim oRng As Word.Range

Private Sub UserForm_Initialize()
Set oRng = Selection.Range
LoadData
End Sub

Sub LoadData()
Dim xmlDoc As New MSXML2.DOMDocument30
xmlDoc.validateOnParse = True
xmlDoc.async = False
xmlDoc.Load (Source)
GetNodeValues xmlDoc.ChildNodes
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
   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
End Sub

Private Sub cmdInsertCB_Click()
With Me.ComboBox1
  pStr = .Column(0, Me.ComboBox1.ListIndex) & vbCr _
         & .Column(1, Me.ComboBox1.ListIndex) & vbCr _
         & "Phone: " & .Column(2, Me.ComboBox1.ListIndex)
End With
With oRng
  .Text = pStr
  .Collapse wdCollapseEnd
  .Select
End With
Me.Hide
End Sub

Private Sub cmdInsertLB_Click()
With Me.ListBox1
  pStr = .Column(0, Me.ListBox1.ListIndex) & vbCr _
         & .Column(1, Me.ListBox1.ListIndex) & vbCr _
         & "Phone: " & .Column(2, Me.ListBox1.ListIndex)
End With
With oRng
  .Text = pStr
  .Collapse wdCollapseEnd
  .Select
End With
Me.Hide
End Sub


Questions.

1) Is there a more direct or efficient way to determine if the XML source is 
valid?
2) Show there be some kind of "clean up" code to "kill" the xmlDocs I have 
created?

Any other suggestions to improve this method are appreciated. Thanks.

-- 
Greg Maxey -  Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org





transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent