View Full Version : update listbox from XML
Runes
05-22-2013, 04:28 AM
Hi, im new in VBA.
I working with word and i need to update this
I need help with easy script, after day of searching and testing i dont know how to do it  :banghead:
I have userform with button and 3 listboxes. I need reaction after clicking to buton. After click i need read a xml from PC, deserialized, read values and update to concrete listbox. 
Thanks for help
sorry for my english
gmaxey
05-23-2013, 06:11 PM
This might get you started:
Pass the listbox and source path to LoadData:
Sub LoadData(ByRef oListOrComboBox As ListBox, ByRef strDataSource As String)
'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 (strDataSource)
GetNodeValues oListOrComboBox, xmlDoc.ChildNodes
End Sub
Sub GetNodeValues(oListOrComboBox As Object, 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
       With oListOrComboBox
         Select Case xmlnode.ParentNode.nodeName
           Case "Name"
             .AddItem
             .Column(0, oListOrComboBox.ListCount - 1) = xmlnode.NodeValue
           Case "State"
             .Column(1, oListOrComboBox.ListCount - 1) = xmlnode.NodeValue
           Case "Party"
             .Column(2, oListOrComboBox.ListCount - 1) = xmlnode.NodeValue
         End Select
       End With
    End If
    If xmlnode.HasChildNodes Then
      GetNodeValues oListOrComboBox, xmlnode.ChildNodes
    End If
  Next xmlnode
lbl_Exit:
  Exit Sub
End Sub
Runes
05-28-2013, 06:44 AM
Hi 
thx for your reply :) i miss the forum link and i fixit alone :) but thx very mutch for your help :)
But now i have second problem, i like to open a excel file from word userform and read all sheets (in next step i like to delte some sheets but first is filtering)
Function delSheets(exclfile As String, measur_system As String)
    
    Dim objXL As Object
    Set objXL = CreateObject("Excel.Application")
    
    With objXL
        .Workbooks.Open (exclfile)  'everithing ok file is open
            
    For Each sheet In objXL.Workbooks.worksheets    ' hire is the problem :(
            If worksheet.name <> "!" & "*" Then
           Debug.Print worksheet.name
        Else
           objXL.Application.DisplayAlerts = False
           Debug.Print worksheet.name
           objXL.Application.DisplayAlerts = True
        End If
    Next
    End With
End Function
Runes
05-29-2013, 01:32 AM
New function. I dont understand, program working but after reopen the file everithing is in, nothing deleted
Function delSheets(sFile As String, M_System As String)
    Dim i           As Integer
    Dim xlApp       As Object
    Dim xlWrkBk     As Object
    
    On Error Resume Next
    Set xlApp = CreateObject("Excel.Application")
    Set xlWrkBk = xlApp.workbooks.Open(sFile)
 
    
    For i = 1 To xlWrkBk.sheets.Count
        If xlWrkBk.sheets(i).name <> M_System Then
        xlWrkBk.sheets(i).Select
        End If
        Debug.Print xlWrkBk.sheets(i).name
    Next i
    
    xlApp.Application.DisplayAlerts = False
    xlWrkBk.SelectedSheets.delete
    xlApp.Application.DisplayAlerts = True
    xlWrkBk.Save
    xlWrkBk.Close
    xlApp.Close
    xlApp.Quit
    
End Function
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.