如何在保存到 OneDrive 的 Excel 工作簿中运行 SQL 查询?

Posted

技术标签:

【中文标题】如何在保存到 OneDrive 的 Excel 工作簿中运行 SQL 查询?【英文标题】:How can I run a SQL query within an Excel Workbook saved to OneDrive? 【发布时间】:2019-01-07 21:04:13 【问题描述】:

我想对包含在单个 Excel 工作簿中的所有表运行 SQL 查询。我的 VBA 代码使用 ADODB 来运行这些 SQL 查询。

将工作簿保存在 OneDrive 中时打开连接失败,但将工作簿保存到本地驱动器时可以正常工作。

如何在单个 Excel 工作簿中的表上运行 SQL,同时保存在 OneDrive 上?

当图书保存在本地但不在 OneDrive 上时,该代码有效。唯一的变化是在每种情况下看起来完全不同的文件路径:

OneDrivePathExample = "https://d.docs.live.net/....xlsb"

LocalPathExample = "C:\My Documents\....xlsb"

我已经在连接字符串中的文件路径周围进行了一些实验,但不出所料,它们没有奏效:

    原创

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=https://d.docs.live.net/.../Documents/Financial Tracker.xlsb;Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";
    

    将“/”替换为“\”

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=https:\\d.docs.live.net\...\Documents\Financial Tracker.xlsb;Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";`
    

    在路径周围添加方括号

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[https://d.docs.live.net/.../Documents/Financial Tracker.xlsb];Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";
    

    在路径周围添加引号

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source="https://d.docs.live.net/.../Documents/Financial Tracker.xlsb";Extended Properties="Excel 12.0;HDR=Yes;IMEX=1";
    

我意识到我可以通过在运行此代码时将其保存在本地来避免这种情况,然后将其保存回 OneDrive,但如果可能的话,我想避免这种情况。

我也意识到我可以编写 VBA 代码来完成我想要用 SQL 做的事情,但是我最初是这样做的,但后来切换到 SQL 方法,因为 SQL 更快。

这是我的代码:

Function OpenRST(strSQL As String) As ADODB.Recordset
''Returns an open recordset object

Dim cn As ADODB.Connection
Dim strProvider As String, strExtendedProperties As String
Dim strFile As String, strCon As String

strFile = ThisWorkbook.FullName

strProvider = "Microsoft.ACE.OLEDB.12.0"
strExtendedProperties = """Excel 12.0;HDR=Yes;IMEX=1"";"


strCon = "Provider=" & strProvider & _
     ";Data Source=" & strFile & _
     ";Extended Properties=" & strExtendedProperties

Set cn = CreateObject("ADODB.Connection")
Set OpenRST = CreateObject("ADODB.Recordset")

cn.Open strCon  ''This is where it fails

OpenRST.Open strSQL, cn

End Function

cn.Open strCon 行出现以下错误:

运行时错误'-2147467259 (80004005)'; 对象“_Connection”的“打开”方法失败

谢谢!

【问题讨论】:

本地工作簿是否同步到 OneDrive? @Parfait,我可能误解了你的问题,但不,它只是保存到 OneDrive。我通常没有本地副本。 所以您的桌面上没有类似于 Dropbox 的同步 OneDrive?考虑这样做,因为 Jet/ACE SQL 引擎无法指向连接的 URL。 @Parfait,谢谢,假设它可以在多台计算机上工作,这对我来说可能是一个合适的解决方法。今晚我会试一试。 【参考方案1】:

这是我获取文件路径的解决方案。

'This Function search root folder as C: ,D: ...
'Search into all OneDrive folders
Option Explicit
Private Const strProtocol   As String = "Http"
Private Const pathSeparator As String = "\"

Function MainFindFile(ByRef NullFilePath As String, Optional FileName As String) As Boolean
    
    Dim fso                 As FileSystemObject 'Necessary enable microsoft scripting runtime in references
    Dim UserRootFolder      As Folder
    Dim SecondSubFolders    As Folder
    Dim ThirdSubFolders     As Folder
    Dim InitialPath         As String
    Dim OneDriveFolderName  As String
    
    Set fso = New Scripting.FileSystemObject
    
    InitialPath = ActiveWorkbook.FullName
    If FileName = vbNullString Then FileName = ActiveWorkbook.Name

    If InStr(1, InitialPath, strProtocol, vbTextCompare) > 0 Then
        InitialPath = Environ("SystemDrive")
        InitialPath = InitialPath & Environ("HomePath")
        
        'Gets all folders in user root folder
        Set UserRootFolder = fso.GetFolder(InitialPath)
        
        For Each SecondSubFolders In UserRootFolder.SubFolders
            'Searches all folders of OneDrive, you may have how many Onedrive's folders as you want
            If InStr(1, SecondSubFolders.Name, "OneDrive", vbTextCompare) > 0 Then
                OneDriveFolderName = InitialPath & pathSeparator & SecondSubFolders.Name
                'Verifies if file exists in root of Onedrive Folder
                MainFindFile = SearchFile(OneDriveFolderName, FileName, NullFilePath)
                If MainFindFile Then Exit For

                'Uses recursive function to percur all subfolders in root of OneDrive
                For Each ThirdSubFolders In fso.GetFolder(OneDriveFolderName).SubFolders
                    MainFindFile = RecursiveFindFile(ThirdSubFolders, FileName, NullFilePath)
                    If MainFindFile Then Exit For
                Next ThirdSubFolders
            End If
            If MainFindFile Then Exit For
        Next SecondSubFolders
        
    End If
    
    MsgBox NullFilePath
    
End Function
Private Function RecursiveFindFile(Folder As Folder, FileName As String, ByRef NullFilePath As String) As Boolean

    Dim fso         As FileSystemObject
    Dim objFolder   As Folder
    Dim Result      As Boolean
    
    Set fso = New Scripting.FileSystemObject
    
    'Verifies if file exists in root of Onedrive Folder
    RecursiveFindFile = SearchFile(Folder.Path, FileName, NullFilePath)
    If RecursiveFindFile Then Exit Function
    
    For Each objFolder In Folder.SubFolders
        If Not SearchFile(objFolder.Path, FileName, NullFilePath) Then
            RecursiveFindFile = RecursiveFindFile(objFolder, FileName, NullFilePath)
            If RecursiveFindFile Then Exit For
        Else
            RecursiveFindFile = True
            Exit For
        End If
    Next objFolder
    
End Function
Private Function SearchFile(Path As String, FileName As String, ByRef NullFilePath As String) As Boolean
    
    'NullFilePath is a byref variable to be filled by this function
    Dim fso As New Scripting.FileSystemObject
    
    If fso.FileExists(Path & pathSeparator & FileName) Then
        NullFilePath = Path & pathSeparator & FileName
        SearchFile = True
    End If
    
End Function

【讨论】:

【参考方案2】:

将 Https: 替换为“”。这会让你更进一步。

【讨论】:

以上是关于如何在保存到 OneDrive 的 Excel 工作簿中运行 SQL 查询?的主要内容,如果未能解决你的问题,请参考以下文章

如何实现在线编辑WORD文档

Office 365实现多人在线编辑同一个文档(中)

使用 Python 在 OneDrive 文件夹中打开 excel 工作簿

来自不同用户的位于 OneDrive 上的 Excel 工作簿的 Excel 查询

onedrive实现excel在线编辑 online excel

在excel中另存为宏