+ Reply to Thread
Results 1 to 9 of 9

Auto send an email attaching current workbook upon closing Excel

  1. #1
    Registered User
    Join Date
    05-22-2022
    Location
    Texas City, TX
    MS-Off Ver
    2016
    Posts
    30

    Auto send an email attaching current workbook upon closing Excel

    I have already set up a macro that auto saves when closing Excel, that way if someone is in a hurry to leave work and they forget to hit save and just close it down, it will save.

    I'm stuck at Automatically sending an email that attaches the current workbook.

    First of all, is it possible to have both kinds of macros run at the same time when closing Excel?

    If yes, here's the coding I have come up with, but it just doesn't complete the task. (Outlook is set up and ready to go)
    Also, I want it to automatically email and the user not have to do anything on their end.
    I'm not 100% positive the first line of code is correct. I changed the email address for this thread.

    Thanks for any input you give.

    Sub BeforeClose(Cancel As Boolean)

    Dim emailApplication As Object
    Dim emailItem As Object

    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItem(0)

    emailItem.to = "test email address"

    emailItem.Subject = "DSR"

    emailItem.Body = "Test email"

    emailItem.Attachments.Add ActiveWorkbook.FullName

    emailItem.Send

    Set emailItem = Nothing
    Set emailApplication = Nothing

    End Sub

  2. #2
    Forum Contributor GWteB's Avatar
    Join Date
    12-13-2021
    Location
    GMT +1
    MS-Off Ver
    2013
    Posts
    136

    Re: Auto send an email attaching current workbook upon closing Excel

    Quote Originally Posted by bdhobbs1 View Post
    First of all, is it possible to have both kinds of macros run at the same time when closing Excel?
    Yes, both tasks can be performed on closing the workbook. Note that there can be just one handler for the BeforeClose event.
    The event handler macro has to be put in the ThisWorkbook code module and the line on which the sub is declared needs to be read as:
    Please Login or Register  to view this content.
    otherwise Excel and VBA will not recognize the macro as the desired event handler and the macro will not be executed on closing your workbook.
    Did this help? Say thanks by clicking the ★

  3. #3
    Registered User
    Join Date
    05-22-2022
    Location
    Texas City, TX
    MS-Off Ver
    2016
    Posts
    30

    Re: Auto send an email attaching current workbook upon closing Excel

    Thanks for the reply GWteB, I added Private Sub Workbook_BeforeClose to a module, but that stopped it from saving on close.
    It does send an email though.

  4. #4
    Forum Contributor GWteB's Avatar
    Join Date
    12-13-2021
    Location
    GMT +1
    MS-Off Ver
    2013
    Posts
    136

    Re: Auto send an email attaching current workbook upon closing Excel

    Quote Originally Posted by bdhobbs1 View Post
    I'm stuck at Automatically sending an email that attaches the current workbook.
    I have responded to this part and in that regard suggested some amendments to your code.

    Quote Originally Posted by bdhobbs1 View Post
    I have already set up a macro that auto saves when closing Excel, that way if someone is in a hurry to leave work and they forget to hit save and just close it down, it will save.
    You did not provide the code of this part but I think it can be easily merged to make both tasks run sequentially before closing the workbook. As I said, there can be just one event handler so merging is needed.
    It could look like:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-22-2022
    Location
    Texas City, TX
    MS-Off Ver
    2016
    Posts
    30

    Re: Auto send an email attaching current workbook upon closing Excel

    Thanks for your reply

    [QUOTE=GWteB;5686544]
    You did not provide the code of this part, see below.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Application.DisplayAlerts = False
    ThisWorkbook.Save
    Application.DisplayAlerts = True

    End Sub


    I will now go and try and figure out how to put your merge code with my existing code. I'm very weak in this department and still learning.

  6. #6
    Registered User
    Join Date
    05-22-2022
    Location
    Texas City, TX
    MS-Off Ver
    2016
    Posts
    30

    Re: Auto send an email attaching current workbook upon closing Excel

    No luck, I'll try again tomorrow.

  7. #7
    Forum Contributor GWteB's Avatar
    Join Date
    12-13-2021
    Location
    GMT +1
    MS-Off Ver
    2013
    Posts
    136

    Re: Auto send an email attaching current workbook upon closing Excel

    I see, in the end it could look like the code below.
    Note that I've changed the code in a way that the workbook in which this code is running will be attached to the newly created email, rather than the active workbook, since the latter could be a not yet saved workbook, causing a run-time error.
    Also note that both the macros SendEmail and SaveBeforeClose can be placed in either the ThisWorkbook module or in a standard module (but not in both modules at the same time).
    The Workbook_BeforeClose needs to be in the ThisWorkbook module.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    05-22-2022
    Location
    Texas City, TX
    MS-Off Ver
    2016
    Posts
    30

    Re: Auto send an email attaching current workbook upon closing Excel

    Thanks GWteB, works like a charm!

  9. #9
    Forum Contributor GWteB's Avatar
    Join Date
    12-13-2021
    Location
    GMT +1
    MS-Off Ver
    2013
    Posts
    136

    Re: Auto send an email attaching current workbook upon closing Excel

    Glad it's sorted and thanks for letting me know.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Send email from userform with option for attaching files
    By nordicdust in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-17-2019, 03:00 AM
  2. VBA code to copy data from current workbook to another workbook and send email
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-05-2015, 06:47 AM
  3. Workbook not Attaching to auto email
    By Flatdown in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2015, 10:43 AM
  4. Workbook saved - send auto email (first instance only)
    By D_N_L in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2014, 11:20 AM
  5. Send email to fixed address on closing workbook
    By Brontosaurus in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-06-2010, 08:11 PM
  6. [SOLVED] Sending an email from Excel attaching the workbook they are in
    By John in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-21-2006, 11:25 PM
  7. Closing and attaching spreadsheet to an email
    By caddy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-23-2006, 02:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1