Excel Macro to read xml file from web

Posted on 2014-01-22
Last Modified: 2014-01-28

I have several workbooks that need to access an XML file called config.xml from a website, say, 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?

Question by:DemonForce
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 43

Expert Comment

ID: 39801982
Your XML would look something like this

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

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

LVL 43

Accepted Solution

Rob earned 500 total points
ID: 39802018
Sample workbook loading the xml

Author Comment

ID: 39809226
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?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 43

Expert Comment

ID: 39809247
That's correct. The load argument can either be a path or url

Author Comment

ID: 39809291
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?

Author Comment

ID: 39809310
Right, took the ELSE out and it works perfect now, cheers.

Author Comment

ID: 39816024

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.
LVL 43

Expert Comment

ID: 39816062
Did you add the reference to msxml?

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question