2016-01-06 84 views
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 

这种方法感觉很麻烦,但我不希望有诉诸使用壳牌,因为我觉得这将是不太可靠的,除非有人建议来代替。

回答

1

您可以使用一个通用的文本打印机,以及PrintOut方法来实现这一

首先,如果你没有带已经添加一个通用文本打印机

  1. Add Printer对话框中,选择File端口
  2. 选择Generic然后Generic/Text Only
  3. 它命名为您希望

此代码发送每个工作表到这台打印机

Sub SaveWorkbookAsText(wb As Workbook, Optional FldName As String = vbNullString) 
    Dim NewPath As String 
    Dim GenericTextOnlyPrinter As String 
    Dim ws As Worksheet 

    '<~~~ Change this string to match your Generic Text Only Printer Name 
    GenericTextOnlyPrinter = "Text Only (File)" 

    NewPath = ThisWorkbook.Path & Application.PathSeparator 

    If FldName <> vbNullString Then 
     NewPath = NewPath & FldName 
     If Right$(NewPath, 1) <> Application.PathSeparator Then 
      NewPath = NewPath & Application.PathSeparator 
     End If 
    End If 

    For Each ws In wb.Worksheets 
     ws.PrintOut _ 
      ActivePrinter:=GenericTextOnlyPrinter, _ 
      PrintToFile:=True, _ 
      PrToFileName:=NewPath & ws.Name & ".txt", _ 
      IgnorePrintAreas:=True 
    Next 
End Sub 

或者,而不取决于在打印机上,生成在代码

Sub SaveWorkbookAsText(wb As Workbook, Optional FldName As String = vbNullString) 
    Dim NewPath As String 
    Dim ws As Worksheet 
    Dim dat As Variant 
    Dim rw As Long, cl As Long 
    Dim FileNum As Integer 
    Dim Line As String 

    NewPath = ThisWorkbook.Path & Application.PathSeparator 

    If FldName <> vbNullString Then 
     NewPath = NewPath & FldName 
     If Right$(NewPath, 1) <> Application.PathSeparator Then 
      NewPath = NewPath & Application.PathSeparator 
     End If 
    End If 

    For Each ws In wb.Worksheets 
     FileNum = FreeFile 
     Open NewPath & ws.Name & ".txt" For Output As #FileNum ' creates the file 

     dat = ws.UsedRange.Value 
     ' in case the sheet contains only 0 or 1 cells 
     If TypeName(dat) <> "Variant()" Then 
      dat = ws.UsedRange.Resize(, 2) 
     End If 

     For rw = 1 To UBound(dat, 1) 
      Line = vbNullString 
      For cl = 1 To UBound(dat, 2) - 1 
       Line = Line & dat(rw, cl) & vbTab 
      Next 
      Print #FileNum, Line & dat(rw, cl) 
     Next 
     Close #FileNum 
    Next 
End Sub 
+0

我喜欢的文件中的溶液很多,唯一的问题是我正在为同事设计一个模板,我希望当我传递文件时尽可能少地出现兼容性问题。有没有完成同样事情的内部方式? – teepee

+0

假设您不想要求用户添加打印机,更新代码以使用文件生成代码 –

相关问题