Code to bypass Outlook's security prompt

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,300
Hello everyone - -



Situation

As most of you know, when sending an email in code from Excel through Outlook, the following Microsoft Office Outlook dialog appears during code execution in Office releases since 2002:

A Program is trying to automatically send e-mail on your behalf.
Do you want to allow this?
If this is unexpected, it may be a virus and you should choose "No".

Two buttons: [Yes] [No]



Question

Has anyone used or developed code that bypasses this prompt, and if so, would you please post an example of your code here. What I would like is a macro in VBA and / or API that you know from your own first-hand experience has worked for you, that emails an Excel attachment from Excel, using Outlook (not Outlook Express) as the email client, which bypasses the above prompt.



Disqualifiers

(1)
I am aware of but cannot use any commercial utility programs such as Redemption or "ClickYes" from ContextMagic. This project will need to run on systems not allowed to download such utilities.

(2)
I am aware of email-related sites such as Ron Debruin's and others. Respectfully, I do not want or need links to those sites. Taking nothing away from Ron or anyone else, I have visited dozens of sites and tested lots of code including my own, but have not found, modified, or been able to develop code that actually does what I am asking for.

(3)
I cannot use the CDO method. I will not know what my end user clients' SMTP server addresses will be.

(4)
The solution cannot involve C++ or other development languages. Only a stand-alone macro in an Excel workbook that uses VBA and / or API will suffice.

(5)
I cannot use an older Office pre-security prompt version. This project will need to be executed on Office 2003 applications.



Qualifiers

(1)
Early or late binding is OK.

(2)
Establishing any VBA Project library references would be no problem.

(3)
Must be VBA / API code for an Excel macro that sends an Email with an Excel attachment in Outlook which bypasses the Outlook security prompt.

(4)
The code you post has been tested successfully by you.



Many thanks to you VBA and API wizards out there who can help me develop this solution, if indeed a solution is possible given the disqualifiers.
 
Hi, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Very interesting functionality! I tried to modify my existing VBA code to also include this (to avoid clicking “Yes” on the annoying security button – but I got lost. (I’m quite new to VBA…) I wonder if anyone can help me to add this to my code below please. The code below works perfectly (except that I have to click the button…) – and is purely based on Rons de Bruins excellent codes examples: Thanks Ron!<o:p></o:p>
<o:p></o:p>
I’m using the following VBA to send a mail with the Excel content from the cells A1:A50 in the worksheet called "Presentation". The content is copied as text in the mail body:

Rich (BB code):
Sub Mail_Selection_Range_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2007
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    'Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a range if you want
    Set rng = Sheets("Presentation").Range("A1:A50").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
        .To = "ecksell@company.com"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2007
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
 
    TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
 
    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With
 
    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With
 
    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.ReadAll
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")
 
    'Close TempWB
    TempWB.Close savechanges:=False
 
    'Delete the htm file we used in this function
    Kill TempFile
 
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function


Thanks a lot for the help, <o:p></o:p>
Ecksell<o:p></o:p>
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi Again, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
The VBA code posted by “Fin Fang Foom” together with his suggested setting in Outlook Reference solved my problem to automatically send the mail without the annoying prompt message – Great and thanks!<o:p></o:p>
<o:p> </o:p>
BUT: Instead of automatically sending an Attachment – I would like to send a specific range of cells in the mail body based on Mr Ron de Bruins excellent example which I posted in my previous thread: Using for example:<o:p></o:p>
<o:p> </o:p>
Set rng = Sheets("Presentation").Range("A1:A50").SpecialCells(xlCellTypeVisible)<o:p></o:p>
<o:p> </o:p>
How can this be done by modifying the VBA code please?
<o:p> </o:p>
Thanks a lot, <o:p></o:p>
Ecksell <o:p></o:p>
 
Upvote 0
Afternoon,
I have the same issue as Ecksell, i see that the post was back in 2008.

Has there i been a solution to this issue?
 
Upvote 0
Thanks for your replay.
unfortunately i cant use any external programs.

i have the same issue as the guy that that posted the original
 
Upvote 0
i think i may have got around it using send keys along with .display.

will post code if it works all the time, just now its working some times but not others
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,392
Members
449,445
Latest member
JJFabEngineering

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