【问题标题】:Import a CSV file into Access database from SharePoint Online Document Library将 CSV 文件从 SharePoint Online 文档库导入 Access 数据库
【发布时间】:2016-04-11 09:26:09
【问题描述】:

我在 SharePoint Online 文档库中上传了一个 CSV 文件,该文件每天都会更新。我使用这个 CSV 文件来创建一些带有 Access 数据库的报告。我想要实现的是自动将 CSV 文件导入 Access,而无需在本地下载和保存 CSV 文件。我使用的代码如下:

Sub ImportCSV()

   DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Daily_Report_Table", FileName:="https://mycompany.sharepoint.com/teams/Daily%20Reporting.csv", HasFieldNames:=True

End Sub

但是,它要么要求我登录,然后说我无权访问该文件,要么没有执行说它无法加载 HTML 页面...

【问题讨论】:

标签: csv ms-access sharepoint vba


【解决方案1】:

不要被链接的标题所迷惑。

您不能直接通过 HTTP 导入/链接文件。代码为你下载文件,准备链接:

Sub ImportCSV()

    Dim Result As Long
    Dim Url As String
    Dim Filename As String

    Url = "https://mycompany.sharepoint.com/teams/Daily%20Reporting.csv"
    Filename = "C:\Imports\DailyReporting.csv"

    Result = DownloadFile(Url, Filename)
    If Result = 0 Then
        DoCmd.TransferText TransferType:=acLinkDelim, TableName:="Daily_Report_Table", FileName:=Filename, HasFieldNames:=True 
    End If

End Sub

支持代码模块:

Option Compare Database
Option Explicit

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
    ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) _
    As Long

Public Function DownloadFile( _
    ByVal strURL As String, _
    ByVal strLocalFilename As String) _
    As Long

' Download file or page with public access from the web.
' 2004-12-17. Cactus Data ApS, CPH.

' Usage, download a file:
' lngRet = DownloadFile("http://www.databaseadvisors.com/Graphics/conf2002/2002ConferencePicsbySmolin/images/dba02smolin27.jpg", "c:\happybassett.jpg")
'
' Usage, download a page:
' lngRet = DownloadFile("http://www.databaseadvisors.com/conf2002/conf200202.asp", "c:\dbaconference.htm")

' Returns 0 if success, error code if not.
' Error codes:
' -2146697210 "file not found".
' -2146697211 "domain not found".

' Limitation.
' Does not check if local file was created successfully.

    Dim lngRetVal As Long

    lngRetVal = URLDownloadToFile(0, strURL & vbNullChar, strLocalFilename & vbNullChar, 0, 0)

    DownloadFile = lngRetVal

End Function

【讨论】:

  • 谢谢 Gustav,抱歉耽搁了。我尝试了您发送的代码,但出现错误“编译错误:未定义子或函数”,并突出显示“下载文件”。有什么想法吗?
  • 哦,忘了。它附加在答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 2012-05-15
相关资源
最近更新 更多