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.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
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.
Thread starter Similar threads 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 New Slipstick.com Articles 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
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 5
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
K Using Outlook 2016 to draw Using Outlook 1
O Outlook 365 - suddenly unable to send using Gmail POP3 Using Outlook 10
N Disable Auto Read Receipts sent after using Advanced Find Using Outlook 4
G Outlook 2016 sync contacts directly between phone and computer using outlook 2016 Using Outlook 0
O Save attachments using hotkey without changing attributes Outlook VBA and Custom Forms 1
J Add an Attachment Using an Array and Match first 17 Letters to Matching Template .oft to Send eMail Outlook VBA and Custom Forms 2
A Edit subject - and change conversationTopic - using VBA and redemption Outlook VBA and Custom Forms 2
A Using or not using apostrophes in search terms has this changed? Using Outlook 0
O Office 365 using POP3 on both laptop and desktop Using Outlook 0
M Using field names to capture a data element Using Outlook 0
B Vba to monitor time to respond to emails using a shared mailbox Outlook VBA and Custom Forms 5
B Looking to get the Recipient email address (or even the "friendly name") from an email I am replying to using VBA Outlook VBA and Custom Forms 4
D Using a VBA Custom Form to Send Reoccurring Email Upon Task Completion Outlook VBA and Custom Forms 4
Z Adding dropdown list using custom form Outlook VBA and Custom Forms 7
O Using .OST and .PST mail thru different providers Using Outlook 5
N Open & Save VBAProject.Otm using VBA Code Outlook VBA and Custom Forms 1
D Remove text in subject using VBA Outlook VBA and Custom Forms 4
P How to export voting results using VBA? Outlook VBA and Custom Forms 2
E Using the Like operator properly Outlook VBA and Custom Forms 1
R Using "check for duplicates" for existing contacts Using Outlook 2
S Find a cell value in excel using outlook vba Using Outlook 1
N Using email notification to update calendar events? Outlook VBA and Custom Forms 4
C Sync Calendars using WiFI Using Outlook 3
A Capturing Send Variables without using Application_ItemSend in ThisOutlookSession Outlook VBA and Custom Forms 8
L Using alpha numeric in email address Using Outlook 5
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
B query outlook using vba Outlook VBA and Custom Forms 13
M Using conditional formatting on a shared calendar with categories Using Outlook 6
Similar threads


















































Top