I've almost got this working but I'm stuck on one part. Here's what I'm trying to do:
- Save Outlook email attachment (.csv file)
- Open attachment in Excel
- Delete the last 6 lines of the file
- Re-save the file
I'm able to save the file and get it to open in Excel, but then nothing else happens. No matter what I try I'm unable to get any actions to happen within Excel; I can't get it to delete the last 6 rows (Parse the Footer). Any help would be greatly appreciated!
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
ActiveCell.Activate
- heh. Seriously though,objAtt.DisplayName
is referenced when saving the altered file but what is its meaning outside of yourFor Each
loop?ActiveCell
? No cells are activated anywhere in your code.