Fails to Attach file

DanMee

New Member
Joined
May 3, 2012
Messages
43
So this is a piece of code, that runs in a different workbook, as part of the same system, perfectly fine. However will not function as required in this workbook.

This doesn't throw an error, but when hovering over "ActiveWorkbook.FullName" in the .Add code, it shows as <automation error="">

I have literally no idea how to fix this. Previous weeks this code has been working fine, however I am at the point of this project launching and I cannot see for the life of me why the attachment is not adding. I have tried every possible variation of declaring the file to be attached, and all have the exact same issue.

Code:
Dim FilePath As String    
Dim FileName As String
Dim OutApp As Object
Dim OutMail As Object


    ActiveSheet.Copy
    Sheets(MON).Protect Password = NOMINAL PASSWORD HERE
    FilePath = FILEPATH
    FileName = FILENAMEincEXT
    
    ActiveWorkbook.SaveAs FileName:=FilePath & FileName


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .to = EMAILSTRING
        .CC = EMAILSTRING2
        .BCC = ""
        .Subject = SUBJ
        .Body = "BODY HERE"
        .Attachments.Add ActiveWorkbook.FullName
        .Send
    End With
    On Error GoTo 0


    ActiveWorkbook.Close savechanges:=False


    Set OutMail = Nothing
    Set OutApp = Nothing
</automation>
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This doesn't throw an error, but when hovering over "ActiveWorkbook.FullName" in the .Add code, it shows as
What does it show?

Assuming you correctly declared both FILEPATH and FILENAMEincEXT, you know which is the attachment name. So I suggest you try these modifications:
Code:
    ActiveWorkbook.SaveAs FileName:=FilePath & FileName   'Existing
    ActiveWorkbook.Close False         '<<< ADD

Code:
        .Attachments.Add FilePath & FileName    '<<< MODIFIED  ActiveWorkbook.FullName

Code:
'   ActiveWorkbook.Close savechanges:=False      <<< Commented, or remove the line

And by the way, why your code include the horrible "On Error Resume Next"?

Bye
 
Upvote 0
What does it show?
After the lines have run in step mode, it shows Automation Error inside the greater than and less than symbols (apparently my previous message thought i was trying to HTML)
I suggest you try these modifications

Unfortunately this yields the same result - the email sends, but with no attachment.

And by the way, why your code include the horrible "On Error Resume Next"?

Bye
I'm entirely self taught with VBA, so its something that was in the code i pulled from google originally


Dryver14 said:
Where are these coming from?

FilePath = FILEPATH
FileName = FILENAMEincEXT

These are just masks I've used for here as I did not want the actual string data shown for privacy purposes. I know there are no issues with these string values as the file successfully saves in the correct location with the correct variable filename.
 
Upvote 0
Your code seems spot on only thing I'm looking at and I have no idea if it makes a difference is the line activesheet.copy which doesn't seem to paste anywhere
A random thought is that you might need to clear the clipboard.
 
Upvote 0
I have finally found the error. And its nothing to do with the code in the end.

For some unknown reason, despite zero changes to the cell formats of the template sheet it copies each time, the base file size had risen from 67kb to 25mb... it was too big to attach....

Many thanks for all your help!
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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