How to save as .txt in vba

I am looking for my macro to save a new sheet that I created as a .txt file. this is the code that i still have.

Sub Move() ' ' Move Macro ' ' Keyboard Shortcut: Ctrl+m ' Sheets("Sheet1").Select Range("A1").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select Selection.Copy Workbooks.Add ActiveSheet.Paste ActiveWorkbook.SaveAs Filename:="e:" & _ "HDR" + Format(Now(), "YYYYMMDDhhmmss") & ".txt" End Sub 

This includes my macro. I am having problems with the last part, where it is saved as a .txt file.
I am currently getting a bunch of crap in my .txt file, here is an example,
"PK !!" - U {Š [Content_Types] .xml ¢ (ÌTÝNÂ0¾7-Þš € ‰ 1 † Á ... â ¥ PÚ3¶ÐμMOÁñöž • Ÿ¨ ".
Any help would be great.

+5
source share
2 answers

Manually changing the file name extension does not actually change the file type. The SaveAs method takes a file type argument. The code you want

 ActiveWorkbook.SaveAs Filename:="e:" & "HDR" + Format(Now(), "YYYYMMDDhhmmss") _ & ".txt", FileFormat:= xlTextWindows 

Performing a search from Excel Help for XlFileFormat will allow you to get an almost complete list of possible file formats, including 6 text formats and 4 CSV formats.

+4
source

Adding txt to the name does not automatically encode the word document into text format.

Try instead

 ActiveWorkbook.SaveAs Filename:="e:" & _ "HDR" + Format(Now(), "YYYYMMDDhhmmss") & ".txt", FileFormat:=wdFormatText, Encoding:=1252 
+2
source

Source: https://habr.com/ru/post/1203824/


All Articles