Converting already opened XLS to CSV - excel

I have VBscript code which opens XLS file and export it to CSV, see below:
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open("test.xls")
oBook.SaveAs "test.csv", 6
oBook.Close False
oExcel.Quit
It works fine but I need to do the same - convert XLS to CSV with already opened file "test.xls" on my computer which is different (edited by me) versus saved version. Is that possible?

Yes, you just need to connect the existing instance of Excel and set workbook object variable equal to the already open workbook instead:
Set oExcel = GetObject(, "Excel.Application")
Set oBook = oExcel.Workbooks("test.xls")

Related

Workbooks.Open refers to XLA instead of XLSM in VBScript

I am trying to open .xlsm file in VBScript. I also have one 3rd party add-in installed (i.e. Quality.xla file which is automatically loaded when I open my Sales file. I want that add-in (Quality.xla) and unloading the add-in is not a solution.
When I execute the below code
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.DisplayAlerts = False
xlApp.AlertBeforeOverwriting = False
Set xlBook = xlApp.Workbooks.Open("D:\Sales.xlsm")
WScript.Echo xlBook.Name
and check for
xlBook.Name
it returns name of the Add-in instead of Sales.xlsm file. Is there anyway I can still get xlbook to refer to Sales.xlsm as I need to refer to other sheets in that Excel workbook.
Thanks
I can't see anything wrong with your code. I can't recreate the issue either. There's no logic to the script returning the addin's name.
On the off-chance though that this is a real issue, I came up with the following workaround for it which should produce the right result :
Dim xlApp, xlBook, xlWb
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.DisplayAlerts = False
xlApp.AlertBeforeOverwriting = False
xlApp.Workbooks.Open ("D:\Sales.xlsm")
For Each xlWb In xlApp.Workbooks
If xlWb.Name = "Sales.xlsm" Then Set xlBook = xlWb
Next
WScript.Echo xlBook.Name
That said, I really wouldn't recommend using it unless you've absolutely no other choice. Perhaps just use it to help you debug the issue you're seeing.

Numbers get converted to strings while xlsx to csv conversion using vbs

Hi i am trying to convert .xlsx file to .csv file and then read this csv file in python. Below is my code in VBS:
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
This code is working properly. but when i read converted csv file in python the numbers get converted to string. example:
$ 266,74245.4545 -> '$266,74245'
Is there any way i can make changes in vbs code to get these numbers as it is in csv file. for my application i require whole number while reading.
I took above code from "https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line"
I found solution to this question. I'm changing the currency($) format to a Numbers format in the opened worksheet, and then converting it to CSV.
This how the code looks:
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.ActiveSheet.Range(WScript.Arguments.Item(2)).Select
oExcel.Selection.NumberFormat = "0000000000.00000"
oBook.SaveAs WScript.Arguments.Item(1), 6

VBScript to remove empty rows from csv

I am brand new to VBScripts and trying to use a script I found on SO. The script will convert an XLSX file to a CSV. That is working great but the CSV has empty rows and it is causing me some headaches.
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
How can I modify the above script to remove empty rows before saving as CSV?
I read this would do it but it does not seem to work and throws a runtime error
oBook.UsedRange

vbs script protected view

I have a set of excel files that open in protected view. I would like to open these excel files, remove the protected view, then save them as tab delimited text files. So far, I have been able to do everything but when I run the script, a prompt comes up asking if the user would like to save the excel file. I want to get rid of this prompt to make the script fully automatic. Here is the current code:
format = -4158
Set objFSO = CreateObject("Scripting.FileSystemObject")
src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
oExcel.ProtectedViewWindows.Open(src_file)
oExcel.ActiveProtectedViewWindow.Edit
oExcel.Quit
Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.Worksheets(5).Activate
oBook.SaveAs dest_file, format
oBook.Close False
oExcel.Quit
I have found solutions to removing the prompt online, but because I have to edit the protected view window, I could not find a way to use the solutions. How can I make it so that this VB script removes the protected view, and saves the excel file as a tab delimited text file? It should do this without showing any windows or prompts, i.e., it should be automatic.
To simply have Excel ignore message boxes in general it is like this:
oExcel.DisplayAlerts = False
'your code here to close workbook
oExcel.DisplayAlerts = True

Transform a couple of xlsx to csv

on my windows pc I have a folder with a couple of csv files but sometimes there is a xlsx file between.
Later I want to copy all csv files into one, so that I can load it into a DB.
But for that I need to transform the xlsx files also into csv. I do not want to open all separately. Is there a way to do it automatically? I tried to create a macro in Excel but I didn't know how to apply that to all xlsx files.
Thanks for your help!
try the FileSystemObject approach
strPath = "C:\PATH_TO_YOUR_FOLDER"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder (strPath)
For Each objFile In objFolder.Files
If objFso.GetExtensionName (objFile.Path) = "xls" Then
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
' Include your code to work with the Excel object here
objWorkbook.Close True 'Save changes
End If
Next
objExcel.Quit
that should get u started.
Remember, after version 2003, Excel's Application.FileSearch is deprecated, so FileSystemObject approach is better

Resources