Ulquiorra Schiffer
Ulquiorra Schiffer

Reputation: 89

VBA Excel save txt as txt UTF-8

I got all of my code working, but this saves the txt in ANSI, would like to save txt as UTF-8, could some help me please with this?

Option Explicit

Sub SaveWorkSheetAsCSV()
    
    ActiveSheet.Buttons.Delete
    
    Dim FolderPath As String
    FolderPath = "C:\Users\" & Environ("USERNAME") & "\Test"
    
    Dim FileName As String: FileName = Format(Now, "yyyymmdd-hh.mm ") & " Test"
    
    Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets(4)
    
    Application.ScreenUpdating = False
    
    sws.Copy
    Dim dwb As Workbook: Set dwb = ActiveWorkbook
    
    Application.DisplayAlerts = False
    dwb.SaveAs FolderPath & "\" & FileName & ".txt", xlCSV, Local:=True
    Application.DisplayAlerts = True
    dwb.Close SaveChanges:=False
    
    Application.ScreenUpdating = True
    
    ThisWorkbook.FollowHyperlink FolderPath

End Sub

Upvotes: 0

Views: 609

Answers (1)

pgSystemTester
pgSystemTester

Reputation: 9917

You've got a couple options to modify your SaveAs

Try any of these"

'My best guess that will work
dwb.SaveAs FolderPath & "\" & FileName, xlCurrentPlatformText, Local:=True

'Alternative text
dwb.SaveAs FolderPath & "\" & FileName, xlUnicodeText, Local:=True

'CSV approach
dwb.SaveAs FolderPath & "\" & FileName, xlCSVUTF8, Local:=True

Upvotes: 1

Related Questions