Jump to content

Help with Excel "Save as CSV"


Recommended Posts

I'm trying to open an excel file and save as CSV and I'm having trouble. Below is what I have:


          

 Local $oExcel, $oWorkbook   
            $oExcel = _Excel_Open()
            If @error Then Exit MsgBox($MB_SYSTEMMODAL, "_Excel_Open Error", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

            $oWorkbook = _Excel_BookOpen($oExcel, "C:\Users\think\Desktop\Update.xls")

            Switch @error
                Case 1
                        Exit MsgBox($MB_SYSTEMMODAL, "_Excel_BookOpen Error 1", "$oExcel is not an object or not an application object")
                Case 2
                        Exit MsgBox($MB_SYSTEMMODAL, "_Excel_BookOpen Error 2", "Specified $sFilePath does not exist")
                Case 3
                        Exit MsgBox($MB_SYSTEMMODAL, "_Excel_BookOpen Error 3", "Unable to open $sFilePath. @extended is set to the COM error code returned by the Open method")
            EndSwitch          


            ;Save as CSV
            $oWorkbook = _Excel_BookSaveAs("C:\Users\think\Desktop\Update.csv", "csv", 0, 1)

 

Excel opens the workbook but does not save it as a CSV. What am I doing wrong?

Edited by Jadog
Link to post
Share on other sites
On 9/10/2021 at 12:58 PM, Jadog said:
            ;Save as CSV
            $oWorkbook = _Excel_BookSaveAs("C:\Users\think\Desktop\Update.csv", "csv", 0, 1)

What am I doing wrong?

The first parameter is the Workbook object not the Path and filename of the file to be saved to . As I already wrote to you in thread  unable-to-open-excel-file  , you should pay more attention to Function parameters and Return Values ;).

#include <Excel.au3>
#include <MsgBoxConstants.au3>

Local $oExcel, $oWorkbook
$oExcel = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "_Excel_Open Error", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

$oWorkbook = _Excel_BookOpen($oExcel, "C:\Users\think\Desktop\Update.xls")

Switch @error
    Case 1
        Exit MsgBox($MB_SYSTEMMODAL, "_Excel_BookOpen Error 1", "$oExcel is not an object or not an application object")
    Case 2
        Exit MsgBox($MB_SYSTEMMODAL, "_Excel_BookOpen Error 2", "Specified $sFilePath does not exist")
    Case 3
        Exit MsgBox($MB_SYSTEMMODAL, "_Excel_BookOpen Error 3", "Unable to open $sFilePath. @extended is set to the COM error code returned by the Open method")
EndSwitch


;Save as CSV
Local $sWorkbook = "C:\Users\think\Desktop\Update.csv"
; Info : XlFileFormat enumeration -> $xlCSV or $xlCSVWindows ?
_Excel_BookSaveAs($oWorkbook, $sWorkbook, $xlCSV, True) ; True = overwrites an existing file
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookSaveAs", "Error saving workbook to '" & $sWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
   

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...