:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

[VBA] Send e-mail multiple recipients

ฟอรัมถาม-ตอบปัญหาการใช้งาน MS Excel and VBA
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
kannaree
Member
Member
Posts: 128
Joined: Thu Nov 19, 2015 12:07 pm

[VBA] Send e-mail multiple recipients

#1

Post by kannaree »

สวัสดีคะ อาจารย์ หนูมีเรื่องจะถามอีกแล้วค่ะ

อยากได้แนวคิดว่าโจทย์แบบนี้จะทำในลักษณะใดดี หนูลองใช้ vloopup ข้อมูลจาก database
แต่ก็ใช้ไม่ได้ผลเพราะ เลขที่ Vlookup(Column commodity) เหมือนกัน

สมมติ ข้อมูลดิบใน sheets แรก จะมีฟิลว์กำหนด Commodity จากภาพ
ถ้า Commodity เป็น 2 จะ copy ข้อมูลใน rowนั้นๆ ส่งเมล์ (ขั้นตอนนี้ทำไปแล้วค่ะ)
ติดอยู่คือ มีข้อมูลเหมือนกัน แต่ต้องส่งเมล์หาลูกค้าใน Commodity 2 หลายราย
3.png
(ตัวอย่างข้อมูลดิบ)


จากนั้นในดาต้าเบสมีการแยก ตามรูป
1.png
อาจารย์หรือเพื่อน ๆ พอที่จะมีไอเดียให้หนูบ้างไหมคะ พอดีหนูยังไม่เก่งในการเขียน vba

รบกวนอาจารย์และเพื่อนๆ ด้วยค่ะ
2.png
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 30761
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: [VBA] Send e-mail multiple recipients

#2

Post by snasui »

:D ใช้การ Loop เข้าไปแต่ละบรรทัดแทนการใช้สูตร Lookup ค่ามา เนื่องจากสูตร Lookup มาได้เพียงค่าเดียวเท่านั้นครับ
kannaree
Member
Member
Posts: 128
Joined: Thu Nov 19, 2015 12:07 pm

Re: [VBA] Send e-mail multiple recipients

#3

Post by kannaree »

ขอบคุณคะอาจารย์ แต่หนูมีข้อสงสัยว่าถ้าเราเขียน loop check แต่ละเงื่อนไข
จะทำให้โปรแกรมช้าไหมคะ :)
User avatar
snasui
Site Admin
Site Admin
Posts: 30761
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 365
Contact:

Re: [VBA] Send e-mail multiple recipients

#4

Post by snasui »

:D การ Loop เราสามรรถเขียนให้เร็วได้ขึ้นอยุ่กับวิธีการ ซึ่งวิธีการปกติทั่วไปก็ไม่ช้านอกจากจะ Loop วนไม่รู้จบครับ
kannaree
Member
Member
Posts: 128
Joined: Thu Nov 19, 2015 12:07 pm

Re: [VBA] Send e-mail multiple recipients

#5

Post by kannaree »

อาจารย์และเพื่อนๆ ในบอร์ด พอดีหนูต้องการ เขียน loop การทำงานประมาณนี้ แต่ติดปัญหา ไม่ทราบว่า เราสามารถแก้ไขโค้ด
ในลักษณะนี้ได้อย่างไรค่ะ

เนื่องจาก Column A มีข้อมูลที่เป็น Commodity ต่างกัน หนูเขียนโค้ดให้เช็คค่า ในแต่ละ Column
A1=32
A2=35
A3=36

และใช้ loop for ในการวนค่า แล้วในรูป for มีการซ้อน if ซึ่งมีลักษณะดังรูป แต่ loop for ไม่สามารถ Next ได้หลายครั้ง
หนูจะเขียน Code ในล้กษณะนี้ได้อย่างไรคะ อันนี้คือ logic ตัวอย่างที่ไม่ใช่โค้ดจริง
1.png

Code: Select all

Private Sub CommandButton4_Click()


   Dim noSession As Object, noDatabase As Object, noDocument As Object
   Dim obAttachment As Object, EmbedObject As Object, stPath As Object
   Dim stSubject As Variant, stSubcode As Variant, stSubname As Variant, stAttachment As String, stsupplier As String
   Dim vaRecipient As Variant, subMsg, vaMsg, NameMsg, signMsg, posMsg, contMsg, strMsg As Variant

   Dim MyObj As Object, MySource As Object, file As Variant
   Dim stFile, stcode, stname As String
   Dim FName As String
  
   Const EMBED_ATTACHMENT As Long = 1454
   Const stTitle As String = "Status Active workbook"
   Const stMsg As String = "The active workbook must first be saved " & vbCrLf _
         & "before it can be sent as an attachment."
         
        
   Worksheets("tempsheet").Activate



'found file in folder --------------------------------------------------------------------------------------------
file = Dir("C:\Users\PUR_Kannaree\Desktop\File RFQ\p'kant\FILE\File Attachment\")

    While (file <> "")
    
    Dim i, LastRow As Long
    Dim fnst As String
           
        With Sheets("tempsheet")
            LastRow = Cells(Rows.Count, "A").End(xlUp).Row
          
        End With
          
    If LastRow > 0 Then
   
    For i = 2 To LastRow
   
    fnst = Range("A" & i).Value
                  
    FName = fnst
               
            If InStr(file, FName) > 0 Then
              
            stFile = file
              file = Dir
             End If
     
  
 'vlookup send email--------------------------------------------------------------------------------------------------------------------
                   Dim rng As String
                   Dim ws1, ws2 As Worksheet
                   Dim MyStringVar1, Stcustomer, StApp, Stduedate As String
                   Dim r, LastRow1 As Long
                   Set ws1 = ThisWorkbook.Sheets("template")
                   Set ws2 = ThisWorkbook.Sheets("tempsheet")
               
                   With Sheets("tempsheet")
                       LastRow1 = Cells(Rows.Count, "A").End(xlUp).Row
                      
                   End With
                        
If LastRow1 > 0 Then
rng = Range("A" & i).Value
                        
    stFile = rng
    MsgBox rng
          
                                                    
                                                   
                                
    '---- Commodity  "30" ----------------------------------------------------------------------------------------------------'
            Dim j As Integer
            
            If rng = "30" Then
                                        
                                        With ws2
                     
                                        On Error Resume Next 'add this because if value is not found, vlookup fails, you get 1004
                                        
                                        
                                        Stcustomer = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 2, False)
                                        MsgBox Stcustomer
                                        
                                        StApp = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 3, False)
                                        MsgBox StApp
                                         
                                         
                                        Stduedate = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 4, False)
                                        MsgBox Stduedate
                                        
                                        On Error GoTo 0
                                        End With
                                        
                                        
                                       For j = 1 To 3
                                       MsgBox j
                                         
                                                If j = 1 Then
                                                                               
                                                    stcode = "1500196"
                                                    stname = "EXIM"
                                                    
                                                    
                                                    MyStringVar1 = "kannaree.wawa@gmail.com"
                                                  
                                                ElseIf j = 2 Then
                                                  
                                                     stcode = "1500209"
                                                    stname = "BOSSARD"
                                                    
                                                    MyStringVar1 = "kannaree.wawa@gmail.com"
                                                    
                                                ElseIf j = 3 Then
                                                  
                                                stcode = "1500062"
                                                stname = "ITS"
                                                    
                                                MyStringVar1 = "kannaree.wawa@gmail.com"
                                                 
                                                End If
                                
                                                                                          
                                   
                                    
                                        If MyStringVar1 = "" Then
                                           MsgBox "Item not found"
                                        
                                        End If
            '--------------------------------------------------------------------------------------
                           'Check if the active workbook is saved or not
                           'If the active workbook has not been saved at all.
                           If Len(ActiveWorkbook.Path) = 0 Then
  
                              MsgBox stMsg, vbInformation, stTitle
                              Exit Sub
                           End If
                         
                           'If the changes in the active workbook has been saved or not.
                           If ActiveWorkbook.Saved = False Then
                          
                              If MsgBox("Do you want to save the changes before sending?", _
                                    vbYesNo + vbInformation, stTitle) = vbYes Then _
                                    ActiveWorkbook.Save
                           End If
                        
                           'Get the name of the recipient from the user.
                           
                            vaRecipient = MyStringVar1
                            'Array("Joe_Boe@somewhere.com", "kristen_Dean@somewhere.com", "john_doe@somewhereelse.com", "jane.doe@anotherplace.com")
                         
                            
                           
                           'If the user has canceled the operation.
                           If vaRecipient = False Then Exit Sub
                     
                           'Get the message from the user.
                           
                           subMsg = "Dear Supplier," & vbNewLine
                           
                           vaMsg = vbNewLine & "             " & "Customer Name :  " & Stcustomer & vbNewLine & "             " & "Application : " & StApp & vbNewLine & "             " & "Due date :  " & Stduedate & vbNewLine & "             " & vbNewLine & "*** Please open the file attachment for update then reply email ***" & vbNewLine
                           
                          
                           
                           signMsg = vbNewLine & vbNewLine & vbNewLine & "Best regards,"
                           
                           NameMsg = vbNewLine & vbNewLine & vbNewLine & "    " & "> Apimuk Hattakit <"
                                    
                           posMsg = vbNewLine & "Sourcing Engineer (Plastic)"
                           
                           contMsg = vbNewLine & "SVI PUBLIC COMPANY LIMITED" & vbNewLine & " 142 Moo 5 Tiwanon Road,Bangkadi," & vbNewLine & "Muang,Pathumthani 12000 Thailand" & vbNewLine & " Tel:(+66)2 105 0456 Ext.1374" & vbNewLine & "Mobile : (+66)86-600-9954" & vbNewLine & "Skype : apimuk_41961 " & vbNewLine & vbNewLine & "apimuk@svi.co.th" & vbNewLine & "http://www.svi-hq.co.th"
                           strMsg = vbNewLine & "******************************************"
                           'If the user has canceled the operation.
                           If vaMsg = False Then Exit Sub
                          
                           'Add the subject to the outgoing e-mail which also can be retrieved from the users
                           'in a similar way as above.
                           stSubject = "RFQ_"
                           stSubcode = "_" & stcode & "_"
                           stSubname = stname
                           stsupplier = stFile
                            
                           'Retrieve the path and filename of the active workbook.
                            
                            stAttachment = "C:\Users\PUR_Kannaree\Desktop\File RFQ\p'kant\FILE\File Attachment\" & stFile & ".xlsx"
                            
                            
                           'Instantiate the Lotus Notes COM's Objects.
                           Set noSession = CreateObject("Notes.NotesSession")
                           Set noDatabase = noSession.GETDATABASE("", "")
                         
                           'If Lotus Notes is not open then open the mail-part of it.
                           If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
                       
                           'Create the e-mail and the attachment.
                           Set noDocument = noDatabase.CreateDocument
                           Set obAttachment = noDocument.CreateRichTextItem("stAttachment")
                           Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
                         
                           'Add values to the created e-mail main properties.
                           With noDocument
                         
                              .Form = "Memo"
                              .SendTo = vaRecipient
                              
                              .copyTo = ""
                              .BlindCopyTo = ""
                              .Subject = stSubject & Format(Date, " mm/dd/yyyy") & stSubcode & stSubname & stsupplier
                              .Body = subMsg & vaMsg & signMsg & NameMsg & posMsg & strMsg & contMsg & strMsg
                              
                              .SaveMessageOnSend = True
                           End With
                         
                           'Send the e-mail.
                           With noDocument
                         
                              .PostedDate = Now()
                              .Send 0, vaRecipient
                           End With
                         
                           'Release objects from the memory.
                           Set EmbedObject = Nothing
                           Set obAttachment = Nothing
                           Set noDocument = Nothing
                           Set noDatabase = Nothing
                           Set noSession = Nothing
                         
                           'Activate Excel for the user.
                           AppActivate "Microsoft Excel"
                         
                           'MsgBox "The e-mail has successfully been created and distributed.", vbInformation
             
                    'End With
                     Next j
                     
                   
                                    
                    End If
               End If
                     
         Next i
                
    '---- Commodity  "32"  ----------------------------------------------------------------------------------------------------'
            
                                    Dim tw As Integer
                                    If rng = "32" Then
                                        
                                        With ws2
                     
                                        On Error Resume Next 'add this because if value is not found, vlookup fails, you get 1004
                                        
                                        
                                        Stcustomer = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 2, False)
                                        MsgBox Stcustomer
                                        
                                        StApp = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 3, False)
                                        MsgBox StApp
                                         
                                         
                                        Stduedate = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 4, False)
                                        MsgBox Stduedate
                                        
                                        On Error GoTo 0
                                        End With
                                        
                                        
                                       For tw = 1 To 1
                                       MsgBox tw
                                         
                                                If tw = 1 Then
                                                                               
                                                    stcode = "1500259"
                                                    stname = "GOKO"
                                                    
                                                    
                                                    MyStringVar1 = "kannaree.wawa@gmail.com"
                                                  
                                                
                                                    
                                             
                                                 
                                                End If
                                
                                                                                          
                                   
                                    
                                        If MyStringVar1 = "" Then
                                           MsgBox "Item not found"
                                        
                                        End If
          
     'Check if the active workbook is saved or not
                           'If the active workbook has not been saved at all.
                           If Len(ActiveWorkbook.Path) = 0 Then
  
                              MsgBox stMsg, vbInformation, stTitle
                              Exit Sub
                           End If
                         
                           'If the changes in the active workbook has been saved or not.
                           If ActiveWorkbook.Saved = False Then
                          
                              If MsgBox("Do you want to save the changes before sending?", _
                                    vbYesNo + vbInformation, stTitle) = vbYes Then _
                                    ActiveWorkbook.Save
                           End If
                        
                           'Get the name of the recipient from the user.
                           
                            vaRecipient = MyStringVar1
                            'Array("Joe_Boe@somewhere.com", "kristen_Dean@somewhere.com", "john_doe@somewhereelse.com", "jane.doe@anotherplace.com")
                         
                            
                           
                           'If the user has canceled the operation.
                           If vaRecipient = False Then Exit Sub
                     
                           'Get the message from the user.
                           
                           subMsg = "Dear Supplier," & vbNewLine
                           
                           vaMsg = vbNewLine & "             " & "Customer Name :  " & Stcustomer & vbNewLine & "             " & "Application : " & StApp & vbNewLine & "             " & "Due date :  " & Stduedate & vbNewLine & "             " & vbNewLine & "*** Please open the file attachment for update then reply email ***" & vbNewLine
                           
                           
                           signMsg = vbNewLine & vbNewLine & vbNewLine & "Best regards,"
                           
                           NameMsg = vbNewLine & vbNewLine & vbNewLine & "    " & "> Apimuk Hattakit <"
                                    
                           posMsg = vbNewLine & "Sourcing Engineer (Plastic)"
                           
                           contMsg = vbNewLine & "SVI PUBLIC COMPANY LIMITED" & vbNewLine & " 142 Moo 5 Tiwanon Road,Bangkadi," & vbNewLine & "Muang,Pathumthani 12000 Thailand" & vbNewLine & " Tel:(+66)2 105 0456 Ext.1374" & vbNewLine & "Mobile : (+66)86-600-9954" & vbNewLine & "Skype : apimuk_41961 " & vbNewLine & vbNewLine & "apimuk@svi.co.th" & vbNewLine & "http://www.svi-hq.co.th"
                           strMsg = vbNewLine & "******************************************"
                           'If the user has canceled the operation.
                           If vaMsg = False Then Exit Sub
                          
                           'Add the subject to the outgoing e-mail which also can be retrieved from the users
                           'in a similar way as above.
                           stSubject = "RFQ_"
                           stSubcode = "_" & stcode & "_"
                           stSubname = stname
                           stsupplier = stFile
                            
                           'Retrieve the path and filename of the active workbook.
                            
                            stAttachment = "C:\Users\PUR_Kannaree\Desktop\File RFQ\p'kant\FILE\File Attachment\" & stFile & ".xlsx"
                            
                            
                           'Instantiate the Lotus Notes COM's Objects.
                           Set noSession = CreateObject("Notes.NotesSession")
                           Set noDatabase = noSession.GETDATABASE("", "")
                         
                           'If Lotus Notes is not open then open the mail-part of it.
                           If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
                       
                           'Create the e-mail and the attachment.
                           Set noDocument = noDatabase.CreateDocument
                           Set obAttachment = noDocument.CreateRichTextItem("stAttachment")
                           Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
                         
                           'Add values to the created e-mail main properties.
                           With noDocument
                         
                              .Form = "Memo"
                              .SendTo = vaRecipient
                              
                              .copyTo = ""
                              .BlindCopyTo = ""
                               .Subject = stSubject & Format(Date, " mm/dd/yyyy") & stSubcode & stSubname & stsupplier
                              .Body = subMsg & vaMsg & signMsg & NameMsg & posMsg & strMsg & contMsg & strMsg
                              
                              .SaveMessageOnSend = True
                           End With
                         
                           'Send the e-mail.
                           With noDocument
                         
                              .PostedDate = Now()
                              .Send 0, vaRecipient
                           End With
                         
                           'Release objects from the memory.
                           Set EmbedObject = Nothing
                           Set obAttachment = Nothing
                           Set noDocument = Nothing
                           Set noDatabase = Nothing
                           Set noSession = Nothing
                         
                           'Activate Excel for the user.
                           AppActivate "Microsoft Excel"
                           
                      Next tw
               
                      
        End If

 

'---- Commodity  "26M"  -----------------------------------------------------------------------------------------------------

         If rng = "26M" Then
      
                                        With ws2
                     
                                            On Error Resume Next 'add this because if value is not found, vlookup fails, you get 1004
                                            
                                            Stcustomer = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 2, False)
                                            MsgBox Stcustomer
                                            
                                            StApp = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 3, False)
                                            MsgBox StApp
                                             
                                             
                                            Stduedate = Application.WorksheetFunction.VLookup(rng, ws1.Range("B2:P3000").Value, 4, False)
                                            MsgBox Stduedate
                                            
                                            On Error GoTo 0
                                        End With
                
                                        stcode = "1500xxx"
                                        stname = "YYYYY"
                                     
                                                    
                                        MyStringVar1 = "kannaree@svi.co.th"
               
               
               
         End If
     'Check if the active workbook is saved or not
                           'If the active workbook has not been saved at all.
                           If Len(ActiveWorkbook.Path) = 0 Then
  
                              MsgBox stMsg, vbInformation, stTitle
                              Exit Sub
                           End If
                         
                           'If the changes in the active workbook has been saved or not.
                           If ActiveWorkbook.Saved = False Then
                          
                              If MsgBox("Do you want to save the changes before sending?", _
                                    vbYesNo + vbInformation, stTitle) = vbYes Then _
                                    ActiveWorkbook.Save
                           End If
                        
                           'Get the name of the recipient from the user.
                           
                            vaRecipient = MyStringVar1
                            'Array("Joe_Boe@somewhere.com", "kristen_Dean@somewhere.com", "john_doe@somewhereelse.com", "jane.doe@anotherplace.com")
                         
                            
                           
                           'If the user has canceled the operation.
                           If vaRecipient = False Then Exit Sub
                     
                           'Get the message from the user.
                           
                           subMsg = "Dear Supplier," & vbNewLine
                           
                           vaMsg = vbNewLine & "             " & "Customer Name :  " & Stcustomer & vbNewLine & "             " & "Application : " & StApp & vbNewLine & "             " & "Due date :  " & Stduedate & vbNewLine & "             " & vbNewLine & "*** Please open the file attachment for update then reply email ***" & vbNewLine
                           
                           
                           signMsg = vbNewLine & vbNewLine & vbNewLine & "Best regards,"
                           
                           NameMsg = vbNewLine & vbNewLine & vbNewLine & "    " & "> Apimuk Hattakit <"
                                    
                           posMsg = vbNewLine & "Sourcing Engineer (Plastic)"
                           
                           contMsg = vbNewLine & "SVI PUBLIC COMPANY LIMITED" & vbNewLine & " 142 Moo 5 Tiwanon Road,Bangkadi," & vbNewLine & "Muang,Pathumthani 12000 Thailand" & vbNewLine & " Tel:(+66)2 105 0456 Ext.1374" & vbNewLine & "Mobile : (+66)86-600-9954" & vbNewLine & "Skype : apimuk_41961 " & vbNewLine & vbNewLine & "apimuk@svi.co.th" & vbNewLine & "http://www.svi-hq.co.th"
                           strMsg = vbNewLine & "******************************************"
                           'If the user has canceled the operation.
                           If vaMsg = False Then Exit Sub
                          
                           'Add the subject to the outgoing e-mail which also can be retrieved from the users
                           'in a similar way as above.
                           stSubject = "RFQ_"
                           stSubcode = "_" & stcode & "_"
                           stSubname = stname
                           stsupplier = stFile
                            
                           'Retrieve the path and filename of the active workbook.
                            
                            stAttachment = "C:\Users\PUR_Kannaree\Desktop\File RFQ\p'kant\FILE\File Attachment\" & stFile & ".xlsx"
                            
                            
                           'Instantiate the Lotus Notes COM's Objects.
                           Set noSession = CreateObject("Notes.NotesSession")
                           Set noDatabase = noSession.GETDATABASE("", "")
                         
                           'If Lotus Notes is not open then open the mail-part of it.
                           If noDatabase.IsOpen = False Then noDatabase.OPENMAIL
                       
                           'Create the e-mail and the attachment.
                           Set noDocument = noDatabase.CreateDocument
                           Set obAttachment = noDocument.CreateRichTextItem("stAttachment")
                           Set EmbedObject = obAttachment.EmbedObject(EMBED_ATTACHMENT, "", stAttachment)
                         
                           'Add values to the created e-mail main properties.
                           With noDocument
                         
                              .Form = "Memo"
                              .SendTo = vaRecipient
                              
                              .copyTo = ""
                              .BlindCopyTo = ""
                               .Subject = stSubject & Format(Date, " mm/dd/yyyy") & stSubcode & stSubname & stsupplier
                              .Body = subMsg & vaMsg & signMsg & NameMsg & posMsg & strMsg & contMsg & strMsg
                              
                              .SaveMessageOnSend = True
                           End With
                         
                           'Send the e-mail.
                           With noDocument
                         
                              .PostedDate = Now()
                              .Send 0, vaRecipient
                           End With
                         
                           'Release objects from the memory.
                           Set EmbedObject = Nothing
                           Set obAttachment = Nothing
                           Set noDocument = Nothing
                           Set noDatabase = Nothing
                           Set noSession = Nothing
                         
                           'Activate Excel for the user.
                           AppActivate "Microsoft Excel"









'----------------------------------------------------------------------------------------------------------------------------
End If
       
     MsgBox "The e-mail has successfully been created and distributed.", vbInformation
    
        Exit Sub

    Wend

  
End Sub



ขอบคุณค่ะ
You do not have the required permissions to view the files attached to this post.
kannaree
Member
Member
Posts: 128
Joined: Thu Nov 19, 2015 12:07 pm

Re: [VBA] Send e-mail multiple recipients

#6

Post by kannaree »

ลืมไปบรรทัดนึงค่ะ ขอแก้ไขรูป
1.png
You do not have the required permissions to view the files attached to this post.
kannaree
Member
Member
Posts: 128
Joined: Thu Nov 19, 2015 12:07 pm

Re: [VBA] Send e-mail multiple recipients

#7

Post by kannaree »

:) หนูทำได้แล้วค่ะ หนูก็ย้าย next loop แรก มาไว้ล่างสุด


ขอบคุณค่ะ
Post Reply