简体   繁体   中英

VBA: convert xls to csv and save it in a certain path

Please help me. I'm trying to convert a .xls/.xlsx placed on a certain path on a server (let's say one\\test\\1.xls) file to .csv (without damageing the date format or any other info) and save it in a fixed path (let's say one\\result\\1.csv). This process must repeat itself automatically every 24 hours.

Thank you!

So far I only got the code below that only converts to csv and saves it in the same location as the original file.
Also for some unknown reason it converts HALF of the dates to 05/31/2017 for exemple (and the original format is 31.05.2017).

Sub ConvertXLSToCSV()

    Dim InputPath As String

    Dim PowershellCommand As String



    ' To suppress excel prompts and alert messages while the macro is running.

    Application.DisplayAlerts = False

    Do

        ' Taking the input excel file path which needs to be converted to .csv format.

        InputPath = InputBox("Enter the full path of the input excel file.", "Convert XLS to CSV")

        If Trim(InputPath) <> "" Then

            If Dir(InputPath) = vbNullString Then

                MsgBox "File: '" & InputPath & "' doesn't exists.", vbOKOnly + vbCritical, "Convert XLS to CSV"

            ElseIf Split(Dir(InputPath), ".")(1) = "xlsx" Or Split(Dir(InputPath), ".")(1) = "xls" Then

                InputPath = Trim(InputPath)

                ' Opening the input excel file and saving it in .csv using powershell.

                PowershellCommand = "$ExcelWB = new-object -comobject excel.application" & vbNewLine

                PowershellCommand = PowershellCommand & "$ExcelWB = new-object -comobject excel.application" & vbNewLine

                PowershellCommand = PowershellCommand & "$ExcelWB.Visible = $false" & vbNewLine & "$ExcelWB.DisplayAlerts = $false" & vbNewLine

                PowershellCommand = PowershellCommand & "$Workbook = $ExcelWB.Workbooks.Open('" & InputPath & "')" & vbNewLine

                PowershellCommand = PowershellCommand & "$Workbook.SaveAs('" & Left(InputPath, (InStrRev(InputPath, ".", -1, vbTextCompare) - 1)) & ".csv',6)" & vbNewLine

                PowershellCommand = PowershellCommand & "$Workbook.Close($false)" & vbNewLine

                PowershellCommand = PowershellCommand & "$ExcelWB.quit()" & vbNewLine

                PowershellCommand = "Powershell.exe -command " & PowershellCommand

                Set WshShell = CreateObject("WScript.Shell")

                WshShell.Exec (PowershellCommand)

                Exit Do

            Else:

                MsgBox "Input file is not in excel (.xlsx/.xls) format.", vbOKOnly + vbCritical, "Convert XLS to CSV"

            End If

        ElseIf StrPtr(InputPath) <> 0 Then MsgBox "Please enter the full path of the input excel file.", vbOKOnly + vbExclamation, "Convert XLS to CSV"

        End If

    Loop Until StrPtr(InputPath) = 0

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM