2013-04-03 78 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