Solved

VBA Email

Posted on 2013-10-28
25
571 Views
Last Modified: 2013-10-29
Hello,
I use below script to do some scheduled emails.
the problem I have is the file name.
it is not always 4511.txt
sometimes the file name is 4511-SOMENUMBERS (Example: 4511-533461.txt)
is there anyway to modify below to capture this.


MailTo = "mail@mail.com"
Subject = "4511"
Body = "4511"
Attachment = "C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511.txt"
Email MailTo, Subject, Body, Attachment
Sub Email (MailTo, Subject, Body, Attachment)
 Set App = CreateObject("Outlook.Application")
 Set Item = App.CreateItem(0)
 With Item
  .To = "mail@mail.com"
  .CC = ""
  .BCC = "mail@mail.com"
  .Subject = "4511"
  .HTMLBody = "***** 4511*****"
 End With
Set MsgAttachments = Item.Attachments
MsgAttachments.Add Attachment
 Item.Send
End Sub

Any help is appreciated,
Thanks
0
Comment
Question by:W.E.B
  • 11
  • 10
  • 4
25 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39605660
Do you mean like this?  Or are you looking for something else?

MailTo = "mail@mail.com"
Subject = "4511"
Body = "4511"
Attachment = "C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511.txt"
Email MailTo, Subject, Body, Attachment

Sub Email (MailTo, Subject, Body, Attachment)
   Set App = CreateObject("Outlook.Application")
   Set Item = App.CreateItem(0)
   With Item
      .To = "mail@mail.com"
      .CC = ""
      .BCC = "mail@mail.com"
      .Subject = Subject
      .HTMLBody = "***** " & Body & " *****"
   End With
   Set MsgAttachments = Item.Attachments
   MsgAttachments.Add Attachment
   Item.Send
End Sub

Open in new window

~bp
0
 

Author Comment

by:W.E.B
ID: 39605703
Hello,
this is what it is recognizing now as the attachment.

Attachment = "C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511.txt"

I'm trying to get it to recognize
the file name that begins with 4511 (+ what ever numbers come after)

 (Example file name: 4511-533461.txt)

4511 will always be the same, the numbers after 4511 will always change

thanks
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39605917
is it always ending with txt?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 15

Expert Comment

by:Ess Kay
ID: 39605922
You could use the GetFiles method which takes a wildcard mapping pattern and returns the files. If you are using .NET 4.0 you could also use the EnumerateFiles method which returns an IEnumerable<string> instead of an array. Once you get the files you could apply your filtering logic using LINQ extension methods to order the collection by file creation time and get the first element.

instread of
Attachment = "C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511.txt"
Attachment  = Directory.EnumerateFiles("C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511", "*.txt", SearchOption.TopDirectoryOnly).
    OrderByDescending(Function(file) New FileInfo(file).CreationTime).
    FirstOrDefault()

Open in new window

0
 

Author Comment

by:W.E.B
ID: 39605925
.txt
or
.csv

thanks
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39605938
then try this. It will get the name of last made file which starts with 4511 regardless of extension


Attachment  = Directory.EnumerateFiles("C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511", "*", SearchOption.TopDirectoryOnly).OrderByDescending(Function(file) New FileInfo(file).CreationTime).FirstOrDefault()
0
 

Author Comment

by:W.E.B
ID: 39605964
I appreciate the help,

I tried
Attachment  = Directory.EnumerateFiles("C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511", "*", SearchOption.TopDirectoryOnly).OrderByDescending(Function(file) New FileInfo(file).CreationTime).FirstOrDefault()

I get error
line 4
char 152
error syntax error
code 800A03ea

thanks
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39606381
Wrapped up right now, but I will be back in a bit to share some other approaches that should work in VBS.

~bp
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39606922
you need to add

Imports System.IO
0
 

Author Comment

by:W.E.B
ID: 39607004
Hello,
not sure where to add this?
Imports System.IO

MailTo = "mail@mail.com"
Subject = "4511"
Body = "4511"
Attachment  = Directory.EnumerateFiles("C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511", "*", SearchOption.TopDirectoryOnly).OrderByDescending(Function(file) New FileInfo(file).CreationTime).FirstOrDefault()

Email MailTo, Subject, Body, Attachment
Sub Email (MailTo, Subject, Body, Attachment)
 Set App = CreateObject("Outlook.Application")
 Set Item = App.CreateItem(0)
 With Item
  .To = "mail@mail.com"
  .CC = ""
  .BCC = "mail@mail.com"
  .Subject = "4511"
  .HTMLBody = "***** 4511*****"
 End With
Set MsgAttachments = Item.Attachments
MsgAttachments.Add Attachment
 Item.Send
End Sub


thanks,
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39607482
Give this a try.  If there are multiple matching files then you will get an email for each.

MailTo = "mail@mail.com"

Attachment = Dir("C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices\4511*.*")
Do While Attachment <> ""
   Subject = "4511"
   Body = "4511"
   Email MailTo, Subject, Body, Attachment
Loop

Sub Email (MailTo, Subject, Body, Attachment)
   Set App = CreateObject("Outlook.Application")
   Set Item = App.CreateItem(0)
   With Item
      .To = "mail@mail.com"
      .CC = ""
      .BCC = "mail@mail.com"
      .Subject = Subject
      .HTMLBody = "***** " & Body & " *****"
   End With
   Set MsgAttachments = Item.Attachments
   MsgAttachments.Add Attachment
   Item.Send
End Sub

Open in new window

~bp
0
 

Author Comment

by:W.E.B
ID: 39608216
Hello,
I get error

line 3
Char 1
Error: Type mismatch 'Dir'
code: 800A000D

Thanks,
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39608377
So, how exactly are you running this?  You posted it as a VBA question, and the code works in VBA under say Excel, I tested that here.  So now I'm wondering exactly what language / environment you are working in (VBA, VBS, Visual Basic, VB.Net, etc).  So how are you executing this "script"?

~bp
0
 

Author Comment

by:W.E.B
ID: 39608388
Email_Files.vbs

thanks
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39608517
So how do you execute it, just double clicking, or via CSCRIPT, etc?

~bp
0
 

Author Comment

by:W.E.B
ID: 39608531
just double clicking,

Thanks
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39608977
Okay, this should get the job done.  Check the values set near the top to make sure those are correct.

Also, when you find a different file name, I wasn't sure if you wanted to still have 4511 in the email subject and body, or the actual file name.  I set it up with the actual file name, and left your old statement commented out if you want it to always be 4511.

' Define location and names for file selection
strBaseDir = "C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices"
strBaseName = "4511"
arrExt = Array(".csv", ".txt")

' Specify email related information
MailTo = "mail@mail.com"
Subject = "4511"
Body = "4511"

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Access the folder where the files exist
Set objFolder = objFSO.GetFolder(strBaseDir)

' Process all files in this folder, and look for name matches
For Each objFile in objFolder.Files
   If CheckFileName(objFile.Name) Then
      Email MailTo, objFile.Name, objFile.Name, objFile.Path
      ' Email MailTo, Subject, Body, objFile.Path
   End If
Next

' Subroutine to send the email with the file attached
Sub Email (MailTo, Subject, Body, Attachment)
   Set App = CreateObject("Outlook.Application")
   Set Item = App.CreateItem(0)
   With Item
      .To = MailTo
      .CC = ""
      .BCC = MailTo
      .Subject = Subject
      .HTMLBody = "***** " & Body & " *****"
   End With
   Set MsgAttachments = Item.Attachments
   MsgAttachments.Add Attachment
   Item.Send
End Sub

' Function to determine if we should process this file or not
Function CheckFileName(strName)
   ' Assume we don't process it
   CheckFileName = False

   ' Check start of file name for a match
   If LCase(Left(strName, Len(strBaseName))) = LCase(strBaseName) Then
      ' Check included file types and see if we want this one
      For Each strExt in arrExt
         If LCase(Right(strName, Len(strExt))) = LCase(strExt) Then
            CheckFileName = True
            Exit For
         End If
      Next
   End If
End Function

Open in new window

~bp
0
 

Author Comment

by:W.E.B
ID: 39609022
thank you very mush for your time and help.

this one worked.

Is there ANY way that only one email is sent with both attachments?

thanks again,
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39609067
Yes.  Are there any other changes that are needed?  I'd prefer to do them all at once...

~bp
0
 

Author Comment

by:W.E.B
ID: 39609086
this is it, thanks
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39609101
And then if there is only one email, the subject and body should always be just the "4511", right?

~bp
0
 

Author Comment

by:W.E.B
ID: 39609119
CORRECT
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 300 total points
ID: 39609450
Okay, give this a try.

' Define location and names for file selection
strBaseDir = "C:\Users\Wassim.QA-DOMAIN\Desktop\Export Invoices"
strBaseName = "4511"
arrExt = Array(".csv", ".txt")

' Specify email related information
MailTo = "mail@mail.com"
Subject = "4511"
Body = "4511"

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Access the folder where the files exist
Set objFolder = objFSO.GetFolder(strBaseDir)

' Process all files in this folder, and look for name matches
For Each objFile in objFolder.Files
   If CheckFileName(objFile.Name) Then
      Attachments = Attachments & "," & objFile.Path
   End If
Next

If Left(Attachments, 1) = "," Then
   Attachments = Mid(Attachments, 2)
End If

If Attachments <> "" Then
   Email MailTo, Subject, Body, Attachments
End If


' Subroutine to send the email with the file attached
Sub Email (MailTo, Subject, Body, Attachments)
   Set App = CreateObject("Outlook.Application")
   Set Item = App.CreateItem(0)
   With Item
      .To = MailTo
      .CC = ""
      .BCC = MailTo
      .Subject = Subject
      .HTMLBody = "***** " & Body & " *****"
   End With
   Set MsgAttachments = Item.Attachments
   For Each Attachment In Split(Attachments, ",")
      MsgAttachments.Add Attachment
   Next
   Item.Send
End Sub

' Function to determine if we should process this file or not
Function CheckFileName(strName)
   ' Assume we don't process it
   CheckFileName = False

   ' Check start of file name for a match
   If LCase(Left(strName, Len(strBaseName))) = LCase(strBaseName) Then
      ' Check included file types and see if we want this one
      For Each strExt in arrExt
         If LCase(Right(strName, Len(strExt))) = LCase(strExt) Then
            CheckFileName = True
            Exit For
         End If
      Next
   End If
End Function

Open in new window

~bp
0
 

Author Closing Comment

by:W.E.B
ID: 39609550
Awesome,
Appreciate your time and help.
Thanks
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39609657
You are welcome, glad I could be of some help, thanks for the feedback.

~bp
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question