Jump to content
Macro Express Forums
terrypin

I'm Gonna Sit Right Down and...

Recommended Posts

... Write Myself a Letter. Well, an email.

 

In retirement I no longer need an app such as MLO, like Alexis, but I do set alarms to remind me of important ToDos. Right now I'm trying to add a macro to send an email on a special scheduled date, as a backup to my regular one. I thought I'd also learn a bit more about the External Script command at the same time. The macro works fine except for the rather serious snag that the VBS script apparently insists on user input to close a dialog. It won't let me type Enter or click the mouse to close it. Not much use for an alarm I want to set days or weeks ahead!

 

For this discussion I've shown the macro intended for immediate activation, rather than scheduled. To test just change the email address in the script. Or leave it as it is and you should get a Delivery Status Notification (Failure) email instead.

 

The dialog that won't respond to MEP is shown below. Playing with Options and Properties brought no joy. It seems  that VBS takes priority. I'm not clear why a dialog is displayed anyway as there's only one option, Outlook.  (I don't use it, but my regular email app, Agent, doesn't support IMAP.)

 

Anyone know enough about VBS to bypass that dialog please?

// This macro inserts the text contents of the clipboard into the body of a message and emails it immediately (using Outlook) to my BT address.
Delay: 0.1 seconds
Variable Set String %tBody% from the clipboard contents
Delay: 0.1 seconds
External Script: VBScript
Wait for Window Title: Choose Profile
Text Type (Simulate Keystrokes): <ENTER>

<COMMENT Value="This macro inserts the text contents of the clipboard into the body of a message and emails it immediately (using Outlook) to my BT address."/>
<DELAY Flags="\x01" Time="0.1"/>
<VARIABLE SET STRING Option="\x02" Destination="%tBody%" NoEmbeddedVars="FALSE"/>
<DELAY Flags="\x01" Time="0.1"/>
<EXTERNAL SCRIPT Language="VBScript" Dest="%tConsole%" Script="Dim ToAddress\r\nDim MessageSubject\r\nDim MessageBody\r\nDim MessageAttachment\r\n\r\nDim ol, ns, newMail\r\n\r\nToAddress = \"abc@xyz.com\"  ' Change this to your own address to test\r\nMessageSubject = \"Test using VBS\"\r\nMessageBody = \"{%}tBody{%}\"\r\n\r\nSet ol = WScript.CreateObject(\"Outlook.Application\")\r\nSet ns = ol.getNamespace(\"MAPI\")\r\nns.logon \"\",\"\",true,false\r\nSet newMail = ol.CreateItem(olMailItem)\r\nnewMail.Subject = MessageSubject\r\nnewMail.Body = MessageBody & vbCrLf\r\n\r\n' validate the recipient, just in case...\r\nSet myRecipient = ns.CreateRecipient(ToAddress)\r\nmyRecipient.Resolve\r\nIf Not myRecipient.Resolved Then\r\nMsgBox \"unknown recipient\"\r\nElse\r\n   newMail.Recipients.Add(myRecipient)\r\n   newMail.Send\r\nEnd If\r\n\r\nSet ol = Nothing" Encoding="0"/>
<WAIT FOR WINDOW TITLE Title="Choose Profile" Partial="FALSE" Wildcards="FALSE" Indefinite="TRUE" Hours="0" Minutes="0" Seconds="10"/>
<TEXT TYPE Action="0" Text="<ENTER>"/>
  

And here's the VBS script:
========================= = 
  
Dim ToAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment

Dim ol, ns, newMail

ToAddress = "abc@xyz.com"  ' Change this to your own address to test
MessageSubject = "Test using VBS"
MessageBody = "{%}tBody{%}"

Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
ns.logon "","",true,false
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf

' validate the recipient, just in case...
Set myRecipient = ns.CreateRecipient(ToAddress)
myRecipient.Resolve
If Not myRecipient.Resolved Then
MsgBox "unknown recipient"
Else
   newMail.Recipients.Add(myRecipient)
   newMail.Send
End If

Set ol = Nothing
  

 

 

ScriptDialog.jpg

Share this post


Link to post
Share on other sites

Why don't you use the MEP email command?

If using VBS, I wouldn't use MAPI. I'd use and SMTP.Client object. So I'm not much help in VBS because either I would do it in the MEP email command, or do it in VB.NET. But the SMTPCLient has the ability to send simple messages without having to create a message object. You might check it out. 

Just looking around in VBScript I see ones like this that look simple enough using CDO.Message.

Sorry I can't help you with the profile prompt. I know that's really the answer you're looking for. However if it were me I'd not involve Outlook unless I had a real need to that I couldn't do with one of these classes. 

Share this post


Link to post
Share on other sites

Thanks Cory. As mentioned, I'm only using VBS as a learning exercise. And now my curiosity is piqued.

 

The script I tried only offered me that Outlook option.

 

Ive tried a few other scripts in vain. That one you suggested looked promising but gave the error 'The transport failed to connect to the server.'

 

Knowing very little about VBS and almost nothing about the obscure workings of email delivery, servers and such, I was hoping to get by in copy/paste/edit mode, with little further investment!

 

I'll put it on the back burner for now.

 

But MEP's command works OK.

 

Terry, East Grinstead, UK

 

 

 

 

 

Share this post


Link to post
Share on other sites

That sounds like your SMTP server settings. GIve me what you have with your password and sensitive stuff redacted and I'll see if it works with my settings. 

Share this post


Link to post
Share on other sites

This works for me. I used my SendPulse account. SendPulse is free for a limited number of emails. But it's a high number. I use it for many things. You might consider setting up a free account. In any case, just change the values on lines 5-8 for your SMTP server and your to, from, etcetera on lines 12-15. 

VBScript To Send EMail.vbs

Share this post


Link to post
Share on other sites

Thanks Cory, appreciate your follow-up. But I couldn't get that working here. I tried both my BT Internet and Gmail settings (copied from my offline email program, Forte's Agent). Both gave the same error:
https://www.dropbox.com/s/9el2xdxhknbcnhc/CoryVBS-Email-2.jpg?raw=1

 

I decided against following the detailed instructions here about fixing that error, as I don't want to risk it simply out of curiosity.

https://support.microsoft.com/en-gb/help/928100/error-message-when-you-try-to-programmatically-send-an-e-mail-message

 

And as I already have at least three email accounts I'll pass on trying SendPulse.

 

It's not worth your spending much time on but here's a redacted version of what i tried:
https://www.dropbox.com/s/it9h8dz165nkp9g/CoryVBScriptToSendEMail-Gmail-Redacted.vbs?raw=1

 

 

Share this post


Link to post
Share on other sites

I appreciate you just want to move on but here are a few thoughts.

 

Either your settings are incorrect or there are security measures preventing transmission. Malware often is used to make your machine a zombie and send spam so it's a common thing that security software will try to block. And some servers are ficicy about being used this way. I tried my Office 365 server and couldn't get it to work with that script. However I do use the .NET SMTPClient class. One reasons i suggested SendPulse is that it's not finicky and often works when I can't get anything else to work. If you want, I'll email you my credentials and you can try it. It is free BTW.

SendPulse is not an email service. It offers a few different services for mass mailing mostly, but use of their SMTP servers is one of the services one can use for free if the volume is low.

I would be happy to make you a command line program for you. It would only take a few minutes in VB.NET. Or Ican make it a Windows Form you can interact with using controls in MEP. But I think a console app would be cool. I can have it accept your information as command line parameters. Maybe if the message is large, we could point it to a file. Most of my programs use a email/SMS notification feature, so it's easy for me.

Share this post


Link to post
Share on other sites

That's very kind of you, Cory, thank you, but I'm going to pass.

 

My list of 'things to learn' is not getting any smaller. So I try to resist most new temptations. Right now I'm doing my best to move beyond my 'google/copy/paste/edit' mode of writing Excel VBA macros. Never having learnt any object based programming language, that's struggle enough for now. And I also need to get back to some practical electronics in the shed workshop to continue my Arduino programming. 😉

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...