【问题标题】:how can a target the current user's documents folder?一个目标怎么可能是当前用户的文档文件夹?
【发布时间】:2018-08-11 21:11:34
【问题描述】:

所以我们有一个要求用户登录的系统,它有自己的漫游配置文件。那么在这串代码中,我如何定位当前用户的文件夹? (仅供参考 excel 2010)

'WORKAROUND:
Dim PID As Double
Dim strRootPath As String

Const strExpExe = "explorer.exe"
Const strArg = " "    '" /e,/root, "

'this is where i need to figure out how to target current user's documents
'// Change rootpath here
strRootPath = "C:\Data Files"

PID = Shell(strExpExe & strArg & strRootPath, 3)

该功能的其余部分做得很好...它打开文件资源管理器我只是想不出告诉它寻找当前用户的语法。

【问题讨论】:

    标签: vba excel exacttarget file-location


    【解决方案1】:

    可能最好的方法是使用这样的函数:

    Function docsFolder() As String
        docsFolder = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
    End Function
    

    还有其他方法,但此方法适用于任何版本的 Windows 并具有用户自定义。

    例如,在我的例子中,我的文档文件夹位于映射的 X: 驱动器上,因此简单地将我的用户名填充到 C:\ 路径中是行不通的。


    更多信息:

    【讨论】:

    • 它在那个位置......只是用户改变了。所以对我来说是。 C:\Users\095573\Documents 一串数字需要是迁移变量。
    • @DanielRoy - 我记得不同版本的 Windows 也位于不同的位置。它可以改变的原因有很多。我编辑的答案涵盖了我能想到的所有可能性..
    • 使用所描述的函数方法我会把它放在哪里?嵌套在函数或 workbook_open 函数中?
    • 这是 100% 正确且绝对是最佳解决方案,因为用作文档首选存储位置的用户个人文件夹可以是任何文件夹。正如微软在支持文章Configuration of the My Documents folder 中解释的那样,任何用户都可以非常轻松地更改文档的首选文件夹。
    【解决方案2】:

    我不确定您希望它有多灵活,但您可以尝试以下方法

    strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
    

    【讨论】:

      【解决方案3】:

      知道了!谢谢!对于任何可能关心的人...让她奔跑的最后一根弦!

      Function docsFolder() As String
      docsFolder = 
      
      CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
      End Function
      
      
      
      
      Private Sub test()
      
      Dim PID As Double
      Dim strRootPath As String
      
      Const strExpExe = "explorer.exe"
      Const strArg = " "    '" /e,/root, "
      
      
      '// Change rootpath here
      strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
      
      PID = Shell(strExpExe & strArg & strRootPath, 3)
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-13
        • 2023-03-22
        • 2018-08-02
        相关资源
        最近更新 更多