send email attachment in VBS

I am trying to send an emai from a vbs script. Everything works fine, except for the attachment, as it turns out I can't do:

 

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = subj
objMessage.To = ToAdd
objMessage.From = FromAddress
objMessage.TextBody = now() & VbCrLf & BodyText
objMessage.AddAttachment = folder & NewFileName & "." & FileExt

 

Apparently I need to specify an exact location for my file, which isn't ideal as I intend to use the script on different servers, and the filename will change every day. Is there a better way to send an email in vbs as I can't seem to find much information apart from using CDO.

 

  • Edited by iGGt Tuesday, September 07, 2010 3:28 PM correction
September 7th, 2010 3:25pm

If you are unable to specify the exact location of the attachment, how should Windows work it out? Do you have some strategy? If so, what is it?
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2010 3:27pm

the location is "folder\filename_20100907.txt".

The script is designed to create a folder in the current directory (wherever that may be) and then create the text file in that directory. But that could be c:\reports\folder\filename_20100907.txt" or it could be "d:\cust_report\folder\filename_20100907.txt"

And of course tomorrow, the filename would become "filename_20100908.txt"

September 7th, 2010 3:48pm

Are you saying that your strategy is to send this file:

{WorkingDrive}\{WorkingDirectory}\filename_YYYYMMDD.txt

If so then you could use this function
sWD = CreateObject("WScript.Shell").CurrentDirectory
for your folder and the day(now()) etc. functions for the date code.

  • Marked as answer by iGGt Wednesday, September 08, 2010 9:54 AM
Free Windows Admin Tool Kit Click here and download it now
September 7th, 2010 8:43pm

I believe the correct format is:

objMessage.AddAttachment  folder & NewFileName & "." & FileExt

(without an "=" after  .Addattachment )

Search for .Addattachment in this forum and you will see many examples.

 

SirHorace

 

 


                        

                
                        
            
  • Marked as answer by iGGt Wednesday, September 08, 2010 9:55 AM
September 8th, 2010 3:28am

Unfortunately I've already tried that:

 

  	Set objMessage = CreateObject("CDO.Message")
  	objMessage.Subject = subj
    objMessage.To = ToAdd
    objMessage.From = FromAddress
  	objMessage.TextBody = now() & VbCrLf & BodyText
  	objMessage.AddAttachment folder & NewFileName & "." & FileExt 

results in:

 

Script: C:\........

Line: 167

Char: 6

Error: Unknown Error

Code: 800C000D

Source: CDO.Message.1

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2010 7:44am

As pegasus writes you can use the Script current folder...

 

But how do you decide where to put the files in the first place ?

 

I guess you have some kind of logic that decides if you want to write your new file to c:\report or D:\newreport

 

So if you script is the same on all servers, how do you know where to place the file ?

September 8th, 2010 8:00am

This thread is getting a little confusing. Here is why:
  1. Is your problem the fact that your script does not know the name/location of the attachment or is it a syntax issue?
  2. Is this a guessing game in which we have to find out what line 167 looks like?
  3. What does your folder/file name resolve to? This modified code will tell you:
    objMessage.TextBody = now() & VbCrLf & BodyText
    wscript.echo "File path=" & folder & NewFileName & "." & FileExt
    objMessage.AddAttachment folder & NewFileName & "." & FileExt
  4. Does the file you identified above actually exist? Is it perhaps locked?
Free Windows Admin Tool Kit Click here and download it now
September 8th, 2010 8:07am

Hi,

Can you create a new variable and assign this like below:

filePath =  folder & NewFileName & "." & FileExt

 

wscript.echo filePath

see what that turns out to be.

you have to format the string properly, that is where the problem is

 

also you should not use the = after the addattachment.

here is a sample.

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "me@my.com"
objMessage.To = "test@test.com"
objMessage.TextBody = "This is some sample message text."
objMessage.AddAttachment "c:\temp\readme.txt"
objMessage.Send


September 8th, 2010 8:13am

Hi guys,

thanks for all your input. what I realised whilst answering the above questions was that

folder & NewFileName & "." & FileExt

is actually "files\filename_20100908.xls"

which is different to:

c:\reports\files\filename_20100908.xls

 

So by using:

sWD = CreateObject("WScript.Shell").CurrentDirectory

report = sWD & "/"

which gives me c:\reports/files\filename_2010908.xls

 

meaning I can now use:

addAttachment report

 

which works!!! I have an attachment

 

 

(strangely I also added an extra IF statement:

If objfso.FileExists(folder & NewFileName & "." & FileExt) Then...

around the email section to send a different message depending on whether the message was created / not created and had content / was empty) and although I got the message that the file had been created, and did contain data, I no longer got the error message. still no attachment at that stage though)

 

cheers for all your help guys

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2010 9:54am

I am a novice at VBscript..I have been assigned to send email of file(s) as a notice in the subject field from multiple folders. I am lost as to how can this be done..Any help would be much appreciated...thanks
April 13th, 2015 9:38am

I am a novice at VBscript..I have been assigned to send email of file(s) as a notice in the subject field from multiple folders. I am lost as to how can this be done..Any help would be much appreciated...thanks

Unfortunately for you, respondents in this forum are unlikely to write the script for you. Here is how you can solve your problem:

  1. Type a search phrase such as "EMail VBscript" into a Google search box. You will get lots of hits.
  2. Play with one or two of the scripts you find until you get the knack of the principle.
  3. Try to customise the script to your specific requirements.
  4. Post your script here and explain which parts you still have a problem with. Do not append your question to someone else's ancient thread. Start a thread of your own!

Free Windows Admin Tool Kit Click here and download it now
April 13th, 2015 11:43am

Unfortunately for you, respondents in this forum are unlikely to write the script for you. Here is how you can solve your problem:
  1. Type a search phrase such as "EMail VBscript" into a Google search box. You will get lots of hits.
  2. Play with one or two of the scripts you find until you get the knack of the principle.
  3. Try to customise the script to your specific requirements.
  4. Post your script here and explain which parts you still have a problem with. Do not append your question to someone else's ancient thread. Start a thread of your own!

A new thread was created for this question here:

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/c07ce69b-1ace-4e08-acdd-a29b503de7c0/vbscript-novice-needs-help?forum=ITCG

April 13th, 2015 11:48am

I am a novice at VBscript..I have been assigned to send email of file(s) as a notice in the subject field from multiple folders. I am lost as to how can this be done..Any help would be much appreciated...thanks

Unfortunately for you, respondents in this forum are unlikely to write the script for you. Here is how you can solve your problem:

  1. Type a search phrase such as "EMail VBscript" into a Google search box. You will get lots of hits.
  2. Play with one or two of the scripts you find until you get the knack of the principle.
  3. Try to customise the script to your specific requirements.
  4. Post your script here and explain which parts you still have a problem with. Do not append your question to someone else's ancient thread. Start a thread of your own!

Free Windows Admin Tool Kit Click here and download it now
April 13th, 2015 3:41pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics