2013-04-03 94 views
0

我正在尋找示例「源代碼」來執行此操作,以便我可以創建一個按鈕來自動導出所有表以傳播具有相同名稱但具有.xls擴展名的表格。我已經知道如何手動導出表格。MS Access導出爲.xls

+0

打開宏錄製功能,然後手動進行。 Bam,excel只是爲你製作了一些源代碼。通讀它,找到相關部分並從那裏開始。 – gbtimmon 2013-04-03 21:08:28

+4

由於MS Access中沒有記錄宏功能,請閱讀「DoCmd.TransferSpreadsheet方法」的聯機幫助主題。它包含一個代碼示例。自己嘗試。如果它不起作用,請向我們展示您的代碼,並描述發生的事情與您希望發生的事情。包含任何錯誤消息的完整文本。 – HansUp 2013-04-03 21:11:54

+1

+1對於@HansUp的建議 - 另外,對DAO'TableDefs'集合進行一些研究,因爲這可能是您要獲取要導出的表的列表的地方。 – 2013-04-03 21:22:34

回答

1

我沒有測試過,但是這樣的事情應該導出所有同一工作簿工作...

Dim lTbl As Long 
Dim strFile As String 
Dim d As Database 

'Set current database to a variable adn create a new Excel instance 
Set d = CurrentDb 

strFile = "c:\FolderName\FileName.xls" '## Change to file you want 

'Loop through all tables 
For lTbl = 0 To d.TableDefs.Count 
    'If the table name is a temporary or system table then ignore it 
    If Left(d.TableDefs(lTbl).Name, 1) = "~" Or _ 
     Left(d.TableDefs(lTbl).Name, 4) = "MSYS" Then 
     '~ indicates a temporary table 
     'MSYS indicates a system level table 
    Else 
     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, d.TableDefs(lTbl).Name, strFile 
    End If 
Next lTbl 

'Release database object from memory 
Set d = Nothing 

或者,這對所有的工作簿分開:

Dim lTbl As Long 
Dim strFile As String 
Dim d As Database 

'Set current database to a variable adn create a new Excel instance 
Set d = CurrentDb 
Set xlApp = CreateObject("Excel.Application") 

strFilePath = "c:\Database\" '## Cahnge to where you want to save" 

'Loop through all tables 
For lTbl = 0 To d.TableDefs.Count 
    'If the table name is a temporary or system table then ignore it 
    If Left(d.TableDefs(lTbl).Name, 1) = "~" Or _ 
     Left(d.TableDefs(lTbl).Name, 4) = "MSYS" Then 
     '~ indicates a temporary table 
     'MSYS indicates a system level table 
    Else 
     Set wbExcel = xlApp.workbooks.Add 
     strFile = d.TableDefs(lTbl).Name & ".xls" 
     wbExcel.SaveAs FileName:=strFilePath & strFile, FileFormat:=56 
     wbExcel.Close 
     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, d.TableDefs(lTbl).Name, strFilePath & strFile 
     Set wbExcel = Nothing 
    End If 
Next lTbl 

xlApp.Quit 
Set wbExcel = Nothing 
Set xlApp = Nothing 

'Release database object from memory 
Set d = Nothing 
+0

+1一個很好的答案,一個不好的問題。如果有一個「慈善」徽章,你會贏得它。 – 2013-04-24 07:17:40