1
我有以下Outlook VBA,它運行時會在Excel中打開一個csv文件,複製其中的數據(不包括標題行),打開Access數據庫,打開表格,刪除表格行並粘貼新數據,並在完成時關閉Access和Excel。在Excel中打開一個csv文件複製並將該數據粘貼到Access數據庫表
我有這個代碼運行幾個規則,並不斷得到一個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
你在哪一行得到錯誤? – Verzweifler
它沒有說,但它會出現,一旦Excel工作簿打開和(A2)被選中它就停在那裏。 –
爲了清楚起見,我在模塊模塊10的過程CopyPasteIAFeed中得到錯誤91(對象變量或塊變量未設置)。我在代碼中丟失了什麼。這跑了一次,然後工作,但沒有。 –