VBA 전망 | VBA 코드를 사용하여 Outlook에서 이메일을 보내는 방법은 무엇입니까?

우리는 Excel에서 VBA를 보았고 매크로를 생성하여 Excel에서 작업을 자동화하는 방법을 보았습니다. Microsoft Outlook에는 VBA에 대한 참조가 있으며 VBA를 사용하여 Outlook을 제어 할 수 있습니다. 따라서 Outlook에서 반복되는 작업을 더 쉽게 자동화 할 수 있습니다. Excel과 유사하게 Outlook에서 VBA를 사용하려면 개발자 기능을 활성화해야합니다.

VBA 전망

VBA의 장점은 PowerPoint, Word 및 Outlook과 같은 다른 Microsoft 개체를 참조 할 수 있다는 것입니다. 멋진 프레젠테이션을 만들고 Microsoft Word 문서로 작업 할 수 있으며 마지막으로 이메일도 보낼 수 있습니다. 예, 잘 들었습니다. 엑셀 자체에서 이메일을 보낼 수 있습니다. 이것은 어색하게 들리지만 동시에 우리 얼굴에 미소를 짓습니다. 이 기사에서는 VBA 코딩을 사용하여 Excel에서 Microsoft Outlook 개체로 작업하는 방법을 보여줍니다. 읽어…

Excel에서 Outlook을 어떻게 참조합니까?

Outlook은 개체이며 개체 참조 라이브러리에서 이에 대한 참조를 설정해야합니다. 참조 할 Outlook 개체를 설정하려면 아래 단계를 따르십시오.

1 단계 : Visual Basic Editor로 이동합니다.

2 단계 : 도구> 참조로 이동합니다.

3 단계 : 아래 참조 개체 라이브러리에서 아래로 스크롤하여 "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY"를 선택합니다.

Excel VBA에서 사용할 수 있도록 "MICROSOFT OUTLOOK 14.0 OBJECT LIBRARY" 확인란을 선택 합니다.

이제 Excel에서 VBA Outlook 개체에 액세스 할 수 있습니다.

Excel에서 VBA Outlook에서 이메일을 보내는 코드 작성

Outlook 앱을 통해 Excel에서 이메일을 보낼 수 있습니다. 이를 위해 VBA 코드를 작성해야합니다. Outlook에서 이메일을 보내려면 아래 단계를 따르세요.

이 VBA Outlook to Excel 템플릿을 여기에서 다운로드 할 수 있습니다 – VBA Outlook to Excel 템플릿

1 단계 : 하위 프로 시저를 만듭니다.

암호:

 옵션 Explicit Sub Send_Exails () End Sub 

2 단계 : 변수를 VBA Outlook.Application 으로 정의합니다 .

암호:

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

3 단계 : VBA Outlook 애플리케이션에 대한 위의 변수 참조. Outlook에서는 이메일을 보내야하므로 다른 변수를 Outlook.MailItem으로 정의합니다.

암호:

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

4 단계 : 이제 두 변수 모두 개체 변수입니다. 설정해야합니다. 먼저 "OutlookApp"변수를 New 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 문을 사용하여 VBA Outlook 메일에 액세스 합니다.

암호:

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

이제 우리는 "이메일 본문", "받는 사람", "참조", "숨은 참조", "제목"등과 같은 전자 메일 항목에서 사용 가능한 모든 항목에 액세스 할 수 있습니다.

7 단계 : 이제 with 문 안에 을 넣어 IntelliSense 목록을 볼 수 있습니다 .

Step 8: First select the body format as olFormatHtml.

Code:

 With OutlookMail .BodyFormat = olFormatHTML End With 

Step 9: Now display the email.

Code:

 With OutlookMail .BodyFormat = olFormatHTML .Display End With 

Step 10: Now we need to write the email in the body of the email. For this select HtmlBody.

Code:

 With OutlookMail .BodyFormat = olFormatHTML .Display .HTMLBody = "Write your email here" End With 

Below is the example of the body of the email writing.

Step 11: After writing the email we need to mention the email id of the receiver. For this access “To”.

Step 12: Next mention for whom you want to CC the email.

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