Solved

Excel Macro to read xml file from web

Posted on 2014-01-22
8
809 Views
Last Modified: 2014-01-28
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
0
Comment
Question by:DemonForce
  • 4
  • 4
8 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39801982
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

0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39802018
Sample workbook loading the xml
xml.xlsm
config.xml
0
 

Author Comment

by:DemonForce
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?

Thanks
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39809247
That's correct. The load argument can either be a path or url
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:DemonForce
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?
0
 

Author Comment

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

Author Comment

by:DemonForce
ID: 39816024
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.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39816062
Did you add the reference to msxml?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now