有没有办法在未保存的 Excel 工作簿上使用 OLE DB Provider for Jet?

发布于 2024-08-17 10:02:27 字数 1334 浏览 7 评论 0原文

我正在与 Microsoft OLE DB Provider for Jet 合作,使用 VBA 对 Excel 中的电子表格执行查询。有没有办法在未保存工作簿上执行以下代码?

例如,如果工作簿从未保存过,ActiveWorkbook.FullName 将返回“Book1”。在这种情况下,数据源将假定该路径是活动目录,并因文件从未保存而出错。

有没有办法使用Excel临时文件作为Jet的数据源?我想对此进行测试,但我什至不知道如何返回 Excel 临时文件的路径和名称。

Public Sub LocalJetQuery()

    Dim objStartingRange As Range
    Dim objConnection As New ADODB.Connection
    Dim objRecordset As New ADODB.Recordset

    Dim strDSN As String
    Dim strSQL As String

    Set objStartingRange = Application.Selection

    If CLng(Application.Version) >= 12 Then
        strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;" _
        & "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
        & "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";"
    Else
        strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" _
        & "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
        & "Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
    End If

    strSQL = "SELECT * FROM [" & objStartingRange.Worksheet.Name & "$];"

    objConnection.Open strDSN
    objRecordset.Open strSQL, objConnection

    Application.Workbooks.Add(xlWBATWorksheet).Sheets(1).Cells(1, 1).CopyFromRecordset objRecordset

End Sub

谢谢!

I am working with the Microsoft OLE DB Provider for Jet to execute queries on spreadsheets in Excel using VBA. Is there a way to execute the following code on an unsaved workbook?

For example, ActiveWorkbook.FullName returns "Book1" if the workbook has never been saved. In that case the Data Source will assume the path is the active directory, and error out because the file was never saved.

Is there any way to use the Excel temporary file as the Data Source for Jet? I would like to test this but I don't even know how to return the Path and Name for the Excel temporary file.

Public Sub LocalJetQuery()

    Dim objStartingRange As Range
    Dim objConnection As New ADODB.Connection
    Dim objRecordset As New ADODB.Recordset

    Dim strDSN As String
    Dim strSQL As String

    Set objStartingRange = Application.Selection

    If CLng(Application.Version) >= 12 Then
        strDSN = "Provider=Microsoft.ACE.OLEDB.12.0;" _
        & "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
        & "Extended Properties=""Excel 12.0 Xml;HDR=No;IMEX=1"";"
    Else
        strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;" _
        & "Data Source=" & objStartingRange.Worksheet.Parent.FullName & ";" _
        & "Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
    End If

    strSQL = "SELECT * FROM [" & objStartingRange.Worksheet.Name & "$];"

    objConnection.Open strDSN
    objRecordset.Open strSQL, objConnection

    Application.Workbooks.Add(xlWBATWorksheet).Sheets(1).Cells(1, 1).CopyFromRecordset objRecordset

End Sub

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2024-08-24 10:02:27

不,就像大卫·芬顿在评论中所说的那样。

No. Just like David Fenton says in the comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文