Reading XML files newer than.

KongGulerod

New Member
Joined
Feb 23, 2017
Messages
17
Office Version
  1. 2013
Platform
  1. Windows
Hi

I have a script that reads XML files, one file at at time and joins them to a sheet. It works. The function that does the actual reading of the XML looks like this:

Code:
Private Function ReadXmlFile(filename As String, ws As Worksheet, rowNo As Integer) As Boolean
    Dim objXML As New MSXML2.DOMDocument
    Dim point As IXMLDOMNode
    Dim result As IXMLDOMNodeList
    Dim strXml As String
    Dim arrFields As Variant
    Dim value As String
    Dim i As Integer
    
    On Error GoTo ErrorHandler
    
    arrFields = Split(Globals.XmlFields, ":")
    
    'Load XML from file
    If objXML.Load(filename) Then
        strXml = objXML.XML
            
        For i = 0 To UBound(arrFields)
            value = objXML.SelectSingleNode("/my:myFields/my:" & arrFields(i)).Text
            If isDateValue(value) Then value = Left(value, 10)
            ws.Cells(rowNo, i + 1) = value
        Next i
    Else
        notProcessedXml = notProcessedXml & vbCrLf & filename
        ReadXmlFile = False
    End If
    ReadXmlFile = True
Exit Function


ErrorHandler:
    notProcessedXml = notProcessedXml & vbCrLf & filename
    ReadXmlFile = True
End Function

I would love to limit the reading of files to be files that has been changed in 2017. How can I do this. I need to add some testing of the file date in the IF statement, but how and where?

Kind regards KongGulerod
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You can use FileDateTime in the VBA library. I would put it just before your loop into this function. For instance this loop that only wants to read files from the current year.

Code:
Dim filenames As Collection
Dim filename As Variant
Dim ws As Worksheet
Dim Success As Boolean

......

  For Each filename In filenames
    If Year(FileDateTime(CStr(filename))) = Year(Date) Then
      Success = ReadXmlFile(CStr(filename), ws, 1)
    End If
  Next filename
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top