【问题标题】:Open a csv file in Excel copy and paste that data into an Access Database table在 Excel 中打开一个 csv 文件,将该数据复制并粘贴到 Access 数据库表中
【发布时间】:2020-06-30 20:04:38
【问题描述】:

我有以下 Outlook VBA 在收到电子邮件时运行,它将在 Excel 中打开一个 csv 文件,复制其中的数据(不包括标题行),打开一个 Access 数据库,打开一个表,删除表行粘贴新数据,完成后关闭 Access 和 Excel。

我在多个规则上运行此代码并不断收到 91 错误代码。

代码如下:

Public Sub CopyPasteIAFeed(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim ExApp As Excel.Application
Dim ExWbk As Workbook

    On Error GoTo CopyPasteIAFeed_Error

Set ExApp = CreateObject("Excel.Application")
Set ExWbk = ExApp.Workbooks.Open("C:\Users\" & Environ("UserName") & "\Documents\NCR\Data Feeds\Report NCR - Daily New Activity Requests.csv")
' Open Feed in Microsoft Excel window.
ExApp.Visible = True
ExApp.ScreenUpdating = True
ExApp.ActiveSheet.Range("A2").Select
ExApp.ActiveSheet.Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy

Dim oApp As Access.Application
Dim LPath As String

LPath = "C:\Users\" & Environ("UserName") & "\Documents\NCR\Database\SP - Link to KM - Non-Critical Request Repository.accdb"
Set oApp = CreateObject("Access.Application")
' Open database in Microsoft Access window.
oApp.OpenCurrentDatabase LPath
oApp.Visible = True
oApp.DoCmd.OpenTable "ReportNCRDailyNewActivity", acViewNormal, acEdit
oApp.DoCmd.RunSQL "DELETE * FROM ReportNCRDailyNewActivity"
oApp.DoCmd.RunCommand acCmdPasteAppend

oApp.CloseCurrentDatabase
oApp.Quit acQuitSaveAll
ExApp.CutCopyMode = False
ExApp.Quit

Set objAtt = Nothing
Set oApp = Nothing
Set ExApp = Nothing

MsgBox "InStream Activity Feed Imported. Continue"

   On Error GoTo 0
   Exit Sub

CopyPasteIAFeed_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CopyPasteIAFeed of Module Module10"

End Sub

【问题讨论】:

  • 你在哪一行得到错误?
  • 它没有说,但似乎一旦打开 Excel 工作簿并选择 (A2),它就会停在那里。
  • 为了清楚起见,我在模块模块 10 的过程 CopyPasteIAFeed 中收到错误 91(对象变量或未设置块变量)。我的代码中是否缺少某些内容。这运行了一次并且工作了,但从那以后就没有了。
  • 您正在使用ActiveSheetSelection - 非常不可靠的数据寻址方式,请参阅this post。修复后还会出现错误吗?
  • 你有什么建议?

标签: excel vba ms-access outlook


【解决方案1】:

甚至不需要使用 Excel,因为 MS Access 可以使用 DoCmd.TransferText 充分上传 csv 文件。

...
Dim oApp As Access.Application
Dim LPath As String

LPath = "C:\Users\" & Environ("UserName") & "\Documents\NCR\Database\SP - Link to KM - Non-Critical Request Repository.accdb"

Set oApp = CreateObject("Access.Application")
' Open database in Microsoft Access window.
oApp.OpenCurrentDatabase LPath
oApp.DoCmd.RunSQL "DELETE * FROM ReportNCRDailyNewActivity"
oApp.DoCmd.TransferText acImportDelim, , "ReportNCRDailyNewActivity", LPath, True

oApp.CloseCurrentDatabase   
oApp.Quit 
...

顺便说一句,Access 默认附带 Jet/ACE SQL 引擎,并且可以迁移和连接到其他关系数据库管理系统 (RDMS)。考虑使用这个强大的软件作为任何数据迁移需求的中心点。作为数据库客户端,它可以处理各种文件类型:csv、txt、tab、xlsx、sql、xml,甚至是 html,以及 ODBC/OLEDB 源。因此,您的 Outlook 宏可以很好地用作 Access VBA 模块!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-06
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多