Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel Macro to read xml file from web

Posted on 2014-01-22
8
Medium Priority
?
867 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 43

Expert Comment

by:Rob
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 43

Accepted Solution

by:
Rob earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 43

Expert Comment

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

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 43

Expert Comment

by:Rob
ID: 39816062
Did you add the reference to msxml?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

916 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