【问题标题】:How to save .xlsx to .csv or .txt from Access VBA?如何从 Access VBA 将 .xlsx 保存为 .csv 或 .txt?
【发布时间】:2017-09-09 15:31:30
【问题描述】:

我已成功使用 Access VBA 将查询导出到 .xlsx,并且我已使用 VBA 打开 .xlsx 文件,但现在我需要执行“另存为”以将文件转换为 .csv 或者,如果可能的话,.txt。这是一个包含数千个文件的大型自动化过程的一部分,所以我真的没有任何手动步骤。我需要从查询到 .txt 的过程在 Access VBA 中完全自动化。这是我当前的代码,它成功打开了我创建的文件:

Sub Export_Reduced_Inforce()

    Dim Dest_Path, Dest_File As String

    Dim xlApp As Object

    Dest_Path = "C:\Inforce_Reduction\Result Files\"
    Dest_File = "Test1"

    DoCmd.TransferSpreadsheet acExport, 10, _
     "0801_Reduce Inforce", Dest_Path & Dest_File, True

    Set xlApp = CreateObject("Excel.Application")

    xlApp.Visible = True
    xlApp.Workbooks.Open Dest_Path & Dest_File & ".XLSX", True, False

End Sub

【问题讨论】:

    标签: excel vba csv ms-access


    【解决方案1】:

    你可以根据你的需要调整这行代码:

    xl2.ActiveWorkbook.SaveAs ThisWorkbook.Path & "/" & "name your file" & ".csv"
    

    xl2= 这是您想要保存的 excel 文件,因此请使用 xlApp 或您声明的 excel 文件进行更改

    【讨论】:

    • 谢谢!我对其进行了调整:xlApp.Activeworkbook.SaveAs Dest_Path & Dest_File & ".csv", 20
    • 很高兴能帮到你!
    【解决方案2】:

    好的,你可以使用它来打印实际的 SQL。

    Private Sub Command2_Click()
    
    Dim db As Database
    Dim qr As QueryDef
    
    Set db = CurrentDb
    
    For Each qr In db.QueryDefs
      TextOut (qr.Name)
      TextOut (qr.SQL)
      TextOut (String(100, "-"))
    Next
    
    End Sub
    
    Public Sub TextOut(OutputString As String)
    
        Dim fh As Long
    
        fh = FreeFile
        Open "C:\Users\rs17746\Desktop\Text_Files\sample.txt" For Append As fh
        Print #fh, OutputString
        Close fh
    
    End Sub
    

    【讨论】:

      【解决方案3】:

      如果您想扩展您的想法并将数据库中的所有对象导出到文本文件,请运行以下脚本。

      Private Sub Command4_Click()
      
      
      On Error GoTo Err_ExportDatabaseObjects
      
          Dim db As Database
          'Dim db As DAO.Database
          Dim td As TableDef
          Dim d As Document
          Dim c As Container
          Dim i As Integer
          Dim sExportLocation As String
      
          Set db = CurrentDb()
      
          sExportLocation = "C:\Users\rs17746\Desktop\Text_Files\" 'Do not forget the closing back slash! ie: C:\Temp\
      
          For Each td In db.TableDefs 'Tables
              If Left(td.Name, 4) <> "MSys" Then
                  DoCmd.TransferText acExportDelim, , td.Name, sExportLocation & "Table_" & td.Name & ".txt", True
              End If
          Next td
      
          Set c = db.Containers("Forms")
          For Each d In c.Documents
              Application.SaveAsText acForm, d.Name, sExportLocation & "Form_" & d.Name & ".txt"
          Next d
      
          Set c = db.Containers("Reports")
          For Each d In c.Documents
              Application.SaveAsText acReport, d.Name, sExportLocation & "Report_" & d.Name & ".txt"
          Next d
      
          Set c = db.Containers("Scripts")
          For Each d In c.Documents
              Application.SaveAsText acMacro, d.Name, sExportLocation & "Macro_" & d.Name & ".txt"
          Next d
      
          Set c = db.Containers("Modules")
          For Each d In c.Documents
              Application.SaveAsText acModule, d.Name, sExportLocation & "Module_" & d.Name & ".txt"
          Next d
      
          For i = 0 To db.QueryDefs.Count - 1
              Application.SaveAsText acQuery, db.QueryDefs(i).Name, sExportLocation & "Query_" & db.QueryDefs(i).Name & ".txt"
          Next i
      
          Set db = Nothing
          Set c = Nothing
      
          MsgBox "All database objects have been exported as a text file to " & sExportLocation, vbInformation
      
      Exit_ExportDatabaseObjects:
          Exit Sub
      
      Err_ExportDatabaseObjects:
          MsgBox Err.Number & " - " & Err.Description
          Resume Exit_ExportDatabaseObjects
      
      End Sub
      

      【讨论】:

        【解决方案4】:

        这里还有一个版本供您使用。这会将每个查询的结果导出到一个单独的文本文件中。

        Private Sub Command0_Click()
        
        
        Dim qdf As QueryDef
        Dim strFileName As String
        For Each qdf In CurrentDb.QueryDefs
        If Left(qdf.Name, 1) <> "~" Then
        
        'you need to figure out TransferText command. Maybe
        'you won't be lazy and expect people to read it to
        'you and tutor you on how it works.
        strFileName = qdf.Name
        
        'Docmd.TransferText ....
        DoCmd.TransferText transferType:=acExportDelim, TableName:=strFileName, FileName:="C:\test\" & strFileName & ".txt", hasfieldnames:=True
        
        End If
        Next qdf
        MsgBox "Done"
        
        
        End Sub
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-03
          相关资源
          最近更新 更多