Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 599
  • Last Modified:

VBA Email

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
W.E.B
Asked:
W.E.B
  • 11
  • 10
  • 4
1 Solution
 
Bill PrewCommented:
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
 
W.E.BAuthor Commented:
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
 
Ess KayEntrapenuerCommented:
is it always ending with txt?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
Ess KayEntrapenuerCommented:
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
 
W.E.BAuthor Commented:
.txt
or
.csv

thanks
0
 
Ess KayEntrapenuerCommented:
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
 
W.E.BAuthor Commented:
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
 
Bill PrewCommented:
Wrapped up right now, but I will be back in a bit to share some other approaches that should work in VBS.

~bp
0
 
Ess KayEntrapenuerCommented:
you need to add

Imports System.IO
0
 
W.E.BAuthor Commented:
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
 
Bill PrewCommented:
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
 
W.E.BAuthor Commented:
Hello,
I get error

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

Thanks,
0
 
Bill PrewCommented:
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
 
W.E.BAuthor Commented:
Email_Files.vbs

thanks
0
 
Bill PrewCommented:
So how do you execute it, just double clicking, or via CSCRIPT, etc?

~bp
0
 
W.E.BAuthor Commented:
just double clicking,

Thanks
0
 
Bill PrewCommented:
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
 
W.E.BAuthor Commented:
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
 
Bill PrewCommented:
Yes.  Are there any other changes that are needed?  I'd prefer to do them all at once...

~bp
0
 
W.E.BAuthor Commented:
this is it, thanks
0
 
Bill PrewCommented:
And then if there is only one email, the subject and body should always be just the "4511", right?

~bp
0
 
W.E.BAuthor Commented:
CORRECT
0
 
Bill PrewCommented:
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
 
W.E.BAuthor Commented:
Awesome,
Appreciate your time and help.
Thanks
0
 
Bill PrewCommented:
You are welcome, glad I could be of some help, thanks for the feedback.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 11
  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now