Excel VBA从保管箱打开工作簿,然后保存/关闭当前工作簿

时间:2016-02-09 16:57:12

标签: excel vba excel-vba path dropbox

好的,这是我到目前为止所尝试的代码:

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 个答案:

答案 0 :(得分:0)

就我而言," info.json" file将路径显示为:

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

它使用double" \"字符。提供的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