Using a macro to send email to diffrent address

Status
Not open for further replies.
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.
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.
 
Your construct executes the Else statement for all names except R95. Use ElseIf:
Code:
if ... then ...
elseif ... then ...
else ... 
endif

I'd prefer this way
Code:
select case fnameend
case x, y, z, "R95": emailadd="..."
case else:emailadd="..."
end select
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
H send reminder if no reply received on first or original email using macro Using Outlook 2
O using macro to send attachments Using Outlook 3
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
S Macro using .SendUsingAccount only works the first time, after starting Outlook Outlook VBA and Custom Forms 4
Sabastian Samuel HOW DO I FORWARD AN EMAIL WITH MACRO using an email that in the body of another email Outlook VBA and Custom Forms 3
D Print Attachments only in selected emails using a macro Outlook VBA and Custom Forms 3
A Forward Outlook Email by Filtering using Macro Rule Outlook VBA and Custom Forms 44
Diane Poremsky Embed Images in New Messages using a Macro Using Outlook 0
S Editing an email with notes and saving it for record using Macro Outlook VBA and Custom Forms 3
reza Macro to automatic reply using orignal mail Outlook VBA and Custom Forms 10
nathandavies How to save emails to a specific folder on a network automatically using a macro Outlook VBA and Custom Forms 14
B Auto reply using macro include original email Using Outlook 4
J Macro generating email using default signature and hyperlink Outlook VBA and Custom Forms 5
M Reply Macro using O365 Connected Account Using Outlook 2
H using macro for sending multiple mail in outlook Outlook VBA and Custom Forms 1
J Specify Font Name or Face using VBA Macro in Word Outlook VBA and Custom Forms 2
R Trigger Application_ItemSend event when sending using Access macro Outlook VBA and Custom Forms 2
H using VBA to edit subject line Outlook VBA and Custom Forms 0
e_a_g_l_e_p_i Need clarification on 2-Step Verification for Gmail using Outlook 2021 Using Outlook 10
e_a_g_l_e_p_i Outlook 2021 not letting me setup my Gmail using pop Using Outlook 1
Geldner Problem submitting SPAM using Outlook VBA Form Outlook VBA and Custom Forms 2
O How to find out the domain and server settings that my Outlook is using? Using Outlook 2
S Outlook 2019 Custom outlook Add-in using Visual Studio Outlook VBA and Custom Forms 0
D Outlook 2021 Using vba code to delete all my spamfolders not only the default one. Outlook VBA and Custom Forms 0
M using excel to sort outlook appointment items Outlook VBA and Custom Forms 4
R Advise on using multiple instances of network files based on customers Outlook VBA and Custom Forms 8
HarvMan Using Emojis in Outlook 365 Using Outlook 3
T Outlook 2019 Not Using Auto Compete After Deletion of 365 Using Outlook 1
M USING INITIALS AS RECIPIENTS Using Outlook 1
T Outlook 2019 Using Gmail aliases in Outlook Using Outlook 6
M Saving emails using Visual Basic - Selecting folder with msoFileDialogFolderPicker Outlook VBA and Custom Forms 6
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
justicefriends How to set a flag to follow up using VBA - for addressee in TO field Outlook VBA and Custom Forms 11
M Extract "Date sent" from emails (saved to folder using drag and drop) Outlook VBA and Custom Forms 1
I Outlook for Mac 2019 using on desktop and laptop IMAP on both need help with folders Using Outlook 1
David McKay VBA to manually forward using odd options Outlook VBA and Custom Forms 1
H Stationery using between OL 2019 and OL 2010 Using Outlook 0
P Prevent Outlook 2016 from using DASL filter Using Outlook 4
O Calendar - Location: what happens when using my own way of entering locations Using Outlook 1
M Disable Contact Card Results when using "Search People" in Outlook Ribbon Using Outlook 7
K can't get custom form to update multiple contacts using VBA Outlook VBA and Custom Forms 3
S Outlook VBA How to adapt this code for using in a different Mail Inbox Outlook VBA and Custom Forms 0
pcunite Outlook 2019/O365 Build 13127.20408 errors when using MAPI calls Using Outlook 1
B Change Font and Font size using VBA Outlook VBA and Custom Forms 9
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
X Using Outlook 2013 and Outlook 365 Using Outlook 1
A Going to folder using shortcuts Using Outlook 3
A Outlook replies not using "delivered to" address in From Using Outlook 1
Terry Sullivan E-Mails Sent Using a Group Box Result in 70 Kickbacks Using Outlook 4
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4

Similar threads

Back
Top