DoCmd.TransferSpreadsheet Export: Let user choose save destination?

旧城冷巷雨未停 提交于 2021-02-08 10:25:47

问题


I successfully run TransferSpreadsheet to export an Access query to an Excel file at a specified location:

Private Sub cmdExportQuery_Click()
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "trndOTQry", _ 
"\\company.com\root\share\public\D2S\D2S\D2S_Scorecard\Trend OT.xls"
End Sub

Given that this is a shared file, I want a dialog to open that allows the user to select whatever save destination he chooses. I found this link with a good solution that references this article. When I plug this code into my window:

Private Sub cmdExportQuery_Click()
Dim strSaveAsFilter As String
Dim strSaveAsFileName As String
strFilter = ahtAddFilterItem(strSaveAsFilter, "Excel Files (*.xls)", "*.xls")
strSaveAsFileName = ahtCommonFileOpenSave( _
OpenFile:=False, _
Filter:=strSaveAsFilter, _
Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "trndOTQry", _
strSaveAsFileName, , "-1", True
End Sub

I get the error Compile Error: Sub or Function not defined, it highlights ahtAddFilterItem in blue, and highlights Private Sub cmdExportQuery_Click() in yellow with a yellow arrow. I tried making a Public Sub, but nothing happened when I clicked the button. What must I do to allow the user to choose the save destination when exporting this query to Excel?


回答1:


There is additional code that should have also been included. Check here:

http://access.mvps.org/access/api/api0001.htm

I also found this which may be helpful from someone else who had the same error:

"Did you also get the additional code mentioned in the NOTE

Note: This code still requires the GetOpenFileName routine provided by Ken Getz. Make sure you copy the function from this article.

which sends you to http://www.mvps.org/access/api/api0001.htm"



来源:https://stackoverflow.com/questions/21008058/docmd-transferspreadsheet-export-let-user-choose-save-destination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!