How to specify current user desktop for DoCmd.TransferText

◇◆丶佛笑我妖孽 提交于 2020-01-06 09:03:13

问题


This is what I have:

Private Sub EthosRpt_Click()

    DoCmd.OpenQuery "EthosSessions"

    DoCmd.TransferText acExportDelim, , "EthosSessions", "C:\Users\JDoe\Desktop\EthosRpt.csv", True

End Sub

It works with my user account (where my user account = JDoe). How do I get it to work for ANY current user?


回答1:


Try with:

Private Sub EthosRpt_Click()

    Dim FileName As String

    DoCmd.OpenQuery "EthosSessions"

    FileName =  Environ("UserProfile") & "\Desktop\EthosRpt.csv"
    DoCmd.TransferText acExportDelim, , "EthosSessions", FileName, True

End Sub



回答2:


try "%USERPROFILE%\Desktop\EthosRpt.csv"

like: DoCmd.TransferText acExportDelim, , "EthosSessions", "%USERPROFILE%\Desktop\EthosRpt.csv", True




回答3:


first get the username as string and replace the username

Dim strDisplayName as string
Set objAD = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objAD.UserName)
strDisplayName = objUser.DisplayName

than

DoCmd.TransferText acExportDelim, , 
"EthosSessions","C:\Users\" & strDisplayName & "\Desktop\EthosRpt.csv", True


来源:https://stackoverflow.com/questions/50595765/how-to-specify-current-user-desktop-for-docmd-transfertext

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