Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'This vba code was written for the article "Excel to csv as utf8 encode directly by Nicolas" (https://www.nicolaslagios.com/2017/09/16/excel-csv-utf8-encode-directly-by-nicolas/)
- 'Note that this code with the tools works also on UNC (network) paths
- Sub saveCSVutf8()
- Application.DisplayAlerts = False
- 'deletes the old temp xls & csv files from previous conversions
- 'do not change this
- Dim aFile As String
- Dim bFile As String
- aFile = ThisWorkbook.Path & "\temp.xls"
- bFile = ThisWorkbook.Path & "\temp_temp.csv"
- If Len(Dir$(aFile)) > 0 Then
- Kill aFile
- End If
- If Len(Dir$(bFile)) > 0 Then
- Kill bFile
- End If
- 'Save a copy of your xlsx or xlsm or whatever as xls 2003
- With ActiveWorkbook.WebOptions
- .RelyOnCSS = True
- .OrganizeInFolder = True
- .UseLongFileNames = True
- .DownloadComponents = False
- .RelyOnVML = False
- .AllowPNG = True
- .ScreenSize = msoScreenSize1024x768
- .PixelsPerInch = 96
- .Encoding = msoEncodingUTF8
- End With
- With Application.DefaultWebOptions
- .SaveHiddenData = True
- .LoadPictures = True
- .UpdateLinksOnSave = True
- .CheckIfOfficeIsHTMLEditor = True
- .AlwaysSaveInDefaultEncoding = False
- .SaveNewWebPagesAsWebArchives = True
- End With
- ChDir ThisWorkbook.Path & "\"
- ActiveWorkbook.SaveAs FileName:= _
- ThisWorkbook.Path & "\temp.xls" _
- , FileFormat:=xlExcel8, Password:="", WriteResPassword:="", _
- ReadOnlyRecommended:=False, CreateBackup:=False
- 're-opens the previous original file
- Workbooks.Open(FileName:= _
- ThisWorkbook.Path & "\YOUR-FILE.xlsm" _ 'CHANGE YOUR-FILE.xlsm WITH WHATEVER THE NAME IS AND THE EXTENSION
- ).RunAutoMacros Which:=xlAutoOpen
- 'THIS STEP IS IMPORTAND, YOU NEED IT TO DELETE ANY UNNECESSARY SHEETS ON YOUR NEW XLS FILE AND LEAVE ONLY ONE THERE
- 'IF YOU WANT TO ACTIVATE THIS STEP JUST, DELETE THE "'" BELOW AND EDIT THE CODE ACCORDING TO YOUR NEEDS
- 'IF YOU DONT KNOW HOW TO EDIT THE CODE BELOW JUST RECORD YOUR STEPS IN A MACRO WITH THE MACRO RECORDER AND ADD YOUR CODE BELOW
- 'Windows("temp.xls").Activate
- 'Sheets(6).Delete 'Delete the last sheet
- 'Sheets(5).Delete 'same
- 'Sheets(4).Delete 'same
- 'Sheets(2).Delete 'Delete the second sheet
- 'Sheets(1).Delete 'Delete the first sheet
- 'Sheets(1).Name = "temp" 'rename the remaining sheet
- 'ActiveWorkbook.Save 'save the document
- 'activates the original xlsx(m) file
- Windows("YOUR-FILE.xlsm").Activate
- 'starting the convertion of xls file to utf8 csv by starting the bat converter
- Shell ThisWorkbook.Path & "\convert.bat"
- Application.Wait (Now + TimeValue("0:00:10"))
- 'replace decimal .0 on csv file
- 'IF YOU DONT WANT TO REPLACE DECIMALS JUST DELETE THE BELOW STEPS
- Dim sBuf As String
- Dim sTemp As String
- Dim iFileNum As Integer
- Dim sFileName As String
- sFileName = ThisWorkbook.Path & "\temp_temp.csv" 'Edit as needed
- iFileNum = FreeFile
- Open sFileName For Input As iFileNum
- Do Until EOF(iFileNum)
- Line Input #iFileNum, sBuf
- sTemp = sTemp & sBuf & vbCrLf
- Loop
- Close iFileNum
- sTemp = Replace(sTemp, ".0""", """")
- iFileNum = FreeFile
- Open sFileName For Output As iFileNum
- Print #iFileNum, sTemp
- Close iFileNum
- 'final opens folder containing your utf8 csv file
- Shell "explorer.exe" & " " & ThisWorkbook.Path & "\", vbNormalFocus
- 'closes the temporary xls file
- Windows("temp.xls").Close
- Application.DisplayAlerts = True
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement