[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(1, xFname$, "11111", vbTextCompare) > 0 Then
Temat = "Company 1111"
Tresc = "ple ple 1"
MsgBox ("Sent to company 11111")
ElseIf InStr(1, xFname$, "22222", vbTextCompare) > 0 Then
Temat = "Company 2222"
Tresc = "ple ple 2"
MsgBox ("Sent to company 22222")
ElseIf InStr(1, xFname$, "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
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.
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(1, xFname$, "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.
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.
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
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.)
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"
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.
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.
Bookmarks