将 MS Access 指向 VBA 中的可变路径以导出到 Excel
Pointing MS Access to a variable path in VBA for exporting to Excel
我有一个经常处理的项目,数据来自 Access,我需要导出到 Excel。在几年前我的公司升级到 Windows 2010 之前,以下代码一直有效。发生的事情是我将指向我想要的子目录(例如 P:\project\evaluation\output),它会向上保存一个子目录(例如 P:\project\evaluation)。
代码:
Sub ExporttoXL()
Dim response, today
exportdir = fncOpenFolder()
today = Format(Date, "mmddyy")
response = InputBox("What is the date for the title of the output file? (Recommend: mmddyy format)", "Output file date for name", today)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query001", "Output-" & response & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query002", "Output-" & response & ".xls"
End Sub
----------------
Public Function fncOpenFolder() As String
Dim fdlg As Object
Set fdlg = Application.FileDialog(4) 'msoFileDialogFolderPicker
With fdlg
.AllowMultiSelect = False
.Title = "Select Folder"
If .Show = -1 Then
fncOpenFolder = .SelectedItems(1)
Else
fncOpenFolder = ""
End If
End With
Set fdlg = Nothing
End Function
TransferSpreadsheet
的 FileName 参数应该是 "the file name and path of the spreadsheet you want to import from, export to, or link to." 但是你的代码只给出了它没有路径的文件名。 exportdir 变量在您从 fncOpenFolder() 中给它一个值后不会被使用。
修改代码并使用 exportdir 将包含要作为导出目标的工作簿文件名的路径...
Dim strFullPath As String
strFullPath = exportdir & "\Output-" & response & ".xls"
Debug.Print strFullPath '<- view this in Immediate window; Ctrl+g will take you there
'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
' "Query001", "Output-" & response & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query001", strFullPath
我有一个经常处理的项目,数据来自 Access,我需要导出到 Excel。在几年前我的公司升级到 Windows 2010 之前,以下代码一直有效。发生的事情是我将指向我想要的子目录(例如 P:\project\evaluation\output),它会向上保存一个子目录(例如 P:\project\evaluation)。
代码:
Sub ExporttoXL()
Dim response, today
exportdir = fncOpenFolder()
today = Format(Date, "mmddyy")
response = InputBox("What is the date for the title of the output file? (Recommend: mmddyy format)", "Output file date for name", today)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query001", "Output-" & response & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query002", "Output-" & response & ".xls"
End Sub
----------------
Public Function fncOpenFolder() As String
Dim fdlg As Object
Set fdlg = Application.FileDialog(4) 'msoFileDialogFolderPicker
With fdlg
.AllowMultiSelect = False
.Title = "Select Folder"
If .Show = -1 Then
fncOpenFolder = .SelectedItems(1)
Else
fncOpenFolder = ""
End If
End With
Set fdlg = Nothing
End Function
TransferSpreadsheet
的 FileName 参数应该是 "the file name and path of the spreadsheet you want to import from, export to, or link to." 但是你的代码只给出了它没有路径的文件名。 exportdir 变量在您从 fncOpenFolder() 中给它一个值后不会被使用。
修改代码并使用 exportdir 将包含要作为导出目标的工作簿文件名的路径...
Dim strFullPath As String
strFullPath = exportdir & "\Output-" & response & ".xls"
Debug.Print strFullPath '<- view this in Immediate window; Ctrl+g will take you there
'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
' "Query001", "Output-" & response & ".xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query001", strFullPath