+ Reply to Thread
Results 1 to 12 of 12

Send sheet as Outlook attachment

Hybrid View

  1. #1
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Code for sending w/sheet as attachment in outlook and clear w/sheet entries after

    Hi again,

    The "permanent" file is the original workbook which you've created, and which normally doesn't store the visitor's details. The "temporary" file is just a saved copy of this "permanent" file in which the current visitor's details have been entered.

    The "temporary" file is appended to the email as an attachment, and is then deleted once the email has been sent. In a relatively simple situation such as this you could in fact send the "permanent" file as an attachment, and dispense with the "temporary" file, but the method I posted is one which I used in a more complex situation.

    Now - let's try to find out where the problem is occurring!

    Go into the VBA Editor, and place the cursor anywhere within the code of the SendEmail function. Now press key F8. You'll see that the first (i.e. Function Declaration) line of the routine is highlighted.

    Press the F8 key once again and you'll see that the first executable line (i.e. On Error GoTo ErrorEncountered) of the routine is highlighted. Continue to press the F8 key, and note the line where pressing this key causes the program execution to jump to the "MsgBox "An error was encountered", vbCritical" statement - this is the line which is causing the problem.

    When you've identified the "problem line", have a look at its code and see if there's anything obviously wrong with it - e.g. a reference to a non-existent filepath etc. If you can identify and solve the problem yourself, that's great - otherwise let me know which line is involved and I'll see what I can do.

    Regards,

    Greg M

  2. #2
    Registered User
    Join Date
    02-01-2009
    Location
    South Wales
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Code for sending w/sheet as attachment in outlook and clear w/sheet entries after

    Quote Originally Posted by Greg M View Post
    Hi again,

    The "permanent" file is the original workbook which you've created, and which normally doesn't store the visitor's details. The "temporary" file is just a saved copy of this "permanent" file in which the current visitor's details have been entered.

    The "temporary" file is appended to the email as an attachment, and is then deleted once the email has been sent. In a relatively simple situation such as this you could in fact send the "permanent" file as an attachment, and dispense with the "temporary" file, but the method I posted is one which I used in a more complex situation.

    Now - let's try to find out where the problem is occurring!

    Go into the VBA Editor, and place the cursor anywhere within the code of the SendEmail function. Now press key F8. You'll see that the first (i.e. Function Declaration) line of the routine is highlighted.

    Press the F8 key once again and you'll see that the first executable line (i.e. On Error GoTo ErrorEncountered) of the routine is highlighted. Continue to press the F8 key, and note the line where pressing this key causes the program execution to jump to the "MsgBox "An error was encountered", vbCritical" statement - this is the line which is causing the problem.

    When you've identified the "problem line", have a look at its code and see if there's anything obviously wrong with it - e.g. a reference to a non-existent filepath etc. If you can identify and solve the problem yourself, that's great - otherwise let me know which line is involved and I'll see what I can do.

    Regards,

    Greg M
    Hi Greg,

    Thanks for your patience and clear advice, it runs and works perfect, I didn't have the correct file location on the first attempt which caused the error.

    I now have just one slight problem though, I can't seem to get the macro to work from the Send button; not sure if I am doing something wrong. I've even tried to 'assign macro' to an alternative button from Drawing but when I look in the available macros box there is no macro showing for some reason. I do not normally have a problem in assigning a macro to a button.

    Would it be possible to explain how to connect the macro to the button please.

    Thanks very much for your time

  3. #3
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Code for sending w/sheet as attachment in outlook and clear w/sheet entries after

    Hi Jessy,

    I think I see your problem!

    Your original workbook contained the code :

    Private Sub CommandButton1_Click()
        SendEmail
    End Sub
    
    
    Public Function SendEmail()
    
    Many lines of code here
    End Function

    Now, when you try to assign a routine to a button, the FUNCTION will not appear in the list of available macros (because it's not a "Sub" procedure), and the SUB will not appear because it's been declared as "Private".

    So, either declare the SUB as a "Public" procedure, or (better!) convert the FUNCTION to a "Public Sub" procedure. Just use:
    Public Sub SendEmail()

    The "End Function" statement will be converted automatically to "End Sub".

    This should allow the procedure to appear in the list of available macros so that you can assign it to whichever object you want.

    Please let me know how you get on with this.

    Regards,

    Greg M

  4. #4
    Registered User
    Join Date
    02-01-2009
    Location
    South Wales
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: Code for sending w/sheet as attachment in outlook and clear w/sheet entries after

    Quote Originally Posted by Greg M View Post
    Hi Jessy,

    I think I see your problem!

    Your original workbook contained the code :

    Private Sub CommandButton1_Click()
        SendEmail
    End Sub
    
    
    Public Function SendEmail()
    
    Many lines of code here
    End Function

    Now, when you try to assign a routine to a button, the FUNCTION will not appear in the list of available macros (because it's not a "Sub" procedure), and the SUB will not appear because it's been declared as "Private".

    So, either declare the SUB as a "Public" procedure, or (better!) convert the FUNCTION to a "Public Sub" procedure. Just use:
    Public Sub SendEmail()

    The "End Function" statement will be converted automatically to "End Sub".

    This should allow the procedure to appear in the list of available macros so that you can assign it to whichever object you want.

    Please let me know how you get on with this.

    Regards,

    Greg M
    Hi Greg,

    Thanks for that, have amended the macro as you advised and this has resolved that issue, however, a slight problem has arisen since.

    After the email has been sent, the MsgBox “Your email has been sent” does not display as it did previously; instead there is a flashing Excel tab in the Taskbar and when opened it shows the form and the message, then when I click “OK” it stops flashing. Can we prevent this?

    Also, would it be possible to have the ‘date of visit’ from cell E8 displayed next to the “Mail” on the subject line?

    Once again many thanks for your time.

    J

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Code for sending w/sheet as attachment in outlook and clear w/sheet entries after

    Hi Jessy,

    To add the date from Cell E8 to your Subject line just add one line of code and change one line of code where I've indicated:

    Public Function SendEmail()
    
        Const sDATE_CELL    As String = "E8"
    '   ADD THE ABOVE LINE              <<<<<<<<<<<<<<<<<<<<<<<
    
        Dim objOutlook      As Object
        Dim objEmail        As Object
    
        Dim vDataRange      As Variant
    
        Dim sTemporaryFile  As String
        Dim sCopiesTo       As String
        Dim sMailText       As String
        Dim sSubject        As String
        Dim sSendTo         As String
    
        On Error GoTo ErrorEncountered
    
        sSubject = "Mail  -  " & Sheets("Sheet1").Range(sDATE_CELL).Value
    '   CHANGE THE ABOVE LINE           <<<<<<<<<<<<<<<<<<<<<<<

    I'm not sure about the flashing taskbar item - I've encountered it in the past, but the problem wasn't fully reproducible and I was never able to discover either a solution or a workaround.

    Did this problem occur with the earlier version of the code? The code is working correctly on my pc at the moment (Windows XP, Office 2010).

    Anyway, I hope the addition of the date in the subject line is of some help to you - please continue to let me know how you're getting on.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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