Solved

VBA Email

Posted on 2013-10-28
25
563 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:Wass_QA
  • 11
  • 10
  • 4
25 Comments
 
LVL 51

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:Wass_QA
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
 
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:Wass_QA
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:Wass_QA
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 51

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:Wass_QA
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 51

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:Wass_QA
ID: 39608216
Hello,
I get error

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

Thanks,
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

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:Wass_QA
ID: 39608388
Email_Files.vbs

thanks
0
 
LVL 51

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:Wass_QA
ID: 39608531
just double clicking,

Thanks
0
 
LVL 51

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:Wass_QA
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 51

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:Wass_QA
ID: 39609086
this is it, thanks
0
 
LVL 51

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:Wass_QA
ID: 39609119
CORRECT
0
 
LVL 51

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:Wass_QA
ID: 39609550
Awesome,
Appreciate your time and help.
Thanks
0
 
LVL 51

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now