Skip to content

Instantly share code, notes, and snippets.

@joli80
Created August 3, 2016 12:18
Show Gist options
  • Save joli80/010609c5ec84787cfe3e9db9dce71a7e to your computer and use it in GitHub Desktop.
Save joli80/010609c5ec84787cfe3e9db9dce71a7e to your computer and use it in GitHub Desktop.
VB script for creating a csv file from an Excel file
Dim objFSO, currentDirectory, objFolder, colFiles
Set objFSO = CreateObject("scripting.filesystemobject")
currentDirectory = objFSO.GetAbsolutePathName(".")
Set objFolder = objFSO.GetFolder(currentDirectory)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = "XLSX" Or UCase(objFSO.GetExtensionName(objFile.name)) = "XLS" Then
Wscript.Echo "Exporting " & objFile.Name
Call Writefile(objFile.Path)
Exit For
End If
Next
Set objFSO = Nothing
Sub Writefile(ByVal strFilename)
Dim objExcel
Dim objWB
Dim objws
Set objExcel = CreateObject("Excel.Application")
Set objWB = objExcel.Workbooks.Open(strFilename)
For Each objws In objWB.Sheets
objws.Copy
objExcel.ActiveWorkbook.SaveAs objWB.Path & "\" & objws.Name & ".csv", 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
objExcel.ActiveWorkbook.Close False
Next
objWB.Close False
objExcel.Quit
Set objExcel = Nothing
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment