VBA to delete attachment created on desktop

bh24524

Active Member
Joined
Dec 11, 2008
Messages
372
Office Version
  1. 365
  2. 2007
Hello, I have the code below I found that will attach a range in an Excel file as an email attachment. It is saving a copy of it to my desktop after the email is composed. This is the first time I've done this with only a specified range in the file, but I was wondering if there is a way to have the VBA also delete that file that is created on the desktop, what code that would be, and where that should go?

VBA Code:
Sub Email_File()
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object


    Set rng = Nothing
    On Error Resume Next
    Set rng = Sheets("Sheet1").Range("A1:Q27").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0


    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .Application.DisplayAlerts = False
    End With
   
Dim newBook As Workbook, wFile As String
wFile = ThisWorkbook.Path & "\Transfer-Form.xlsx"
Set newBook = Workbooks.Add
rng.Copy Range("A1")
newBook.SaveAs wFile
newBook.Close False
   
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .Display
        .htmlbody = "<BODY style=font-size:11pt;font-family:Calibri>" & "Please see the attached store transfer. <br>" & "</BODY>" & .htmlbody
        .To = Range("AA1").Value
        If Evaluate("countif(S1,""Perishable"")") >= 1 Then
        .CC = Range("AA2").Value
        ElseIf Evaluate("countif(S1,""Grocery"")") >= 1 Then
        .CC = Range("AA3").Value
        Else
        .CC = Range("AA4").Value
        End If
        .BCC = ""
        .Subject = "Store Transfer: " & Range("F10").Value & " ||  TO  || " & Range("F16").Value
        .Attachments.Add wFile
        '.Send   'or use .Display
    End With
    On Error GoTo 0


    With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .Application.DisplayAlerts = True
    End With
   

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub




Function RangetoHTML(rng As Range)
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook


    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"


    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With


    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")


    TempWB.Close savechanges:=False


    Kill TempFile
   

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Note the addition of the "Kill wfile" line towards the bottom of the macro :

VBA Code:
With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .Application.DisplayAlerts = True
    End With
    
   Kill wFile

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
 
Upvote 0
Hi Logit, thanks for that, unfortunately it doesn't seem to be working though. I had actually tried that before even posting this thread but I thought I was just completely on the wrong track. Glad to know maybe not. I also tried putting kill "C:\Users\bhar05h\OneDrive - United Natural Foods Inc\Desktop\Transfer-Form.xlsx" in the same place as where kill wFile was and I couldn't get that to work either. When I have the Kill wFile line in there and run the macro, I get the following:

1737734033108.png

1737734049951.png

When I had the pathway written out in place of kill wFile, I didn't get the run-time error, but it didn't delete the file either.
Not sure if you might be able to weigh in on that?
 
Upvote 0
Well ... you have used the two methods I would use to delete the workbook. It appears, as far as VBA is concerned, the workbook doesn't exist.
 
Upvote 0
Bummer. Okay, I'll wait and see if someone else might have any additional insight. Thank you anyway though at least.
 
Upvote 0

Forum statistics

Threads
1,226,049
Messages
6,188,566
Members
453,484
Latest member
jlo1673

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top