Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A program is trying to automatically send email on your behalf 2

Status
Not open for further replies.

PWD

Technical User
Jul 12, 2002
823
GB
Good afternoon. I have inherited the following code in VB6 for send e-mails.

Code:
Public Sub email()

Dim oOApp
Dim olMailItem
Dim oOMail

Set oOApp = CreateObject("Outlook.Application")

Set oOMail = oOApp.CreateItem(olMailItem)

If strProvider = "Coins" Then

With oOMail
        .Subject = "E-Business Invoice File"
        .Body = "MLM & WTE Invoices - see please attachment."
        .To = strMailadd
        .Attachments.Add edifilename
        .Send
End With

Is it possible to use something like
Code:
.SendKeys ("{TAB 2}")
.SendKeys ("{Enter}")
in order to get past the “A program is trying to automatically send email on your behalf” window that pops up & stops you in your tracks? Probably not. I am using Outlook 2003 running through Exchange on my firm’s network so I don’t want to think about changing global security settings.

Is there any other way without resorting to 3rd party software?

I’ve had a look at thread329-664947 and extracted the following code
Code:
Public Sub email()
Dim myOlapp
Dim olMailItem
Dim myItem
Dim myAttachments

Set myOlapp = CreateObject("Outlook.Application")
Set myItem = myOlapp.CreateItem(olMailItem)
'myItem.Display     '-- Optional --
myItem.To = "des.lavender@mlmuk.com"
myItem.Subject = "Message for you sir!"
myItem.Body = "Body here."
'myItem.Save       '-- It's advised to save items before
                  '     adding attachments.
Set myAttachments = myItem.Attachments
myAttachments.Add edifilename

'myItem.Send  '<-- This causes the "click yes" message

'Instead of myItem.Send, use the SendKeys feature.
AppActivate myItem
SendKeys ("%s")

End Sub
but it just seems to jump to the next bit of code from where this was called in the VB6 project and not do anything. Grrrr.

Am I close? Am I nuts?

Many thanks,
Des.
 
We never succeeded without using third-party apps. I know you want to avoid them, but I'll post a link to what we ended up using. It works well for us, because it's free and unobtrusive.


"We can categorically state that we have not released man-eating badgers into the area" - Major Mike Shearer
 
Using Outlook in this way there's no way to bypass the Outlook Object Model Guard's security message (as far as I know).

It might be worth looking into CDO to send the e-mail.

If you are really wanting to use sendkeys to achieve this (I'm not a huge fan of sendkeys so I'm not recommending this) you just need to uncomment the myItem.Display line as if it's not displayed, AppActivate won't find it.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thanks. The thing is there's a version of my (ex) colleagues program running on another PC in another office that doesn't give the message. Ah!! Just checked and it's only running Outlook Express. That's probably why they don't get this problem. Dang.
 
Hi HarleyQuinn. I see what uncommenting does, however the
Code:
AppActivate myItem
doesn't seem to give focus to the message so the '%s' (Alt + "S") has no effect. So close, it seems.

Des.
 
Hmm, works fine on my machine. Strange [ponder]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Dang. Perhaps I'm just being thick again but how can the VB6 program AND the e-mail have focus?

“It’s good to talk.”

It doesn’t work when you F8 ‘walkthough’ the code but it does when I just run it.

Yea. Result!! Many thanks.
Des.
 
Glad to help, thanks for the star [smile]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I have registered with this site purely to say thank you for solving this problem - I've searched everywhere and some of the solutions are incredibly complicated and technical (and didn't work!).

This just needed an extra couple of lines of code and problem solved.

I can now automate pretty much my entire job... which is a bit worrying!

Thanks
 
I was clearly not paying attention when this question was originally asked.

If I was obliged to work through Outlook for this I'd have used Outlook Redemption (which, despite the name, isn't an Outlook add-in)

Once installed on your box, and using late binding as you already are, the only change you'd have to make to your code would be:
Code:
Public Sub email()

Dim oOApp
Dim olMailItem
Dim oOMail
[b][blue]Dim objSafeMail[/blue][/b]

Set oOApp = CreateObject("Outlook.Application")

Set oOMail = oOApp.CreateItem(olMailItem)

If strProvider = "Coins" Then

With oOMail
        .Subject = "E-Business Invoice File"
        .Body = "MLM & WTE Invoices - see please attachment."
        .To = strMailadd
        .Attachments.Add edifilename
        [b][blue].Save 
        Set objSafeMail = CreateObject("Redemption.SafeMailItem")
        objSafeMail.Item = oOMail
        objSafeMail.Send[/blue][/b]
End With
 
>AppActivate myItem

>doesn't seem to give focus to the message so the '%s' (Alt + "S") has no effect. So close, it seems.

You may need to use ALt + TAB "(%{tab})" to workaround the focus issue. I had to use it to automate an IBM Client Access install.

-Geates
 
Geates

Did you uncomment
'myItem.Display '-- Optional --

I've distilled the original (vb6) code a little to;

Public Sub email()

Dim myItem As Object 'drop the 'As Object' in VBScript

Const olMailItem = 0

Set myItem = CreateObject("Outlook.Application").CreateItem(olMailItem)
With myItem
.To = "hugh@xxxxxxxxx.com"
.Subject = "Message for you sir!"
.Body = "Body here."
.Attachments.Add CurDir$ & "\MyFile.txt"

.Display 'essential for the call to AppActivate below - the Outlook UI appears for a fleeting moment
'.Send 'This would cause the security message to be displayed
End With

'Instead of myItem.Send, use SendKeys
AppActivate myItem 'default property of myItem returns window caption of the message
Sendkeys "%s" 'Alt-S, hotkey to click the Send button

End Sub

and it appears to work reliably (in VB6).
 
Oh, I didn't run the code. I was just suggesting a possible workaround to an instance where a windows' properties are not conducive to AppActivate (an inability to focus).

-Geates
 
I just want to ask a question for clarification. Please forgive me for being thick. It's early here.

I currently email from MS Access. I have a program that uses VBA to track files created in a directory and send an email when the files are found or changed.

I use this, believe it or not, because I'm not permitted to have a development tool such as Visual Basic or C#. It's amazing what Access can do with APIs and such.

I want to get out of using Access for this purpose. I'm attempting to email from a VBScript. So, I came upon this thread hoping for some guidence.

I want to migrate my application from Access to VBScript. I'm new to VBScript. I just "discovered" it when I was searching for other solutions.

This thread referencing VB6 code. But since it's in the VBScrcipt forum, it's safe to assume that you're also looking to migrate to VBScript as well.

This is the code I found, but doesn't work. It breaks on line 11 which is where I create my object for Outlook. I compact my Access Databases from VBScript and that same "CreateObject" does work when I try to create an Access object. Here my non-functional code:

Code:
Dim ToAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail

'ToAddress = “ce@xxx.com” ‘ change this…
'MessageSubject = “VBS MAPI HowTo”
'MessageBody = “*BODY* email via MAPI *BODY*”

Set ol = CreateObject(”Outlook.Application”)
Set ns = ol.getNamespace(”MAPI”)
ns.logon “”,”",true,false
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = “VBS MAPI HowTo”
newMail.Body = “*BODY* email via MAPI *BODY*”
‘ validate the recipient, just in case…
Set myRecipient = ns.CreateRecipient(“ce@xxx.com”)
myRecipient.Resolve
If Not myRecipient.Resolved Then
MsgBox “unknown recipient”
Else
newMail.Recipients.Add(myRecipient)
newMail.Send
End If
Set ol = Nothing


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top