Link to home
Start Free TrialLog in
Avatar of DemonForce
DemonForce

asked on

Excel Macro to read xml file from web

Hi,

I have several workbooks that need to access an XML file called config.xml from a website, say website.co.uk, although all the workbooks access the same config.xml it needs to have independent sections for each of them, so I can define version numbers initially followed by other data later on...

Therefore, how do I create a excel macro that will access this file, and check version number so I can compare if its latest version, how would I access the individual workbook versions from the xml, and how would the xml be laid out?

Cheers
Avatar of Rob
Rob
Flag of Australia image

Your XML would look something like this

<?xml version="1.0"?> 
<files>
    <file id="00413">
        <version>1.2.3</version>
        <title>Workbook about something</title>
    </file>
    .... // repeat multiple files as necessary
</files>

Open in new window


You'll have a better idea than me about what fields you'd need but I've started with 3: Version, Title and an ID attribute.  A numeric ID that is unique to the workbook is going to be the best way to compare and retrieve data for the workbook requesting it.

sample code to load the xml (need reference to MSXML library)
Sub xmlparse()
    Dim oDoc As New DOMDocument
    oDoc.async = False
    oDoc.validateOnParse = False
    fSuccess = oDoc.Load(ActiveWorkbook.Path & "\config.xml")
     
    MsgBox fSuccess
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DemonForce
DemonForce

ASKER

Seems perfect thank you for the examples, I assume by editing     fSuccess = oDoc.Load(ActiveWorkbook.Path & "\config.xml") I can get it to take the config.xml from any website?

Thanks
That's correct. The load argument can either be a path or url
Ok, been playing around with it and have put more data into the xml, and have it loading the data on worksheet open, however...  When I load the sheet there is a delay as it is loading the data from the file in, and then nothing is displayed on screen until I call the routine from a button attached to the main macro.

It would appear to me that it is loading the data and continues into macro, as it currently does not have data the criteria is not matched so therefore does not show, until it is activated second time?
Right, took the ELSE out and it works perfect now, cheers.
Hi,

I downloaded and extensively modified your example that you supplied for reading in XML.  For some reason when I export the code, and import elsewhere I get an error related to this part...

Dim oDoc As DOMDocument

Compile error:

User-defined type not defined


Always works fine in supplied example.
Did you add the reference to msxml?