From: PAkerly on
Is it possible to use VBScript to attach all files with a particular
extension to an email?

How could this be done?
From: Pegasus [MVP] on
"PAkerly" <pakerly(a)gmail.com> said this in news item
news:60dc06b6-6c7c-488a-8ac5-edb19784cffe(a)r14g2000vbc.googlegroups.com...
> Is it possible to use VBScript to attach all files with a particular
> extension to an email?
>
> How could this be done?

Use the File System Object to pick up the names of all files with the
desired extension, then use the script below to generate the mail message.
You need to inlcude one .AddAttachment line for each file to be attached.

const cdoBasic=1
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set objEmail = CreateObject("CDO.Message")
With objEmail
.From = "James(a)company.com"
.To = "Jim(a)company.com"
.Subject = "Test Mail"
.Textbody = "The quick brown fox " & Chr(10) & "jumps over the lazy dog"
.AddAttachment "d:\Testfile.txt"
With .Configuration.Fields
.Item (schema & "sendusing") = 2
.Item (schema & "smtpserver") = "mail.company.com"
.Item (schema & "smtpserverport") = 25
.Item (schema & "smtpauthenticate") = cdoBasic
.Item (schema & "sendusername") = "James(a)company.com"
.Item (schema & "smtpaccountname") = "James(a)company.com"
.Item (schema & "sendpassword") = "SomePassword"
End With
.Configuration.Fields.Update
.Send
End With


From: PAkerly on
On Dec 28, 7:07 pm, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
> "PAkerly" <pake...(a)gmail.com> said this in news itemnews:60dc06b6-6c7c-488a-8ac5-edb19784cffe(a)r14g2000vbc.googlegroups.com...
>
> > Is it possible to use VBScript to attach all files with a particular
> > extension to an email?
>
> > How could this be done?
>
> Use the File System Object to pick up the names of all files with the
> desired extension, then use the script below to generate the mail message..
> You need to inlcude one .AddAttachment line for each file to be attached.
>
> const cdoBasic=1
> schema = "http://schemas.microsoft.com/cdo/configuration/"
> Set objEmail = CreateObject("CDO.Message")
> With objEmail
>         .From = "Ja...(a)company.com"
>         .To = "J...(a)company.com"
>         .Subject = "Test Mail"
>         .Textbody = "The quick brown fox " & Chr(10) & "jumps over the lazy dog"
>         .AddAttachment "d:\Testfile.txt"
>         With .Configuration.Fields
>                 .Item (schema & "sendusing") = 2
>                 .Item (schema & "smtpserver") = "mail.company.com"
>                 .Item (schema & "smtpserverport") = 25
>                 .Item (schema & "smtpauthenticate") = cdoBasic
>                 .Item (schema & "sendusername") = "Ja....(a)company.com"
>                 .Item (schema & "smtpaccountname") = "Ja...(a)company.com"
>                 .Item (schema & "sendpassword") = "SomePassword"
>         End With
>         .Configuration.Fields.Update
>         .Send
> End With


OK Here is what I am doing, I thought that this would grab all files
but it is only getting and attaching one, multiple times...

'Grab all .txt files
strFolder = "C:\MYFiles"
strExt = "txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

For Each objFile in objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)

strFile = objFile.Path
next

'email the files, there are usually no more than 3 files.
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "me(a)HOST.COM"
objMessage.To = "me(a)HOST.COM"
objMessage.TextBody = "This is some sample message text."
objMessage.AddAttachment strFile
objMessage.AddAttachment strFile
objMessage.AddAttachment strFile

'==This section provides the configuration information for the remote
SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
"myserver.host.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send

Any ideas how I can make this work?
From: Pegasus [MVP] on


"PAkerly" <pakerly(a)gmail.com> said this in news item
news:af062eab-c31a-44e9-a8fe-d98788704c4b(a)h2g2000vbd.googlegroups.com...
> On Dec 28, 7:07 pm, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
>> "PAkerly" <pake...(a)gmail.com> said this in news
>> itemnews:60dc06b6-6c7c-488a-8ac5-edb19784cffe(a)r14g2000vbc.googlegroups.com...
>>
>> > Is it possible to use VBScript to attach all files with a particular
>> > extension to an email?
>>
>> > How could this be done?
>>
>> Use the File System Object to pick up the names of all files with the
>> desired extension, then use the script below to generate the mail
>> message.
>> You need to inlcude one .AddAttachment line for each file to be attached.
>>
>> const cdoBasic=1
>> schema = "http://schemas.microsoft.com/cdo/configuration/"
>> Set objEmail = CreateObject("CDO.Message")
>> With objEmail
>> .From = "Ja...(a)company.com"
>> .To = "J...(a)company.com"
>> .Subject = "Test Mail"
>> .Textbody = "The quick brown fox " & Chr(10) & "jumps over the
>> lazy dog"
>> .AddAttachment "d:\Testfile.txt"
>> With .Configuration.Fields
>> .Item (schema & "sendusing") = 2
>> .Item (schema & "smtpserver") = "mail.company.com"
>> .Item (schema & "smtpserverport") = 25
>> .Item (schema & "smtpauthenticate") = cdoBasic
>> .Item (schema & "sendusername") = "Ja...(a)company.com"
>> .Item (schema & "smtpaccountname") = "Ja...(a)company.com"
>> .Item (schema & "sendpassword") = "SomePassword"
>> End With
>> .Configuration.Fields.Update
>> .Send
>> End With
>
>
> OK Here is what I am doing, I thought that this would grab all files
> but it is only getting and attaching one, multiple times...
>
> 'Grab all .txt files
> strFolder = "C:\MYFiles"
> strExt = "txt"
>
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> Set objFolder = objFSO.GetFolder(strFolder)
>
> For Each objFile in objFolder.Files
> strFileExt = objFSO.GetExtensionName(objFile.Path)
>
> strFile = objFile.Path
> next
>
> 'email the files, there are usually no more than 3 files.
> Set objMessage = CreateObject("CDO.Message")
> objMessage.Subject = "Example CDO Message"
> objMessage.From = "me(a)HOST.COM"
> objMessage.To = "me(a)HOST.COM"
> objMessage.TextBody = "This is some sample message text."
> objMessage.AddAttachment strFile
> objMessage.AddAttachment strFile
> objMessage.AddAttachment strFile
>
> '==This section provides the configuration information for the remote
> SMTP server.
> '==Normally you will only change the server name or IP.
> objMessage.Configuration.Fields.Item _
> ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
> 'Name or IP of Remote SMTP Server
> objMessage.Configuration.Fields.Item _
> ("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
> "myserver.host.com"
> 'Server port (typically 25)
> objMessage.Configuration.Fields.Item _
> ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
> 25
> objMessage.Configuration.Fields.Update
> '==End remote SMTP server configuration section==
> objMessage.Send
>
> Any ideas how I can make this work?

When you get out of your file scanning loop then strFile is set to the name
of the *last* file found, which is not really what you want. To attach all
files, you must move the loop further down, e.g. as below. I also note that
you're not checking for the correct file extension.

objMessage.TextBody = "This is some sample message text."
For Each objFile In objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)
objMessage.AddAttachment objFile.Path
Next


From: PAkerly on
On Dec 29, 10:24 am, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
> "PAkerly" <pake...(a)gmail.com> said this in news itemnews:af062eab-c31a-44e9-a8fe-d98788704c4b(a)h2g2000vbd.googlegroups.com...
>
>
>
> > On Dec 28, 7:07 pm, "Pegasus [MVP]" <n...(a)microsoft.com> wrote:
> >> "PAkerly" <pake...(a)gmail.com> said this in news
> >> itemnews:60dc06b6-6c7c-488a-8ac5-edb19784cffe(a)r14g2000vbc.googlegroups..com...
>
> >> > Is it possible to use VBScript to attach all files with a particular
> >> > extension to an email?
>
> >> > How could this be done?
>
> >> Use the File System Object to pick up the names of all files with the
> >> desired extension, then use the script below to generate the mail
> >> message.
> >> You need to inlcude one .AddAttachment line for each file to be attached.
>
> >> const cdoBasic=1
> >> schema = "http://schemas.microsoft.com/cdo/configuration/"
> >> Set objEmail = CreateObject("CDO.Message")
> >> With objEmail
> >>         .From = "Ja...(a)company.com"
> >>         .To = "J...(a)company.com"
> >>         .Subject = "Test Mail"
> >>         .Textbody = "The quick brown fox " & Chr(10) & "jumps over the
> >> lazy dog"
> >>         .AddAttachment "d:\Testfile.txt"
> >>         With .Configuration.Fields
> >>                 .Item (schema & "sendusing") = 2
> >>                 .Item (schema & "smtpserver") = "mail.company.com"
> >>                 .Item (schema & "smtpserverport") = 25
> >>                 .Item (schema & "smtpauthenticate") = cdoBasic
> >>                 .Item (schema & "sendusername") = "Ja...(a)company.com"
> >>                 .Item (schema & "smtpaccountname") = "Ja...(a)company.com"
> >>                 .Item (schema & "sendpassword") = "SomePassword"
> >>         End With
> >>         .Configuration.Fields.Update
> >>         .Send
> >> End With
>
> > OK Here is what I am doing, I thought that this would grab all files
> > but it is only getting and attaching one, multiple times...
>
> > 'Grab all .txt files
> > strFolder = "C:\MYFiles"
> > strExt = "txt"
>
> > Set objFSO = CreateObject("Scripting.FileSystemObject")
> > Set objFolder = objFSO.GetFolder(strFolder)
>
> > For Each objFile in objFolder.Files
> >    strFileExt = objFSO.GetExtensionName(objFile.Path)
>
> >        strFile = objFile.Path
> > next
>
> > 'email the files, there are usually no more than 3 files.
> > Set objMessage = CreateObject("CDO.Message")
> > objMessage.Subject = "Example CDO Message"
> > objMessage.From = "m...(a)HOST.COM"
> > objMessage.To = "m...(a)HOST.COM"
> > objMessage.TextBody = "This is some sample message text."
> > objMessage.AddAttachment strFile
> > objMessage.AddAttachment strFile
> > objMessage.AddAttachment strFile
>
> > '==This section provides the configuration information for the remote
> > SMTP server.
> > '==Normally you will only change the server name or IP.
> > objMessage.Configuration.Fields.Item _
> > ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
> > 'Name or IP of Remote SMTP Server
> > objMessage.Configuration.Fields.Item _
> > ("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
> > "myserver.host.com"
> > 'Server port (typically 25)
> > objMessage.Configuration.Fields.Item _
> > ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
> > 25
> > objMessage.Configuration.Fields.Update
> > '==End remote SMTP server configuration section==
> > objMessage.Send
>
> > Any ideas how I can make this work?
>
> When you get out of your file scanning loop then strFile is set to the name
> of the *last* file found, which is not really what you want. To attach all
> files, you must move the loop further down, e.g. as below. I also note that
> you're not checking for the correct file extension.
>
> objMessage.TextBody = "This is some sample message text."
> For Each objFile In objFolder.Files
>     strFileExt = objFSO.GetExtensionName(objFile.Path)
>     objMessage.AddAttachment objFile.Path
> Next

Does your code only grab text files? or all files?