VBA:从.xls切换到.xlsx文件格式

时间:2018-01-03 17:17:23

标签: vba excel-vba shell access-vba excel

我正在处理最初编码为输出.xls Excel文件的数据库。我们需要将其更改为.xlsx,但我无法弄明白。我试过简单地将“.xls”更改为“.xlsx”,但这不起作用。它在Set wbk= XLapp.Workbooks.Open(SavePath).Sheets(1)行生成错误 - 至少是调试器突出显示的错误。这是当前的代码部分:

Private Sub ExcelFeederReview_Click()
Dim SavePath As String
SavePath = fGetMyDocsPath & "\FeederReview_" & Format(Now, "YYYYMMDD_hhnn") & ".xls"
'Run Query and export to Savepath
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qFeederExport", SavePath, False, "FeederReview"

'Open query in excel shell
Set XLapp = New Excel.Application
Set wbk = XLapp.Workbooks.Open(SavePath).Sheets(1)

'Format excel doc with excel shell
    With XLapp
            .Application.DisplayAlerts = False
            .Application.Rows("1:1").Select
            .Application.Selection.Font.Bold = True
            .Application.Selection.Orientation = 90
            .Application.Selection.Interior.ColorIndex = 15
            .Application.Range("A2").Select
            .Application.ActiveWindow.FreezePanes = True
            .Application.Range("A1").Select
            .Application.Selection.RowHeight = 86
            .Application.Columns("D:D").Select
            .Application.Selection.NumberFormat = "###0.00"
            .Application.Cells.Select
            .Application.Selection.Font.Name = "Arial"
            .Application.Selection.Columns.AutoFit
            .Application.Range("A1").Select
            .Application.ActiveWorkbook.SaveAs FileName:=SavePath
            .Application.ActiveWorkbook.Close
            .Quit
    End With

'Allows user to view and open file
    OpenFileDiag = MsgBox("Feeder review file saved to the the following location:" & vbCrLf & vbCrLf & SavePath & vbCrLf & vbCrLf & "Would you like to view Saved File?", vbYesNo, "View Saved File?")

'Yes selection opens saved formated excel file
    If OpenFileDiag = vbYes Then

        Set XLAppOpenToView = New Excel.Application
            XLAppOpenToView.Visible = True
            XLAppOpenToView.Workbooks.Open SavePath

'No selection unloads excel shell
    ElseIf OpenFileDiag = vbNo Then

            XLapp.Quit
            Set XLapp = Nothing
            Set XLsheet = Nothing

        Exit Sub

    End If
'Unloading of excel shell to prevent multiple open excel instances
    XLapp.Quit
    Set XLapp = Nothing
    Set XLsheet = Nothing

End Sub

2 个答案:

答案 0 :(得分:2)

将常量size替换为acSpreadsheetTypeExcel9

答案 1 :(得分:0)

尝试将acSpreadsheetTypeExcel9更改为acSpreadsheetTypeExcel12Xml