+ Reply to Thread
Results 1 to 10 of 10

[VBA] Problem with outmail function - function sends mails only to 1 recepient

Hybrid View

sauron12 [VBA] Problem with outmail... 11-20-2015, 01:36 PM
Richard Buttrey Re: [VBA] Problem with... 11-20-2015, 01:50 PM
sauron12 Re: [VBA] Problem with... 11-20-2015, 02:04 PM
Richard Buttrey Re: [VBA] Problem with... 11-20-2015, 05:17 PM
dafer660 Re: [VBA] Problem with... 11-20-2015, 04:37 PM
sauron12 Re: [VBA] Problem with... 11-20-2015, 04:46 PM
sauron12 Re: [VBA] Problem with... 11-20-2015, 05:31 PM
Richard Buttrey Re: [VBA] Problem with... 11-20-2015, 05:57 PM
dafer660 Re: [VBA] Problem with... 11-21-2015, 11:19 AM
sauron12 Re: [VBA] Problem with... 11-21-2015, 03:30 PM
  1. #1
    Registered User
    Join Date
    11-20-2015
    Location
    Poznan
    MS-Off Ver
    Office 2007
    Posts
    5

    [VBA] Problem with outmail function - function sends mails only to 1 recepient

    Hello everyone,

    This is my first post on this forum, so im glad to meet you experts .
    Please help me with following my code.
    It's part of code. Everything works, except one thing.
    When we open dialog popup and choosing path, VBA is searching for all documents with *.pdf extension.
    In order to name of file, code should send email with attached document to recipients.
    If code finds "2222" in part of name file, email should go to company no 2. If code finds "1111", email should be send to another receiver.
    But it doesnt work properly. It's only send 1 email (number 33333), but it should send all 3 emails (because I put 3 documents in folder with names: "11111", "22222" and "33333").

    Can you help me and tell me where is mistake in my code? Im newbie at VBA so I will be grateful for forgiving attitude.


    PHP Code: 
       Do While xFname$ <> ""
          
         
    If InStr(1xFname$, "11111"vbTextCompare) > 0 Then
                        Temat 
    "Company 1111"
                        
    Tresc "ple ple 1"
                           
    MsgBox ("Sent to company 11111")


                        ElseIf 
    InStr(1xFname$, "22222"vbTextCompare) > 0 Then
                                Temat 
    "Company 2222"
                                
    Tresc "ple ple 2"
                                  
    MsgBox ("Sent to company 22222")
                                

                                ElseIf 
    InStr(1xFname$, "33333"vbTextCompare) > 0 Then
                                  Temat 
    "Company 3333"
                                  
    Tresc "ple ple 3"
                                         
    MsgBox ("Send to company 33333")



                        
                 
    End If
                 
    On Error Resume Next
                With OutMail
                                     
    .To "my@mail.com"
                                     
    .CC ""
                                     
    .BCC ""
                                     
    .Subject Temat
                                     
    .HTMLBody Tresc
                                     
    .Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_33333.pdf")
                                     .
    Display
                                     
    .Send
                                         MsgBox 
    ("Tresc")
                                     
    End With
                                     
                                     


             ActiveCell
    .Offset(xRow) = xFname$
             
    xRow xRow 1
             xFname
    $ = Dir
                      xDirect
    $ = .SelectedItems(1) & "\"
         Loop
       End If
       End With
      On Error GoTo 0

        Set OutMail = Nothing
        Set OutApp = Nothing 

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    Hi,

    That's because you ARE only adding one attachment. One way would be to build in an IF Test. i.e.
    I'm not clear whether you're wanting to send 1 file if the company is 11111, 2 files if the company is 22222 or 3 files if 33333, but perhaps youj can use something like
    IF InStr(1, xFname$, "11111", vbTextCompare) > 0 Then .Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_11111.pdf") 
    IF InStr(1, xFname$, "22222", vbTextCompare) > 0 Then 
       .Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_11111.pdf") 
       .Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_22222.pdf") 
    End If
    IF InStr(1, xFname$, "33333", vbTextCompare) > 0 Then 
       .Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_11111.pdf") 
       .Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_22222.pdf") 
       .Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_33333.pdf") 
    End If
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    11-20-2015
    Location
    Poznan
    MS-Off Ver
    Office 2007
    Posts
    5

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    thanks for reply, but I need something else.
    Attachment isnt clue, code should send 3 mails, but it send only 1.

    My code is correct with sending emails - I mean:
    In folder I have 3 files:
    - 11111.pdf
    - 22222.pdf
    - 33333.pdf

    If loop find file 11111.pdf - email should be send to Receiver A (email1@email.com).
    If loop find file 22222.pdf - email should be send to Receiver B (email2@email.com)
    if loop find file 33333.pdf - email should be send to Receiver C (email3@email.com).

    But code doesnt works good. Despite I have 3 correct named files in folder - code find obiously 3 different files in pdf, but it send only one email - to receiver C, but it should send 3 different mails to 3 different email addresses.
    It's running only this part of code properly:
    PHP Code: 
    ElseIf InStr(1xFname$, "33333"vbTextCompare) > 0 Then 
                                  Temat 
    "Company 3333" 
                                  
    Tresc "ple ple 3" 
                                         
    MsgBox ("Send to company 33333"
    But in folder there is two other good files, and they should be also send to other receivers.
    Last edited by sauron12; 11-20-2015 at 02:07 PM.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    Hi,

    In that case I'd use something like

    Dim  stEmail As String
    
    If InStr(1, xFname$, "11111", vbTextCompare)>0 then stEmail = "email1@email.com"
    If InStr(1, xFname$, "22222", vbTextCompare)>0 then stEmail = "email2@email.com"
    If InStr(1, xFname$, "33333", vbTextCompare)>0 then stEmail = "email3@email.com"
    
    With OutMail 
         .To = stEmail 
    
    '.... etc.

  5. #5
    Registered User
    Join Date
    10-18-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    2010
    Posts
    38

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    Greetings,

    Whenever the outmail code runs, you only specify the document in the path which is: ".Attachments.Add ("C:\Users\plmackry\Desktop\testowy folder\565389963_33333.pdf")"

    For it to send another attachment according to the instr found you shoule instead put the ".Attachments.Add mystring" and consider the mystring as the path of the file to send.

    Meaning :

    if this statement is true InStr(1, xFname$, "11111", vbTextCompare) then
    mystring = path of file with "11111"
    else
    goto next one

    So you should just consider the mystring when you define Temar or Tresc.

    Since you only compare the files and no attachment is placed according to the compared files the attachment will always be the one that you put in .Attachment.Add

    Regards,
    Daniel

  6. #6
    Registered User
    Join Date
    11-20-2015
    Location
    Poznan
    MS-Off Ver
    Office 2007
    Posts
    5

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    Hi Dafer, thanks for reply, but it's not important to adding attachments. It will be corrected after and add variable with changing path to file.

    On this stage I'm working with sending 3 mails if script find 3 docs in folder with specified names.
    Problem is in sending mails. Script should send 3 mails (its not important what attachment, text) on address my@mail.com, but it send only 1 mail (it's working only third IF statement, because I receive mail with "Company 3333" in subject.)
    Last edited by sauron12; 11-20-2015 at 05:11 PM.

  7. #7
    Registered User
    Join Date
    11-20-2015
    Location
    Poznan
    MS-Off Ver
    Office 2007
    Posts
    5

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    noo. It's not important. I will change different mails after.

    Now I'm trying to fix not working part code with sending emails. Email receiver can be at this moment the same: "Example@example.com".
    Why script are send ONLY 1 email, when I have in folder 3 files corrected names.
    Script should send one mail (doesnt matter what email) with subject "1111" if there exist pdf file named "1111",
    second mail (subject 22222) should be send if script will find PDF named "2222", and third mail (3333 in subject) should be send if script will find PDF named "3333"

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    Sorry but I don't understand what you're wanting to do. Your original said you wanted to send all three emails but now you're saying only send one email so I'm struggling a bit.

  9. #9
    Registered User
    Join Date
    10-18-2015
    Location
    Lisbon, Portugal
    MS-Off Ver
    2010
    Posts
    38

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    It would be better if you post any attachment with parts of your code so that your needs are understood correctly.

    But i'm assuming that if no other email is sent more then once, then the loop ends after the first IF.

    Create a breakpoint in your code and go step by step using F8 in VBA. i'm sure it will help you understand why the loop is not incrementing and ending instead.

    Regards,
    Daniel

  10. #10
    Registered User
    Join Date
    11-20-2015
    Location
    Poznan
    MS-Off Ver
    Office 2007
    Posts
    5

    Re: [VBA] Problem with outmail function - function sends mails only to 1 recepient

    Hello Daniel and Richard,
    Already I'm managed with problem

    Code works, I need add "call" instruction to each section "IF":
    PHP Code: 
    If InStr(1xFname"11111"vbTextCompare) > 0 Then
                        Adresmailowy 
    "firma1@firma.com"
                        
    Temat "Firma 1111"
                        
    Tresc "ple ple 1"
                           
    MsgBox ("WYSYŁAM DO Firmy 11111")
                           
    Call Mail2(AdresmailowyTematTrescxFnamexDirect)


                        ElseIf 
    InStr(1xFname"22222"vbTextCompare) > 0 Then
                                Adresmailowy 
    "firma2@firma.com"
                                
    Temat "Firma 2222"
                                
    Tresc "ple ple 2"
                                  
    MsgBox ("Wysyłam do Firmy 22222")
                                  
    Call Mail2(AdresmailowyTematTrescxFnamexDirect)
                                

                                ElseIf 
    InStr(1xFname"33333"vbTextCompare) > 0 Then
                                  Adresmailowy 
    "firma3@firma.com"
                                  
    Temat "Firma 3333"
                                  
    Tresc "ple ple 3"
                                         
    MsgBox ("Wysyłam do Firmy 33333")
                                         
    Call Mail2(AdresmailowyTematTrescxFnamexDirect
    And I added outmail function in other section:
    PHP Code: 
    Sub Mail2(Adresmailowy As StringTemat As StringTresc As StringxFname As StringxDirect As String)

        
    Dim OutApp As Object
        Dim OutMail 
    As Object
        Set OutApp 
    CreateObject("Outlook.Application")
        
    Set OutMail OutApp.CreateItem(0)
        
        
                    
    With OutMail
                               
    .To Adresmailowy
                               
    .CC ""
                               
    .BCC ""
                               
    .Subject Temat
                               
    .HTMLBody Tresc
                               
    .Attachments.Add (xDirect xFname)
                               .
    Display
                               
    .Send
                               
                         End With
                                     On Error 
    GoTo 0
                                       Set OutMail 
    Nothing
    End Sub 
    And it works properly. It sends 3 mails if code found files named: "1111", "2222", "3333".
    Thanks guys!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Unable to send mails problem
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2015, 09:32 PM
  2. Problem in delivering the mails
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2014, 01:42 AM
  3. Set OutMail =
    By mattress58 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-08-2013, 10:22 AM
  4. VBS/OutMail script and macro sends email message twice
    By Andrew_Harris in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2013, 08:57 PM
  5. Problem Inserting Round function into an IF function
    By Ash87 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-11-2013, 05:37 PM
  6. Problem with deleting mails
    By Reema in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 06-03-2009, 02:37 AM
  7. Routing Recepient mail failure
    By asteinhoff in forum Excel General
    Replies: 0
    Last Post: 03-24-2005, 11:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1