thansen@cortinacocom
New Member
- Outlook version
- Outlook 2013 64 bit
- Email Account
- Exchange Server
I started with some code that I found on this site. I am trying to send email files to people based on the last part of the file name. The problem that I am having is that all the emails but one go to the last address in the if statement. The if statement that looks for “R95” sends to the correct address. I am at a lose to what the problem is.
Thanks for any help that I can get.
Code:
Dim fldName As String
Sub SendFilesbuEmail()
' From http://slipstick.me/njpnx
Dim sFName As String
i = 0
fldName = "K:\Commissions Reports\2014\14-12-December\"
sFName = Dir(fldName)
Do While Len(sFName) > 0
Call SendasAttachment(sFName)
sFName = Dir
i = i + 1
Debug.Print fName
Loop
MsgBox i & " files were sent"
End Sub
Function SendasAttachment(fName As String)
Dim olApp As Outlook.Application
Dim olMsg As Outlook.MailItem
Dim olAtt As Outlook.Attachments
Set olApp = Outlook.Application
Set olMsg = olApp.CreateItem(0) ' email
Set olAtt = olMsg.Attachments
' attach file
Dim fNameend As String
Dim EmailAdd As String
fNameend = Mid(fName, InStr(fName, ".") - 3, 3)
MsgBox fNameend
MsgBox fldName & fName
olAtt.Add (fldName & fName)
' send message
With olMsg
.Subject = "Here's is the commission Report"
If fNameend = "R10" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R12" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R21" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R24" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R25" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R29" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R30" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R31" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R50" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R75" Then EmailAdd = "thansen@Cortinaco.com"
If fNameend = "R95" Then EmailAdd = "thansen@Cortinaco.com" Else EmailAdd = "kerplunk363@gmail.com"
.To = EmailAdd
.HTMLBody = "Hi " & olMsg.To & ", <br /><br /> I have attached " & fName & fNameend & " as you requested."
.Send
End With
End Function
Thanks for any help that I can get.