【问题标题】:Excel VBA Open a workbook from dropbox then Save/Close Current WorkbookExcel VBA 从保管箱打开工作簿,然后保存/关闭当前工作簿
【发布时间】:2016-08-25 03:49:02
【问题描述】:

好的,这是我迄今为止尝试过的代码:

Dim myFileName As String, DTAddress As String, ans As String, DBPathEstim As String
     Dim sPath As String, stPath As String, WB1 As Workbook, WB2 As Workbook, OriginFile As String
        ' Send the workbook to clients
        myFileName = Worksheets("EstimatingSheet").Range("U11").Value & ".xls"
        sPath = ActiveWorkbook.Path

        stPathClients = Left(sPath, InStrRev(sPath, "\") - 1) & "\Clients\" & myFileName
        ActiveWorkbook.SaveAs stPathClients

        ActiveWorkbook.Close

好的,我想打开一个工作簿。由于我们使用 Dropbox,因此不同用户的工作簿路径可能会有所不同。所以它永远是.....\Dropbox\SSFiles\Estimating 2016.xls

【问题讨论】:

  • 两种选择:1) 使用GetOpenFilename,它允许用户选择要打开的文件。 2) 对操作系统进行测试,然后构建通常安装 DropBox 文件夹的字符串。 (这显然不是万无一失的,因为用户可以把它放在任何地方。
  • 此帮助文章包含有关查找本地 Dropbox 文件夹路径的官方方法的信息:dropbox.com/help/4584

标签: vba excel path dropbox


【解决方案1】:

在我的情况下,“info.json”文件将路径显示为:

"C:\\Users\\[USER]\\Dropbox"

它使用双“\”字符。提供的article 说它应该是这样的:

"C:\Users\[USER]\Dropbox"

无论您的情况是什么,这段代码都可以在任何位置获取 Dropbox 路径:

Function GetDropboxPath() As String
    '---------------------------------------------------------------------------------'
    '* Locates the Dropbox user path by usign the local "info.json" file. ************'
    '* Wrote by cesarmades ***********************************************************'
    '---------------------------------------------------------------------------------'

    ' Loads the local info.json file
    Dim intFile As Integer: intFile = FreeFile
    Open VBA.Interaction.Environ("USERPROFILE") & "\AppData\Local\Dropbox\info.json" For Input As #intFile

    ' Stores info.json file content in a variable
    Dim strFileContent As String: strFileContent = Input(LOF(intFile), intFile)
    Close #intFile

    ' Trims the string and returns the path
    Dim intIPos As Integer: intIPos = VBA.Strings.InStr(1, strFileContent, """path""", vbTextCompare) + 9
    Dim intFPos As Integer: intFPos = VBA.Strings.InStr(1, strFileContent, """host""", vbTextCompare) - 3

    GetDropboxPath = VBA.Strings.Replace(Mid(strFileContent, intIPos, intFPos - intIPos), "\\", "\")
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多