函数忽略参数?

Function ignoring parameter?

我在 Access 中有 VBA 函数,它应该 return 基于我传递给它的字符串变量的特殊文件夹(MyDocuments、Desktop 等)的路径。但是,我总是得到 public 桌面 "C:\Users\Public\Desktop" 而不是我传入的内容。这是函数代码:

Function SpecialFolderPath(whichFolder As String) As String
    Debug.Print whichFolder

    Dim objWSHShell As Object
    Dim strSpecialFolderPath

    Set objWSHShell = CreateObject("WScript.Shell")

    SpecialFolderPath = objWSHShell.SpecialFolders(whichFolder)

    Debug.Print SpecialFolderPath

    Set objWSHShell = Nothing
    Exit Function
ErrorHandler:

    MsgBox "Error finding " & strSpecialFolder, vbCritical + vbOKOnly, "Error"
End Function

所以,无论我作为 whichFolder 传递什么,我总是得到 C:\Users\Public\Desktop。我该如何纠正?

编辑:

我通过以下方式调用此函数:
- DoCmd.OutputTo acOutputQuery, "BoxForecasting_Jobs", "ExcelWorkbook(*.xlsx)", SpecialFolderPath("MyDocuments") & "\BoxForecastByJobs.xlsx", False, "", , acExportQualityPrint
-Set oWB = oXL.Workbooks.Open(SpecialFolderPath("MyDocuments") & "\BoxForecastByJobs.xlsx")

更改此行:

SpecialFolderPath = objWSHShell.SpecialFolders(whichFolder)

至:

SpecialFolderPath = objWSHShell.SpecialFolders("" & whichFolder & "")

我稍微调整了您的代码。添加了WhichFolder = "Templates",使其成为一个sub并通过一个msgbox返回结果。

我的最终结果:

这是一个collection。使用 for each 循环查看其中的内容。

Set wshshell = CreateObject("WScript.Shell")

For each thing in wshshell.SpecialFolders 
    wscript.echo thing
Next 

这些是它接受的名称。

  • 所有用户桌面

    所有用户开始菜单

    所有用户程序

    所有用户启动

    桌面

    收藏夹

    字体

    我的文档

    网罩

    印花罩

    计划

    最近

    发送至

    开始菜单

    启动

    模板