snasui.com ยินดีต้อนรับ
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
ฟอรัมถาม-ตอบปัญหาการใช้งาน Macro และ VBA
Forum rules
ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#1
Post
by nutpochan » Fri Dec 04, 2015 5:14 pm
ผมพยายามใช้ vba ส่ง hotmail จากโค๊ดก็คือ ขออนุญาติโพสโค๊ดครับ
Code: Select all
Sub SendEmail_Outlook()
Const myEmailAddress = "nutpochan@hotmail.com"
Const myEmailPassword = "ขอเก็บเป็นความลับ"
With CreateObject("CDO.message")
With .Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp-mail.outlook.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 421
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = myEmailAddress
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = myEmailPassword
.Update
End With
.To = "nutpochan@hotmail.com"
.From = myEmailAddress
.Subject = "subject"
.TextBody = "the body"
.Send
End With
End Sub
แต่ผลที่ออกมาเป็นเหมือนในรูปภาพที่ผมแนบมาด้วย พร้อมกับไฟล์ excell VBA ที่ผมแนบมาครับ
อยากทราบว่าผมควรแก้ไขโค๊ดตรงส่วนไหนครับ
Attachments
Booking SRR (1) ส่งวันที่ 4 ธ.ค. 58.xlsm
(21.22 KiB) Downloaded 7 times
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#3
Post
by snasui » Fri Dec 04, 2015 7:01 pm
การแนบภาพแนบไฟล์ให้แนบมาในฟอรัมนี้ ตัดเฉพาะกรอบที่แสดงปัญหา ภาพจะได้ไม่เกิน 800*600 ครับ
ตัวอย่าง Code การส่งอีเมลทาง Hotmail ครับ
Code: Select all
Sub SendMailByHotmail()
Const myEmailAddress = "abcdef@hotmail.com"
Const myEmailPassword = "xxyyzz"
With CreateObject("CDO.message")
With .Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.live.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = myEmailAddress
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = myEmailPassword
.Update
End With
.To = "ghijkl@gmail.com"
.From = myEmailAddress
.Subject = "subject"
.TextBody = "the body"
.Send
End With
End Sub
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#4
Post
by nutpochan » Fri Dec 04, 2015 11:12 pm
ผมได้นำโค๊ดที่อาจารย์ให้ไปใช้ แล้วยังติดเหมือนเดิมตามรูป ที่แนบมาครับ ผมต้องไปตั้งค่าตรงไหนก่อนหรือเปล่า
Attachments
eee.png (30.55 KiB) Viewed 344 times
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#5
Post
by nutpochan » Fri Dec 04, 2015 11:16 pm
ตอนที่ผมเอาไปใช้ผมเปลี่ยนแปลงแค่ 3ที่คือ
Code: Select all
Const myEmailAddress = "nutpochan@hotmail.com"
Const myEmailPassword = "***********"
.To = "nutpochan@hotmail.com"
หรือผมต้องเปลี่ยนที่อื่นในโค๊ดด้วยครับ
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#6
Post
by snasui » Fri Dec 04, 2015 11:20 pm
ถ้า Copy ที่ผมเขียนไปใช้ จะเปลี่ยนแค่ 3 ที่ และต้องต่อ Internet ครับ
ในเครื่องผมสามารถส่งอีเมลได้ปกติ ไม่มีปัญหาใดครับ
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#7
Post
by nutpochan » Fri Dec 04, 2015 11:24 pm
หรือผมต้องลง ซอฟแวร์ตัวไหนเพิ่มเติม ครับ
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#8
Post
by snasui » Fri Dec 04, 2015 11:25 pm
อ่านที่ผมเขียนไปด้านบน ผมเพิ่มข้อความที่ต้องสังเกตเพิ่มเติมไว้แล้ว เพราะดูเหมือนว่าไม่ได้ copy ที่ผมเขียนไปใช้ครับ
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#9
Post
by nutpochan » Sat Dec 05, 2015 6:18 am
ผม ก็อปปี้ ตามโค๊ดของอาจารย์ทั้งหมดนั้นแหละครับ เพียงแต่ ตัวหนังสือสีเขียวผมเปลี่ยนแปลงเป็นอีเมลของผม
Code: Select all
Sub SendMailByHotmail()
[color=#80FF40] Const myEmailAddress = "abcdef@hotmail.com"
Const myEmailPassword = "xxyyzz"[/color]
With CreateObject("CDO.message")
With .Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.live.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = myEmailAddress
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = myEmailPassword
.Update
End With
.To = [color=#80FF00]"ghijkl@gmail.com"[/color]
.From = myEmailAddress
.Subject = "subject"
.TextBody = "the body"
.Send
End With
End Sub
แต่เมื่อผมกดrunแล้ว มันยังเป็นdebug เหมือนกับไฟล์แนบมาครับ
Attachments
eee.png (30.55 KiB) Viewed 336 times
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#11
Post
by nutpochan » Sat Dec 05, 2015 12:11 pm
แก้ภาพeror มาใหม่ให้ใหญ่กว่าเดิมแล้วครับ
Attachments
eee.png (19.45 KiB) Viewed 331 times
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#12
Post
by snasui » Sat Dec 05, 2015 2:42 pm
ตรงบรรทัด
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465 ลองเปลี่ยนจาก 465 เป็น 25 ดูครับ หากยังไม่ได้ก็ช่วยไม่ได้เช่นกัน เพราะในเครื่องผมทำงานด้วย Code ที่ผมตอบไปก่อนหน้านี้ได้ปกติครับ
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#13
Post
by nutpochan » Sat Dec 05, 2015 4:45 pm
อาจารย์ใช้ excell 2000 อะไรตอรรันหรอครับ
ก่อนผมรันผมต้องเข้า microsoft outlookก่อนไหม
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#14
Post
by snasui » Sat Dec 05, 2015 5:33 pm
ผมใช้ Excel 2010, 2013, 2016
เท่าที่จำได้น่าจะเป็น Version 2013 และไม่จำเป็นต้องใช้ Outlook ครับ
ด้านล่างเป็นตัวอย่างสำหรับการส่งอีเมลผ่าน Gmail ผม Run ด้วย Excel 2010 แล้วพบว่าสามารถทำงานได้ปกติครับ
Code: Select all
Sub CDO_Mail_With_Gmail()
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Const gmailUser As String = "YourEmail" '1
Const gmailPassword As String = "YourPassword" '2
Const strUrl = "http://schemas.microsoft.com/cdo/configuration/"
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1
Set Flds = iConf.Fields
With Flds
.Item(strUrl & "smtpusessl") = True
.Item(strUrl & "smtpauthenticate") = 1
.Item(strUrl & "sendusername") = gmailUser
.Item(strUrl & "sendpassword") = gmailPassword
.Item(strUrl & "smtpserver") = "smtp.gmail.com"
.Item(strUrl & "sendusing") = 2
.Item(strUrl & "smtpserverport") = 465
.Update
End With
strbody = "Hello"
With iMsg
Set .Configuration = iConf
.To = "EmailAddressOfRecipient" '3
.CC = ""
.BCC = ""
.From = """YourName"" <YourEmail>" '4
.Subject = "Important message"
.TextBody = strbody
.Send
End With
End Sub
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#15
Post
by nutpochan » Sat Dec 05, 2015 5:40 pm
ตรงที่ บทความของ ron
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
"CDO.Message" จะใส่เป็น email ของเรา หรือไม่ต้องเปลี่ยน
"CDO.Configuration" จะใส่เป็น password ของเราหรือไม่ต้องเปลี่ยนครับ
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#16
Post
by snasui » Sat Dec 05, 2015 5:43 pm
ดูตัวอย่างการใช้งานที่ผมโพสต์ไปด้านบน
ของ Ron จะบอกไว้ชัดเจนแล้วว่าตรงไหนต้องเปลี่ยนหรือไม่เปลี่ยนครับ
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#17
Post
by nutpochan » Sat Dec 05, 2015 11:48 pm
อั๊ยยะ ดีใจมากรู้วิธีแก้แล้วครับ แชร์ให้คนที่ไม่รู้
ให้เครดิตกระทู้นี้หน่อยเป็นประโยชน์มาก
https://community.qlik.com/thread/25067
คือว่าพ้อยมันอยู่ที่ถ้าโค๊ดเขียนถูกทุกอย่าง แต่ในบัญชีผู้ใช้ไม่อนุญาติแอปที่มีความปลอดภัยน้อย ก็ส่งไม่ได้ก็เด้ง
ดังนั้นเหมือนเขียนโค็ดถูกแล้วใคร มีปัญญา โค๊ด eror แบบนี้
The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available
อย่าลืมไปตั้งค่าเปิดที่
https://www.google.com/settings/security/lesssecureapps นะครับ
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#18
Post
by nutpochan » Sun Dec 06, 2015 4:20 pm
จากโค๊ดที่แนบมา ถ้าเราอยากให้แนบไฟล์ซึ่งผมเขียนเป็น "C:\Users\โฮม\Desktop\dfdfงาน1.xlsm" แต่ทีนี้ผมอยากให้แนบไฟล์ที่เป็น workbook ปัจจุบันที่ผมใช้งานอยู่ ผมต้องแก้โค๊ดเป็นยังไงครับ โดยเราไม่ต้องมานั่งพิมที่อยู่โค๊ดที่อยู่ไฟล์ใหม่ทุกวัน โดยให้มันอ่านที่อยู่ไฟล์เอง
Code: Select all
' Object creation
Set objMsg = CreateObject("CDO.Message")
Set msgConf = CreateObject("CDO.Configuration")
' Server Configuration
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "nutpochan1@gmail.com"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "************************"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
msgConf.Fields.Update
'send mutiplier email
' Email
objMsg.To = "nutpochan1@gmail.com,nutpochan@hotmail.com"
objMsg.From = "nutpochan1@gmail.com"
objMsg.bodypart.Charset = "utf-8"
objMsg.Subject = Cells(1, 1).Value
objMsg.HTMLBody = Cells(2, 1).Value
objMsg.Sender = "weerfdgsdfg"
objMsg.AddAttachment "C:\Users\โฮม\Desktop\dfdfงาน1.xlsm"
Set objMsg.Configuration = msgConf
' Send
objMsg.Send
' Clear
Set objMsg = Nothing
Set msgConf = Nothing
snasui
Site Admin
Posts: 31253 Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:
#19
Post
by snasui » Sun Dec 06, 2015 5:00 pm
ตัวอย่างการปรับ Code ครับ
จาก
objMsg.AddAttachment "C:\Users\โฮม\Desktop\dfdfงาน1.xlsm" เป็น
objMsg.AddAttachment activeworkbook.Path &"\" & activeworkbook.name
nutpochan
Member
Posts: 99 Joined: Mon Mar 03, 2014 8:24 am
#20
Post
by nutpochan » Sun Dec 06, 2015 6:03 pm
แก้แล้ว debug ดังไฟล์แนบครับ
Attachments
Untitled.png (13.44 KiB) Viewed 314 times