1
我有一個代碼可以創建特定格式的工作表,然後將其保存爲文本文件。我一直使用Sheet.SaveAs,然後以不同的方式命名文件。有沒有更強大的方式來保存文件並移動它們?我當前的代碼運行如下:如何將每個單獨的工作表保存爲一個txt文件
OldPath = ThisWorkbook.Path & "\" ' current path to this workbook
OldFile = OldPath & ShtName & ".txt" ' location of file upon creation
NewPath = OldPath & FldName & "\" ' path for the folder where file will be moved
NewFile = NewPath & ShtName & ".txt" ' location of file after moving
'[3] CREATE INPUT FILES
ThisWorkbook.Sheets(ShtName).SaveAs OldFile, FileFormat:=xlTextWindows
ThisWorkbook.SaveAs OldPath & ThisFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
If Len(Dir(NewPath, vbDirectory)) <> 0 And NewPath <> "" Then 'MOVE FILES TO A FOLDER
Else
MkDir NewPath ' create folder for input files to be moved if not yet created
End If
If Len(Dir(NewFile)) <> 0 Then
' delete an old version of file if it is already in folder
SetAttr NewFile, vbNormal
Kill NewFile
End If
Name OldFile As NewFile
這種方法感覺很麻煩,但我不希望有訴諸使用殼牌,因爲我覺得這將是不太可靠的,除非有人建議來代替。
我喜歡的文件中的溶液很多,唯一的問題是我正在爲同事設計一個模板,我希望當我傳遞文件時儘可能少地出現兼容性問題。有沒有完成同樣事情的內部方式? – teepee
假設您不想要求用戶添加打印機,更新代碼以使用文件生成代碼 –