Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: .csv to .xls conversion using VBS  (Read 13486 times)

0 Members and 1 Guest are viewing this topic.

kyle_engineer

    Topic Starter


    Intermediate
  • 010010110101100
  • Thanked: 4
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
.csv to .xls conversion using VBS
« on: January 02, 2013, 03:42:37 PM »
Hello.  :D

So I'm trying to automate a .csv to .xls conversion. I have this little snippet (below) that works to get the data into an .xls file, the problem is that it doesn't actually utilize the "," delim. So, while this opens the .csv in Excel and re-saves it, I need it to actually use the "Get External Data -> Text File..." method. Anyway, any help is appreciated.

Code: [Select]
Dim myXL
Set myXL=CreateObject ("Excel.Application")

myXL.WorkBooks.Open "C:\DIR\file.csv"

myXL.DisplayAlerts=False 'To keep Excel hidden
myXL.ActiveWorkbook.SaveAs "C:\DIR\file.xls", -4143 'IDK what the "-4143" does
myXL.DisplayAlerts=True

myXL.ActiveWorkbook.Close False
myXL.Quit

Set myXL = Nothing

NOTE: If there are any typos, know that they are correct in the actual .vbs. :)
"Any answer is only as good as it satisfies the question." - Me

0000000100101011000 -
010010010010000001001100010011110100110 001000000010110010100111101010101

kyle_engineer

    Topic Starter


    Intermediate
  • 010010110101100
  • Thanked: 4
    • Yes
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 7
Re: .csv to .xls conversion using VBS
« Reply #1 on: January 02, 2013, 04:22:33 PM »
Alright, I fixed it. :)

This works:

Code: [Select]
Dim myXL
Const xlDelimited = 1
Const xlWorkbookNormal = -4143

Set myXL=CreateObject("Excel.Application")
myXL.Visible=False
myXL.WorkBooks.OpenText "C:\DIR\file.csv", , , xlDelimited, , , , , True 'Change the source path as needed.

myXL.DisplayAlerts=False
myXL.ActiveWorkbook.SaveAs "C:\DIR\new_file.xls", xlWorkbookNormal 'Change the destination path as needed.
myXL.DisplayAlerts=True

myXL.ActiveWorkbook.Close False
myXL.Quit

Set myXL = Nothing

So if anyone needs something similar, here you go! :D

NOTE: This is also for Excel 2000. Don't know how it integrates with newer versions of Excel or newer VB and/or .NET Frameworks. Also, was built on XP Pro SP3, but that shouldn't matter much. Anyway, ENJOY!
"Any answer is only as good as it satisfies the question." - Me

0000000100101011000 -
010010010010000001001100010011110100110 001000000010110010100111101010101