VBA Outlook | วิธีส่งอีเมลจาก Outlook โดยใช้รหัส VBA

เราได้เห็น VBA ใน excel และวิธีที่เราทำให้งานของเราเป็นแบบอัตโนมัติใน excel ด้วยการสร้างมาโครใน Microsoft Outlook เรายังมีข้อมูลอ้างอิงสำหรับ VBA และการใช้งานที่เราสามารถควบคุม Outlook โดยใช้ VBA ทำให้งานซ้ำ ๆ ของเราใน Outlook ง่ายขึ้นโดยอัตโนมัติและ คล้ายกับ excel เราจำเป็นต้องเปิดใช้งานคุณสมบัตินักพัฒนาเพื่อใช้ VBA ใน Outlook

VBA Outlook

ความสวยงามของ VBA คือเราสามารถอ้างอิงวัตถุอื่น ๆ ของ Microsoft เช่น PowerPoint, Word และ Outlook เราสามารถสร้างงานนำเสนอที่สวยงามเราสามารถทำงานกับเอกสาร Microsoft word และสุดท้ายเราสามารถส่งอีเมลได้เช่นกัน ใช่คุณได้ยินถูกต้องเราสามารถส่งอีเมลจาก excel ได้ ฟังดูน่าอึดอัด แต่ในขณะเดียวกันก็ทำให้รอยยิ้มบนใบหน้าของเราด้วยเช่นกัน ในบทความนี้ฉันจะแสดงวิธีการทำงานกับวัตถุ Microsoft Outlook จาก excel โดยใช้การเข้ารหัส VBA อ่านต่อ…

เราอ้างอิง Outlook จาก Excel ได้อย่างไร

โปรดจำไว้ว่า Outlook เป็นวัตถุและเราจำเป็นต้องตั้งค่าการอ้างอิงนี้ในไลบรารีการอ้างอิงวัตถุ ในการตั้งค่าวัตถุ Outlook เพื่ออ้างอิงให้ทำตามขั้นตอนด้านล่าง

ขั้นตอนที่ 1:ไปที่ Visual Basic Editor

ขั้นตอนที่ 2:ไปที่เครื่องมือ> ข้อมูลอ้างอิง

ขั้นตอนที่ 3:ในไลบรารีออบเจ็กต์การอ้างอิงด้านล่างให้เลื่อนลงแล้วเลือก“ MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY”

เลือกช่อง“ MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY”เพื่อให้ใช้งานได้กับ Excel VBA

ตอนนี้เราสามารถเข้าถึงวัตถุ VBA Outlook จาก excel ได้แล้ว

เขียนรหัสเพื่อส่งอีเมลจาก VBA Outlook จาก Excel

เราสามารถส่งอีเมลจาก excel ผ่านแอพ outlook สำหรับสิ่งนี้เราต้องเขียนรหัส VBA ทำตามขั้นตอนด้านล่างเพื่อส่งอีเมลจาก Outlook

คุณสามารถดาวน์โหลด VBA Outlook เป็นเทมเพลต Excel ได้ที่นี่ - VBA Outlook to Excel Template

ขั้นตอนที่ 1:สร้างโพรซีเดอร์ย่อย

รหัส:

 ตัวเลือก Explicit Sub Send_Exails () End Sub 

ขั้นตอนที่ 2:กำหนดตัวแปรเป็น VBA Outlook แอปพลิเคชัน

รหัส:

 Option Explicit Sub Send_Exails () Dim OutlookApp As Outlook.Application End Sub 

ขั้นตอนที่ 3:การอ้างอิงตัวแปรข้างต้นไปยังแอปพลิเคชัน VBA Outlook ใน Outlook เราจำเป็นต้องส่งอีเมลดังนั้นกำหนดตัวแปรอื่นเป็น Outlook.MailItem

รหัส:

 Option Explicit Sub Send_Exails () Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem End Sub 

ขั้นตอนที่ 4:ตอนนี้ทั้งสองตัวแปรเป็นตัวแปรออบเจ็กต์ เราจำเป็นต้องตั้งค่า ชุดแรกตัวแปร“OutlookApp” เป็นใหม่ Outlook.Application

รหัส:

 Sub Send_Exails () Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application End Sub 

ขั้นตอนที่ 5:ตั้งค่าตัวแปรที่สอง“ OutlookMail” ดังต่อไปนี้

ตั้งค่า OutlookMail = OutlookApp.CreateItem (olMailItem)

รหัส:

 Sub Send_Exails () Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem (olMailItem) End Sub 

ขั้นตอนที่ 6:ตอนนี้ใช้ With statement access VBA Outlook Mail

รหัส:

 Sub Send_Exails () Dim OutlookApp เป็น Outlook.Application Dim OutlookMail เป็น Outlook.MailItem ตั้งค่า OutlookApp = Outlook ใหม่ชุดโปรแกรมประยุกต์ OutlookMail = OutlookApp.CreateItem (olMailItem) ด้วย OutlookMail จบด้วย End Sub 

ตอนนี้เราสามารถเข้าถึงรายการทั้งหมดที่มีอยู่ในรายการอีเมลเช่น“ เนื้อหาของอีเมล”“ ถึง”“ CC”“ BCC”“ เรื่อง” และอื่น ๆ อีกมากมาย

ขั้นตอนที่ 7:ตอนนี้ภายในด้วยกับคำสั่งที่เราสามารถดูรายชื่อ IntelliSense โดยวางจุด

ขั้นตอนที่ 8:ขั้นแรกให้เลือกรูปแบบร่างกายเป็นolFormatHtml

รหัส:

 ด้วย OutlookMail .BodyFormat = olFormatHTML ปิดท้ายด้วย 

ขั้นตอนที่ 9:ตอนนี้แสดงอีเมล

รหัส:

 ด้วย OutlookMail .BodyFormat = olFormatHTML แสดงจบด้วย 

ขั้นตอนที่ 10:ตอนนี้เราต้องเขียนอีเมลในเนื้อหาของอีเมล สำหรับการเลือกนี้HtmlBody

รหัส:

 ด้วย OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "เขียนอีเมลของคุณที่นี่" ปิดท้ายด้วย 

ด้านล่างนี้คือตัวอย่างเนื้อหาของการเขียนอีเมล

ขั้นตอนที่ 11:หลังจากเขียนอีเมลแล้วเราจำเป็นต้องระบุรหัสอีเมลของผู้รับ สำหรับการเข้าถึง " ถึง " นี้

ขั้นตอนที่ 12:ถัดไปพูดถึงผู้ที่คุณต้องการCCอีเมล

Step 13: Now mention the BCC email id’s,

Step 14: Next thing is we need to mention the subject for the email we are sending.

Step 15: Now add attachments. If you want to send the current workbook as an attachment then use the attachment as This workbook

Step 16: Finally send the email by using the Send method.

Now, this code will send the email from your VBA outlook mail. Use the below VBA code to send emails from your outlook.

To use the below code you must set the object reference to “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY” under object library of Excel VBA

By setting the reference to the object library is called early binding. The reason why we need to set the reference to object library because without setting the object library as “MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY” We cannot access the IntelliSense list of VBA properties and methods. This makes the writing of code difficult because you need to be sure of what you are writing in terms of technique and spellings.

 Sub Send_Emails() 'This code is early binding i.e in Tools > Reference >You have check "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" Dim OutlookApp As Outlook.Application Dim OutlookMail As Outlook.MailItem Set OutlookApp = New Outlook.Application Set OutlookMail = OutlookApp.CreateItem(olMailItem) With OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Dear ABC" & "

" & "

" & "Please find the attached file" & .HTMLBody 'last .HTMLBody includes signature from the outlook. ''

includes line breaks b/w two lines .To = "[email protected]" .CC = "[email protected]" .BCC = "[email protected];[email protected]" .Subject = "Test mail" .Attachments = ThisWorkbook .Send End With End Sub