简体   繁体   中英

MS Access is ignoring my VBA code to export without headers

I have made a snippet of VBA code which exports two tables from MS Access 2010 into a new xlsx spreadsheet. I manually delete the spreadsheet before I run the code again.

The field names from my tables are exported into the spreadsheet as cell data. Which I DONT want.

In the code same below I've set one export property to true and one to false just to show it is exporting the same way for both. I have tried both false and both true as well.

public sub output

outputFileName as string

outputFileName = "J:\My Documents\testmycode.xlsx"

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Example1 Table", outputFileName, False
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "Example 2 Table", outputFileName, True

end sub

Any help is gratefully recieved!

This argument is not used by TransferSpreadsheet when exporting to EXCEL; it always exports the field names regardless of what you put for this argument in your code or macro. This argument works only for imports.

Maybe this would do it for you.

Public Function DeleteRowInExcel()
On Error GoTo Err_DeleteRowInExcel

    Dim xlApp As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim MyFileName As String

    MyFileName = "c:\temp\MyOutputFile.xls"

    Set xlApp = CreateObject("Excel.Application")
    Set wb = xlApp.Workbooks.Open(MyFileName)
    Set ws = wb.Sheets(1)

    ws.Cells(1, 1).EntireRow.Delete

    wb.Save

Exit_DeleteRowInExcel:
        'Close Excel
    wb.Close savechanges:=False
    xlApp.Quit
    Set xlApp = Nothing
    Set wb = Nothing
    Set ws = Nothing
    Exit Function

Err_DeleteRowInExcel:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_DeleteRowInExcel

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM