+ Reply to Thread
Results 1 to 11 of 11

Looping through many .xml files, exporting each to csv

  1. #1
    Registered User
    Join Date
    03-21-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    6

    Looping through many .xml files, exporting each to csv

    Hello dear community!

    I am completely new to VBA/Excel programming and need your help, because I can't figure out how to make it work from what I am finding via Google...

    So I have a folder with several thousand .xml files. I can open each of those in excel and then export to csv.

    But since I have several thousands of those, I obviously wanna automate the process...

    Can someone help me how to do this (e.g. with VBA)?

    Thanks so much!

    parsimoneous

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Looping through many .xml files, exporting each to csv

    Try this and let me know how it works :

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    03-21-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    6

    Re: Looping through many .xml files, exporting each to csv

    Quote Originally Posted by Logit View Post
    Try this and let me know how it works :

    Please Login or Register  to view this content.


    Hello Logit,

    thank you so much for your help!

    I tried it - at first it said 'compile error: variable not defined'. Google told me that this might be because of the 'Option Explicit' (even though all the objects appear to be defined afterwards?).
    If I remove the 'Option Explicit' it does successfully open one of the xml files in the folder, but then stops with the error message "Runtime error '1004': Method 'SaveAs' of object '_Workbook' failed"


    Do you have an idea of what might've gone wrong? When I try to save it as a csv manually, it gives me a message "some features might be lost, do you wanna continue?" before proceeding - could that cause the problem?
    Btw, if that makes it easier in any way, an export to xls or xlsx would be just as good as csv. In the end I just need sth that I can load into R or Stata (both have routines to import xml, but they fail for some reason while excel is able to open the xmls).

    Thank you again for your time!

    Parsimoneous
    Last edited by Parsimoneous; 03-22-2017 at 10:12 AM.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Looping through many .xml files, exporting each to csv

    Download converter :

    https://sourceforge.net/projects/convertxmltocsv/


    Basic macro code :

    https://www.mjtnet.com/blog/2009/10/...rt-xml-to-csv/


    Larger macro code :

    https://www.extendoffice.com/documen...xml-files.html

    Here is the macro :

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-21-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    6

    Re: Looping through many .xml files, exporting each to csv

    Quote Originally Posted by Logit View Post
    Download converter :

    https://sourceforge.net/projects/convertxmltocsv/


    Basic macro code :

    https://www.mjtnet.com/blog/2009/10/...rt-xml-to-csv/


    Larger macro code :

    https://www.extendoffice.com/documen...xml-files.html

    Here is the macro :

    Please Login or Register  to view this content.

    Thank so much, Logit! I'll only have access to the computer again tomorrow, but then I'll give that a shot.
    One question before that (sorry, this probably is a dumb question, bur I really have no clue how to read this code so I have no idea what's going on): Are those 4 alternative ways of doing it? Or is it one way, so do I need to install the converter and then run all the three macros you listed?

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Looping through many .xml files, exporting each to csv

    Those are all different methods of conversion. UNTESTED ... I just went searching for resources to assist you.

    I don't have any XML files here. So I can't test any of them. If you want to post 5 or 10 samples then I could experiment.

  7. #7
    Registered User
    Join Date
    03-21-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    6

    Re: Looping through many .xml files, exporting each to csv

    Quote Originally Posted by Logit View Post
    Those are all different methods of conversion. UNTESTED ... I just went searching for resources to assist you.

    I don't have any XML files here. So I can't test any of them. If you want to post 5 or 10 samples then I could experiment.
    I see, thank you for your time, Logit! I'll try it tomorrow when I got access to my office computer - if it does not work, I'll try to find a few non-sensitive xmls to post!

  8. #8
    Registered User
    Join Date
    03-21-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    6

    Re: Looping through many .xml files, exporting each to csv

    Quote Originally Posted by Logit View Post
    Those are all different methods of conversion. UNTESTED ... I just went searching for resources to assist you.

    I don't have any XML files here. So I can't test any of them. If you want to post 5 or 10 samples then I could experiment.
    Hi Logit,

    the 'Here is the Macro' macro code that you posted seems to come very close - it is definetly looping through all the xmls, I can see that - it's just that there are no csv/xls/xlsx files appearing anywhere, i.e. the folder looks just the same as before. So something about the 'saving-part' does not yet seem to work... do you have an idea what to change?


    EDIT:

    Actually it does save one file as a .csv (it calls it Book2.csv) on the Desktop! And in the process of running it asked me to confirm that I wanted to save it as a non-macro enabled document.

    ANOTHER EDIT:

    It does put all the xmls together in one big excel file - which, in principle, would be great, the only issue is that it becomes too big - but actually there is a lot of repeated lines in each xml - so is there a way to only use the first like 10 rows maybe of each xml and otherwise keep it the same?


    Thank you
    Last edited by Parsimoneous; 03-23-2017 at 12:08 PM.

  9. #9
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Looping through many .xml files, exporting each to csv

    Post the file/s you are working with.

  10. #10
    Registered User
    Join Date
    03-21-2017
    Location
    US
    MS-Off Ver
    2013
    Posts
    6

    Re: Looping through many .xml files, exporting each to csv

    Quote Originally Posted by Logit View Post
    Post the file/s you are working with.

    Is there some way I can send 2 of those (overall several 1000s) files without posting it online, e.g. via some disposable email address of your or so? Thank you for your patience...

    Btw, the converter program that you mentioned works very well - except it only takes one xml at a time and I have a couple of thousand....

  11. #11
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: Looping through many .xml files, exporting each to csv

    Sent you a private message.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Exporting Files
    By emir0514 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-29-2014, 07:05 PM
  2. Exporting files to text files
    By excelvraag in forum Excel Programming / VBA / Macros
    Replies: 29
    Last Post: 08-22-2012, 10:33 AM
  3. Exporting files to text
    By skartykn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2012, 03:47 AM
  4. Exporting rows to files
    By Moranica in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2010, 12:51 AM
  5. Exporting CSV Files
    By Paul W Smith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-11-2006, 07:45 PM
  6. Exporting lists to txt files
    By Alan Davies in forum Excel General
    Replies: 1
    Last Post: 07-27-2005, 11:05 AM
  7. [SOLVED] Exporting delimited files
    By Greegan in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-14-2005, 03:06 AM
  8. Exporting to several xls files
    By ole_ in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2005, 10:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1