使用VBA和宏

时间:2016-04-15 06:34:41

标签: ms-access access-vba spreadsheet export-to-excel savefiledialog

我有两个问题。我希望这两个查询添加到一个Excel(xlsx)文件中,但是在两个不同的表格中,每个查询一个。 这可以通过硬编码路径实现:

Public Function Export2Queries()
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "SaneringsVurdering", "C:\Users\JGJ\Desktop\Sanering.xls", True
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "K_SaneringLedMet", "C:\Users\JGJ\Desktop\Sanering.xls", True
End Function

现在我想将它与SaveAs对话框结合起来。

Option Compare Database
Public Function FilToSave()

Dim FlDia As FileDialog

Set FlDia = Application.FileDialog(msoFileDialogSaveAs)

With FlDia
    .AllowMultiSelect = False
    .InitialFileName = "C:\"  ' You can set outfile to a full path with a fictitious  or real file name, and the dialog will open in that folder.
    .Title = "Please name the file you want to save"
    If .Show = True Then
        FilName = .SelectedItems(1)


    Else
        MsgBox "No file selected. Process cancelled"
        DoCmd.Hourglass False
        FilToSave = "Cancelled"
        Exit Function
        End If
End With

FilToSave = FilName

End Function

使用上述解决方案。

Public Function Export2Queries()
Dim savefile As String
savefile = FilToSave
If savefile <> "Cancelled" Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "SaneringsVurdering", savefile, True
End If
End Function

现在,此代码适用于使用对话框提示将查询/表导出到想要的位置。

0 个答案:

没有答案