【问题标题】:Using Environ$(“USERPROFILE”) VBA save to path使用 Environ$(“USERPROFILE”) VBA 保存到路径
【发布时间】:2018-01-10 20:59:52
【问题描述】:

我是 VBA 新手,为我的无知道歉。 我在 Excel 2016 中有一个非常复杂的宏,我需要对其进行编辑。 与this previous discussion相关

我需要更改创建的 .pdf 的输出保存路径。现在它使用 Excel 位置作为起点 (ThisWorkbook.Path) 并保存到同一个文件夹中。

但现在它需要保存到另一个文件夹,该文件夹将位于共享 Box 驱动器上。路径将根据访问文件的人而改变。

那么我应该如何使用 Environ$ 来表示 C:\Users\johnsmith\Box\etc。 etc. 会改变,\Box\etc 之后的一切都会改变。等是恒定的? 它使用 Excel 电子表格中的值来组成新文件名。

这是我需要修改的部分:

With shDetail
        If llRow - 3 < 10 Then
            strPDFOutPath = ThisWorkbook.Path & "\2018 Payments\0" & .Cells(llRow, 14).Value & "_" & .Cells(llRow, 3).Value & "_" & "file" & ".pdf"
        Else
            strPDFOutPath = ThisWorkbook.Path & "\2018 Payments\" & .Cells(llRow, 14).Value & "_" & .Cells(llRow, 3).Value & "_" & "file" & ".pdf"
        End If
    End With

    'Save the form as new PDF file.
    objAcroPDDoc.Save 1, strPDFOutPath

应该这样

 strPDFOutPath = Environ$(“USERPROFILE”) & Box\Folder1\Folder2\Folder3\0" & .Cells(llRow, 14).Value & "_" & .Cells(llRow, 3).Value & "_" & "file" & ".pdf"
        Else
            strPDFOutPath = Environ$(“USERPROFILE”) & Box\Folder1\Folder2\Folder3" & .Cells(llRow, 14).Value & "_" & .Cells(llRow, 3).Value & "_" & "file" & ".pdf"

?

【问题讨论】:

  • User = Environ$("UserName"), 获取运行代码的人/机用户名

标签: vba excel environ


【解决方案1】:

这只是它的外观示例:

    Const FILE_SUFFIX As String = "\Box\Etc\Whatever\"
    Dim userPath As String

    userPath = Environ("UserProfile")

    strPDFOutPath  = userPath & FILE_SUFFIX & & .Cells(llRow, 14).Value & "_" & .Cells(llRow, 3).Value & "_" & "file" & ".pdf"

【讨论】:

    猜你喜欢
    • 2019-03-26
    • 1970-01-01
    • 2014-05-13
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    相关资源
    最近更新 更多