2017-04-13 41 views
0

我已成功使用Access VBA將查詢導出爲.xlsx,並且我使用VBA打開了.xlsx文件,但現在我需要執行「保存作爲「將文件轉換爲.csv或如果可能的話.txt。這是一個包含數千個文件的大型自動化過程的一部分,所以我實在不能有任何手動步驟。我需要從查詢到.txt的過程在Access VBA中完全自動化。這裏是我當前的代碼,它成功地打開我創建的文件:將.xlsx保存爲.csv或.txt從Access VBA

子Export_Reduced_Inforce()

昏暗Dest_Path,Dest_File作爲字符串

昏暗xlApp作爲對象

Dest_Path =「C :\ Inforce_Reduction \結果文件\」 Dest_File = 「測試1」

DoCmd.TransferSpreadsheet acExport,10,_ 「0801_Reduce INFORCE」,Dest_Path & Dest_File,真

設置xlApp =的CreateObject( 「Excel.Application」)

xlApp.Visible =真 xlApp.Workbooks.Open Dest_Path & Dest_File & 「.XLSX」,真,假

End Sub

感謝您的幫助!

回答

1

您可以在此行代碼適應你的需求:

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

XL2 =它的Excel文件瓦納保存它,以便改變與xlApp或者你有聲明一下Excel文件

+0

謝謝!我改編了它: xlApp.Activeworkbook.SaveAs Dest_Path&Dest_File&「.csv」,20 –

+0

很高興我能幫助你! – Ionut

0

好,那麼你可以使用它來打印實際的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 
0

爲防萬一你想擴大你的想法並將數據庫中的所有對象導出到文本文件,運行下面的腳本。

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 
0

這裏是你一個版本。這會將每個查詢的結果導出到每個單獨的文本文件中。

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