+ Reply to Thread
Results 1 to 4 of 4

Error with code to sending email upon save

  1. #1
    Registered User
    Join Date
    12-09-2013
    Location
    Virginia
    MS-Off Ver
    Excel 2007
    Posts
    2

    Error with code to sending email upon save

    I found this code online which is supposed to send an email to specific addresses whenever the workbook is saved. I pasted the code under "this workbook" and changed the newmsg.Recipients.Add ("[email protected]") to my email address just to test the code as is. I received an error, "Compile error, syntax error" for the starred line. Any thoughts on what the issue might be? Thanks!

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
    Cancel As Boolean)

    Dim answer As String

    **answer = MsgBox("This is where you put the text to prompt the user if he wants to save or not" vbYesNo, "here is the title of that box")

    If answer = vbNo Then Cancel = True
    If answer = vbYes Then
    'open outlook type stuff
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OlObjects = OutlookApp.GetNamespace("MAPI")
    Set newmsg = OutlookApp.CreateItem(olMailItem)
    'add recipients
    'newmsg.Recipients.Add ("Name Here")
    newmsg.Recipients.Add ("[email protected]")
    'add subject
    newmsg.Subject = "Subject line of auto email here"
    'add body
    newmsg.Body = "body of auto email here"
    newmsg.Display 'display
    newmsg.Send 'send message
    'give conformation of sent message
    MsgBox "insert confirmation box test here", , "title of confirmation box"



    End If


    'save the document
    'Me.Worksheets.Save

    End Sub

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,475

    Re: Error with code to sending email upon save

    you missing a comma,
    HTML Code: 

  3. #3
    Registered User
    Join Date
    12-09-2013
    Location
    Virginia
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Error with code to sending email upon save

    WOOHOO! Thanks so much!

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Error with code to sending email upon save

    I had to edit some more of the code for it to work here :

    Please Login or Register  to view this content.

+ 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. Change VBA to allow email code to bring up outlook email before sending it
    By jonvanwyk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2015, 02:33 AM
  2. [SOLVED] Why is my email code sending a blank email?
    By gmr4evr1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-28-2015, 10:20 AM
  3. automatically save open workbook before sending email
    By a_27826 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-03-2013, 10:02 AM
  4. give option to save if outlook is not configured for sending email
    By pankajy18 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2013, 11:53 AM
  5. [SOLVED] VB Code to save an email to hdd on sending
    By cnixon in forum Outlook Formatting & Functions
    Replies: 1
    Last Post: 08-03-2012, 04:33 AM
  6. Sending a email via VBA error
    By pauldaddyadams in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-10-2009, 12:09 PM
  7. Sending an email on workbook save
    By matpj in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-17-2006, 12:57 PM

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