【问题标题】:vba - export Access table to file named by uservba - 将访问表导出到用户命名的文件
【发布时间】:2020-07-13 20:37:58
【问题描述】:

我目前正在使用 TransferSpreadsheet 将 Access 2013 中的表格导出到 Excel 文件。我设置了默认文件名和位置。它工作正常,除了当用户更改他们想要将文件保存为“另存为”对话框中的名称时,不会使用该名称保存。有没有办法获取用户在“另存为”对话框中输入的文件名,并将该文件名保存在他们选择的位置?

这是我现在正在做的事情:

Dim strTableName As String
Dim strBasePath As String
Dim strFullPath As String
Dim strFileName As String
Dim dlgSaveAs As Object
Const msoFileDialogSaveAs = 2

With CodeContextObject
    strTableName = "New_Rules"
    strBasePath = "C:\Users\" & Environ("USERNAME") & "\Documents\"
    strFileName = "New_Account_Rules_" & Format(Date, "yyyy-mm-dd")
    strFullPath = strBasePath & strFileName & ".xls"
    ' Display the Save As dialog with a default name and path
    Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs)
    With dlgSaveAs
        .InitialFileName = strFullPath
        If dlgSaveAs.Show Then
            ' Do the export
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "New_Rules", strFullPath, True
        End If
    End With

提前致谢。

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    SelectedItems() 集合包含输入/选择的文件名列表。由于您使用的是 msoFileDialogSaveAs 选项,FileDialog 将只允许一个选定的项目。因此,当 .Show 为 True 时,只需将 .SelectedItems(1) 分配给您的 strFullPath 变量:

    With dlgSaveAs
    
        ' Set the initial/default filename...
        .InitialFileName = strFullPath
    
        If .Show Then
    
            ' Get the selected/entered filename...
            strFullPath = .SelectedItems(1)
    
            ' Do the export...
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "New_Rules", strFullPath, True
    
        End If
    End With
    

    【讨论】:

    • 太棒了!谢谢!那行得通。我知道必须有一个简单的方法来做到这一点。
    猜你喜欢
    • 2015-04-26
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多