VB To Outlook Mail Script

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

VB To Outlook Mail Script

Post by CyberCitizen » Thu Jul 17, 2008 6:18 am

What Am I Missing?

I Am Not That Good With VB.

Code: Select all

VBStart
Function CreateEmail(strRecipient, strSubject, strBody)
   'Create OL App object
   Set objOLApp = CreateObject("Outlook.Application")

   If objOLApp is nothing then
      MsgBox "Could not create OL App.  Shutting Down"
      Exit Function
   End If

   'Create a new mail item
   Set objMsg = objOLApp.CreateItem(0)
   If objMsg is nothing then
      MsgBox "Could not create Mail Item.  Shutting Down"
      Exit Function
   End If

   'Set basic message parameters
   objMsg.To = strRecipient
   objMsg.Subject = strSubject
   objMsg.Body = strBody

   'Send the message
   objMsg.Send

   'Free up the space
   Set objOLApp = Nothing
   Set objMsg = Nothing

   CreateEmail = "Success"
End Function
VBEnd

Dialog>InfraMissing
   Caption=Asset Missing From Infra
   Width=399
   Height=261
   Top=104
   Left=16
   Max=0
   Min=0
   Close=1
   Resize=0
   Label=Please enter the details of the missing asset from Infra,8,8,true
   Label=Asset Number,16,32,true
   Edit=AssetNumber,16,48,249,
   Label=Linked Asset (Example COM1234),16,80,true
   Edit=LinkedAsset,16,96,249,
   Label=Infra Call,16,128,true
   Edit=InfraCall,16,144,249,
   Label=Customer Name,16,176,true
   Edit=CustomerName,16,192,249,
   Button=Email%CRLF%Infra%CRLF%Admin,280,48,105,73,3
   Button=Cancel,280,144,105,73,2
EndDialog>InfraMissing

Show>InfraMissing,r

If>%r%=2,Exit
If>%r%=1,Mail

Label>Mail
Let>[email protected]
Let>strSubj=Asset missing from Infra Database
Let>strBody=Asset Number: %InfraMissing.AssetNumber%%CRLF%Linked Asset: %InfraMissing.LinkedAsset%%CRLF%Infra Call: %InfraMissing.InfraCall%%CRLF%Customer Name: %InfraMissing.CustomerName%

VBEval>CreateEmail("%strTo%","%strSubj%","%strBody%"),Answer

Label>Exit

Last edited by CyberCitizen on Tue Jul 22, 2008 6:43 am, edited 1 time in total.
FIREFIGHTER

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 22, 2008 6:33 am

Add this line before the final VBEval line:

StringReplace>strBody,CRLF," & vbCRLF & ",strBody
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Mon Jul 13, 2009 2:05 am

Is There A Trick For Adding Mail Attachments?

I Have A Path Where The Attachment Will Always Be, Just Need To Add It VB.
FIREFIGHTER

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jul 13, 2009 6:07 am

Add this before objMsg.Send

objMsg.AddAttachment "c:\somedir\somefile.bla"
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

email attachments with vbscript

Post by gdyvig » Mon Jul 13, 2009 6:39 am

Hi CyberCitizen,


Our Macro Scheduler scripts just use the SMPTSendMail which does support attachments. We also have some standalone vbscripts. Here is how we do it in standalone vbscripts:

'After the basic message parameters
'For each attachment:
objMsg.AddAttachment(sAttachment)
'Send the message

I don't think it makes any difference, but our CreateObject statment is for "CDO.Message" instead of "Outlook.Application". Everything else looks about the same.


Gale

User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Tue Jul 14, 2009 4:15 am

Current Script

Code: Select all

Let>APP_TITLE=Password Fax Form
VBStart
Function CreateEmail(strRecipient, strSubject, strBody)
   'Create OL App object
   Set objOLApp = CreateObject("Outlook.Application")

   If objOLApp is nothing then
      MsgBox "Could not create OL App.  Shutting Down"
      Exit Function
   End If

   'Create a new mail item
   Set objMsg = objOLApp.CreateItem(0)
   If objMsg is nothing then
      MsgBox "Could not create Mail Item.  Shutting Down"
      Exit Function
   End If

   'Set basic message parameters
   objMsg.To = strRecipient
   objMsg.Subject = strSubject
   objMsg.Body = strBody
   objMsg.AddAttachment "\\libra\global\TSA103.doc"

   'Send the message
   objMsg.Send

   'Free up the space
   Set objOLApp = Nothing
   Set objMsg = Nothing

   CreateEmail = "Success"
End Function
VBEnd

Let>INPUT_BROWSE=0
Input>FaxNumber,Please Enter Fax Number To Send Password Reset Form%CRLF%Enter Full Phone Number Eg 0883432000,
If>%FaxNumber%=,Exit

Label>Mail
Let>[email protected]
Let>strSubj=Password Reset Fax [%FaxNumber%]
Let>strBody=Password Reset Fax [%FaxNumber%]

StringReplace>strBody,CRLF," & vbCRLF & ",strBody

VBEval>CreateEmail("%strTo%","%strSubj%","%strBody%","%strAttach%"),Answer

Label>Exit
Error Received
Image
FIREFIGHTER

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Add the attachment parameter to CreateEmail function

Post by gdyvig » Tue Jul 14, 2009 2:35 pm

Hi CyberCitizen,


You need to add a parameter to your CreateEmail vbscript function to go with the new attachment parameter you are passing in.

Change
Function CreateEmail(strRecipient, strSubject, strBody)
to
Function CreateEmail(strRecipient, strSubject, strBody,strAttachment)

Further down the function

Change
objMsg.AddAttachment "\\libra\global\TSA103.doc"
to
objMsg.AddAttachment(strAttachment)

In the macroscript where you are setting your parameters:

Add
Let>strAttach=\\libra\global\TSA103.doc

Note that I used a strAttach instead of strAttachment. The names do not have to match, but it is OK if they do match.

Make these changes and your CreateEmail vbscript function should be able to handle one attachment.

Let me know if you want to make more modifications to CreateEmail to handle multiple attachments. It can be done without adding additional parameters.


Gale

User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Mon Jul 20, 2009 5:45 am

Code: Select all

Let>APP_TITLE=Asset Missing From Infra
VBStart
Function CreateEmail(strRecipient, strSubject, strBody, strAttachment)
   'Create OL App object
   Set objOLApp = CreateObject("Outlook.Application")

   If objOLApp is nothing then
      MsgBox "Could not create OL App.  Shutting Down"
      Exit Function
   End If

   'Create a new mail item
   Set objMsg = objOLApp.CreateItem(0)
   If objMsg is nothing then
      MsgBox "Could not create Mail Item.  Shutting Down"
      Exit Function
   End If

   'Set basic message parameters
   objMsg.To = strRecipient
   objMsg.Subject = strSubject
   objMsg.Body = strBody
   objMsg.AddAttachment(strAttachment)

   'Send the message
   objMsg.Send

   'Free up the space
   Set objOLApp = Nothing
   Set objMsg = Nothing

   CreateEmail = "Success"
End Function
VBEnd

Let>INPUT_BROWSE=0
Input>FaxNumber,Please Enter Fax Number To Send Password Reset Form%CRLF%Enter Full Phone Number Eg 0883432000,
If>%FaxNumber%=,Exit

Label>Mail
Let>[email protected]
Let>strSubj=Password Reset Fax [%FaxNumber%]
Let>strBody=Password Reset Fax [%FaxNumber%]
Let>strAttach=\\libra\global\TSA103.doc

StringReplace>strBody,CRLF," & vbCRLF & ",strBody

VBEval>CreateEmail("%strTo%","%strSubj%","%strBody%","%strAttach%"),Answer

Label>Exit
Received This Error Once Compiled & Run
Image
FIREFIGHTER

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jul 20, 2009 8:35 am

Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
CyberCitizen
Automation Wizard
Posts: 724
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Mon Jul 20, 2009 10:45 am

Thanks For The Help. Both Of You, Rep Given.
FIREFIGHTER

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

This works for me

Post by gdyvig » Mon Jul 20, 2009 3:06 pm

Hi, CyberCitizen,

I found that simply changing AddAttachment to AddAttachments did not work. However I applied the information in Marcus's other link and found that this does work.

Code: Select all

   'This code failed:
   'objMsg.AddAttachments(strAttachment)
   'This code worked:
   objMsg.Save
   Set myAttachments = objMsg.Attachments  
   myAttachments.Add strAttachment
The link Marcus referred to says the syntax he recommends works for attachments by index. Attachments by value appears to be the default.

I still have one problem. I set strAttachment to a file on my c-drive. After I run the script it sends the email with an Outlook warning. If I open up Outlook on the same pc, I can read the attachment. If I open up Outlook on a different PC I see the attachment but when I try to open it Outlook says it cannot find the file.



Gale

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts