编辑:没关系,我找到了办法。我将把我的答案作为一个单独的帖子发布。
我有一个包含多个记录的连续表单。我希望能够将表单上的所有记录信息导出到名为“RawData”的Excel工作表中,其中Excel文件位于某个静态位置。
我尝试了Docmd.OutputTo,但用一张包含表单记录的表替换了所有内容,而Docmd.TransferSpreadsheet似乎只导入单元格A1中的文本“F1”(不知道为什么)。也许我使用它们错了?
答案 0 :(得分:0)
您基本上必须打开Excel并逐个填写每个单元格。或者这是我知道如何做到这一点的唯一方法......原始代码来自here,但我修改了它,因为原始代码只导出一条记录。
Private Sub btnExportExcel_Click()
Dim appExcel As Object
Dim wkbWorkBook As Object
Dim wksSheet As Object
' creates Excel application
Set appExcel = CreateObject("Excel.Application")
' opens workbook for output
Set wkbWorkBook = appExcel.Workbooks.Open("C:\Users\mikec\Desktop\Book1.xlsx")
' get reference to sheet for output
Set wksSheet = wkbWorkBook.Worksheets("RawData")
' for each record, output all fields in the record
Dim rs As Recordset
Dim row As Integer: row = 1
Dim col As Integer: col = 1
Set rs = Me.Form.Recordset
If rs.RecordCount > 0 Then
rs.MoveFirst
Do While Not rs.EOF
Dim fld As Field
For Each fld In Me.Recordset.Fields
wksSheet.Cells(row, col) = fld
col = col + 1
Next fld
rs.MoveNext
col = 1
row = row + 1
Loop
End If
' save and quit
wkbWorkBook.Save
appExcel.Quit
Set wksSheet = Nothing
Set wkbWorkBook = Nothing
Set appExcel = Nothing
End Sub