我创建了一个子程序,它可以自动执行一些Excel任务,根据数据创建新文件,然后发送电子邮件。以下是我的代码的样子。每次运行这段代码时都会抛出运行时错误438:对象不支持此属性或方法。我对VBA很新。有人可以帮我这个吗?
Sub Foo()
Dim FName As String
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\simohapatra\Desktop\AUTOMATION SM\Raw output.txt", Destination:= _
Range("$A$1"))
.Name = "Checkout"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.ActiveSheet.QueryTables.Refresh BackgroundQuery:=False
End With
Cells.Select
Range("a1").Activate
Selection.Cut
Workbooks.Add
ActiveSheet.Paste
Application.CutCopyMode = False
ChDir "C:\Users\simohapatra\Desktop\AUTOMATION SM\Final output"
FName = "C:\Users\simohapatra\Desktop\AUTOMATION SM\Final output\SM Automation test output between " & Format(Date - 8, "d-mmm") & " and " & Format(Date - 1, "d-mmm") & ".xlsx"
ActiveWorkbook.SaveAs Filename:=FName, FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close True
Windows("1. Carol shipping query.xlsm").Activate
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "simohapatra@ebay.com"
.CC = "edeepak@ebay.com"
.BCC = ""
.Subject = "SM Automation test done"
.Body = "Hi SM, this is to inform that the test results are saved at C:\Users\simohapatra\Desktop\AUTOMATION SM\Final output."
'.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send ' <--------------------------------This is causing troubble
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Dim sKillExcel As String
sKillExcel = "TASKKILL /F /IM Excel.exe"
Shell sKillExcel, vbHide
End Sub