从Access 2007脚本保存自定义导出

时间:2013-04-01 19:59:09

标签: vba ms-access ms-access-2007 access-vba

您好,感谢您查看我的问题。我试图让一个脚本工作从Access 2007数据库导出一组excel电子表格。我有导出和其他功能很好,这是导致我问题的保存功能。我当前的实现是一个消息框,显示给用户询问文件夹名称。这允许用户输入现有文件夹名称以保存到该文件夹​​,但前提是该文件夹已存在且位于“我的文档”目录中。对我来说,这是功能性的,但不完全是我希望我的客户被迫处理的。有没有办法让一个简单的“另存为”或其他默认保存文件对话框出现?我有一个导入脚本工作在另一端有默认文件打开选择器工作,我错过了什么?脚本如下:

Private Sub btnExport_Click()
On Error GoTo Err_Command38_Click

Dim strPath As String

strPath = InputBox("Enter an existing folder name, or enter a file path.", "Export")       
Beep
MsgBox "Report will now be exported to the " & strPath & " folder in My Documents"
Dim strDocName As String
strDocName = "DatabaseExport" + Date$ + ".xlsx"   'Enter your filename here

DoCmd.SetWarnings False
DoCmd.TransferSpreadsheet acExport, 10, "tblBenefit", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblBenefitDispensation", strPath + "/" +     strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblCourse", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblCourseEnrollment", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblDistinguishedStudent", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblEvent", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblEventFacultyAttendee", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblEventPresenter", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblEventsUniversityParticipant", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblForeignLanguageKnowledge", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblLanguage", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblGrant", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblOrganization", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblProgramRole", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblRole", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblStudent", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblStudyAbroad", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblStudyAbroadParticipation", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblTripLocation", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblUniDegreeProgram", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblUniFacultyActivity", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblUniParticipantStudentAttendee", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblUniParticipantFacultyAttendee", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblUniversity", strPath + "/" + strDocName, True
DoCmd.TransferSpreadsheet acExport, 10, "tblUniversityFaculty", strPath + "/" + strDocName, True
MsgBox "Export Complete!", vbOKOnly, ""
DoCmd.SetWarnings True

Exit_Command38_Click:
Exit Sub
Err_Command38_Click:
MsgBox Err.Description
Resume Exit_Command38_Click

End Sub

---结束脚本

我尝试用

之类的东西替换Inputbox方法
Dim fileSelection As Object

Set fileSelection = Application.FileDialog(2)
fileSelection.Show
Set strPath = fileSelection

但是在Set strPath行上抛出了“需要对象”错误。感谢您提供的任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

strPath不是对象,因此Set des不适用。试试这个:

Set fileSelection = Application.FileDialog(2)
fileSelection.Show
If fileSelection.SelectedItems.Count > 0 Then
    strPath = fileSelection.SelectedItems(1)
End If