Solved

VBA Email

Posted on 2013-10-28
25
570 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 53

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 53

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 53

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 53

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 53

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 53

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 53

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 53

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 53

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 53

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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