我几乎已经完成了这项工作,但我被困在了一个部分。这是我正在尝试做的事情:
- 保存 Outlook 电子邮件附件(.csv 文件)
- 在 Excel 中打开附件
- 删除文件的最后 6 行
- 重新保存文件
我能够保存文件并让它在 Excel 中打开,但没有其他任何事情发生。无论我尝试什么,我都无法在 Excel 中执行任何操作;我无法删除最后 6 行(解析页脚)。任何帮助将不胜感激!
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim attachName As String
Dim oXL As Object, oWB As Object, oSheet As Object
saveFolder = "C:\Temp\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
attachName = objAtt.DisplayName
Set objAtt = Nothing
Next
' Start Excel and get Application object
Set oXL = CreateObject("Excel.Application")
' Hide Excel
oXL.Visible = False
' Open the File
Set oWB = oXL.Workbooks.Open(saveFolder & attachName)
'Set the Worksheet
Set oSheet = oWB.Sheets("Sheet1")
'Parse the Footer
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(-5, 0).Range("A1:A6").Select
ActiveCell.Activate
Selection.ClearContents
'Save the File
Set oWB = oXL.Workbooks.Save(saveFolder & "\" & objAtt.DisplayName)
'Clean Up
oWB.Close (True)
oXL.Quit
Set oWB = Nothing
Set oXL = Nothing
End Sub