Help with DeBruin Email script

chefdt

Board Regular
Joined
Jul 1, 2008
Messages
163
I have tried using the code with no success. My screen flashes, no errors, nothing...also no email. What am I doing wrong?

I have a workbook with multiple sheets, with the address in A1, just as described in his code.

DT
 

Some videos you may like

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Why don't you post the code that you are trying, so that we may have a fighting chance to figure out what is going on...
 

chefdt

Board Regular
Joined
Jul 1, 2008
Messages
163
Sorry, I though everyone is familiar with his code. Whenever I ask a question about email, this is the code people send me.

Code:
[COLOR=#3366CC]Sub Mail_Every_Worksheet()[/COLOR][COLOR=black]'Working in Excel 2000-2013
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm[/COLOR]
    Dim sh As Worksheet
    Dim wb As Workbook
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object

    TempFilePath = Environ$("temp") & "\"

    If Val(Application.Version) < 12 Then
        [COLOR=black]'You use Excel 97-2003[/COLOR]
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        [COLOR=black]'You use Excel 2007-2013[/COLOR]
        FileExtStr = ".xlsm": FileFormatNum = 52
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set OutApp = CreateObject("Outlook.Application")

    For Each sh In ThisWorkbook.Worksheets
        If sh.Range("A1").Value Like "?*@?*.?*" Then

            sh.Copy
            Set wb = ActiveWorkbook

            TempFileName = "Sheet " & sh.Name & " of " _
                         & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

            Set OutMail = OutApp.CreateItem(0)

            With wb
                .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

                On Error Resume Next
                With OutMail
                    .to = sh.Range("A1").Value
                    .CC = ""
                    .BCC = ""
                    .Subject = "This is the Subject line"
                    .Body = "Hi there"
                    .Attachments.Add wb.FullName
                    [COLOR=black]'You can add other files also like this
                    '.Attachments.Add ("C:\test.txt")[/COLOR]
                    .Send   [COLOR=black]'or use .Display[/COLOR]
                End With
                On Error GoTo 0

                .Close savechanges:=False
            End With
            
            Set OutMail = Nothing

            Kill TempFilePath & TempFileName & FileExtStr

        End If
    Next sh

    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With [COLOR=#3366CC]End Sub[/COLOR]
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Here is the thing, I am familiar with the code, however his code works and yours does not, so what good does it do me to read his code which works. I want to see (unless this is it verbatim) what you have on your sheet AKA "the code with no sucess". Does that make any sense...
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows

ADVERTISEMENT

And do you really have:

Code:
With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With [COLOR=#3366CC]End Sub[/COLOR]

"End With" and "End Sub" on the same line and you don't get a compile error...
 

JimSnyder

Board Regular
Joined
Feb 28, 2011
Messages
125
I have used and improved on Ron DeBruin's code, but only for CDO. I have not used Outlook, but if you can post what line it fails on while debugging and any errors seen, I can help. Odds are that it is something simple.
 

chefdt

Board Regular
Joined
Jul 1, 2008
Messages
163

ADVERTISEMENT

I have used and improved on Ron DeBruin's code, but only for CDO. I have not used Outlook, but if you can post what line it fails on while debugging and any errors seen, I can help. Odds are that it is something simple.

Oh... I'm also using 2013 (365). Is that a problem possibly?
 

chefdt

Board Regular
Joined
Jul 1, 2008
Messages
163
I have used and improved on Ron DeBruin's code, but only for CDO. I have not used Outlook, but if you can post what line it fails on while debugging and any errors seen, I can help. Odds are that it is something simple.

That's just it. It runs Error Free....Just doesnt send the email.
 

chefdt

Board Regular
Joined
Jul 1, 2008
Messages
163
And do you really have:



"End With" and "End Sub" on the same line and you don't get a compile error...

Code:
[COLOR=#3366CC]Sub Mail_Every_Worksheet()[/COLOR][COLOR=black]'Working in Excel 2000-2013
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm[/COLOR]
    Dim sh As Worksheet
    Dim wb As Workbook
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object

    TempFilePath = Environ$("temp") & "\"

    If Val(Application.Version) < 12 Then
        [COLOR=black]'You use Excel 97-2003[/COLOR]
        FileExtStr = ".xls": FileFormatNum = -4143
    Else
        [COLOR=black]'You use Excel 2007-2013[/COLOR]
        FileExtStr = ".xlsm": FileFormatNum = 52
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set OutApp = CreateObject("Outlook.Application")

    For Each sh In ThisWorkbook.Worksheets
        If sh.Range("A1").Value Like "?*@?*.?*" Then

            sh.Copy
            Set wb = ActiveWorkbook

            TempFileName = "Sheet " & sh.Name & " of " _
                         & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

            Set OutMail = OutApp.CreateItem(0)

            With wb
                .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

                On Error Resume Next
                With OutMail
                    .to = sh.Range("A1").Value
                    .CC = ""
                    .BCC = ""
                    .Subject = "This is the Subject line"
                    .Body = "Hi there"
                    .Attachments.Add wb.FullName
                    [COLOR=black]'You can add other files also like this
                    '.Attachments.Add ("C:\test.txt")[/COLOR]
                    .Send   [COLOR=black]'or use .Display[/COLOR]
                End With
                On Error GoTo 0

                .Close savechanges:=False
            End With
            
            Set OutMail = Nothing

            Kill TempFilePath & TempFileName & FileExtStr

        End If
    Next sh

    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With 
[COLOR=#3366CC]End Sub[/COLOR]
 
Last edited:

chefdt

Board Regular
Joined
Jul 1, 2008
Messages
163
It wasn't the way I had it. The /CODE editor must have put the "squeeze on".
 

Watch MrExcel Video

Forum statistics

Threads
1,123,318
Messages
5,600,921
Members
414,416
Latest member
Nobu

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
Top