Excel 문서에서 VBA 이메일 첨부 스크립트를 자동화하고 있습니다. 데이터 세트는 다음과 같습니다.
File Name Email Body
Sample 1 john@ Hello!
Sample 2 mary @ Hello!
내가하려는 것은 엑셀에게 "이메일"열 아래에있는 각 사람에게 이메일을 작성하고 이메일 본문의 "본문"열에 텍스트를 작성한 다음 이름이있는 파일을 찾아 첨부하도록하는 것입니다. "파일 이름"열 아래에 있습니다. 따라서 John @은 "Hello!"라는 본문이 포함 된 이메일을 받게됩니다. 및 샘플 1 첨부.
이것은 나를 당혹스럽게 만드는 각 루프에 대해 세 개의 개별적인 것이 필요합니다.
지금까지 내 코드는 다음과 같지만 첨부 파일을 찾는 것뿐입니다.
Sub Attachment()
Dim colb As Range, mycell As Range, mycell2 As Range, mycell3 As Range
Set colb = Range(Range("B2"), Range("B2").End(xlDown))
Set colc = Range(Range("C2"), Range("C2").End(xlDown))
Set cold = Range(Range("D2"), Range("C2").End(xlDown))
For Each mycell In colb
Dim path As String
path = mycell.Value
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
Set myAttachments = OutMail.Attachments
On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = "Test"
.Body = ""
.Display
End With
On Error GoTo 0
myAttachments.Add "C:\R\" & path
Set OutMail = Nothing
Set OutApp = Nothing
Next
End Sub
나는 3 개의 루프가 필요하지 않기 때문에 당신이 말하는 것을 100 % 확신하지 못합니다. 이것으로 코드를 업데이트 할 수 없습니까?
With OutMail
.To = mycell.Offset(0, 1).Text
.CC = ""
.BCC = ""
.Subject = "Test"
.Body = mycell.Offset(0, 2).Text
.Display
End With
이것은 수신자와 본문을 얻기 위해 mycell에서 참조하고 오프셋합니다.
이 경우 전체 루틴을 다음과 같이 줄일 수 있습니다.
Sub Attachment()
Dim colb As Range, mycell As Range
Set colb = Range(Range("B2"), Range("B2").End(xlDown))
For Each mycell In colb
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
Set myAttachments = OutMail.Attachments
On Error Resume Next
With OutMail
.To = mycell.Offset(0, 1).Text
.Subject = "Test"
.Body = mycell.Offset(0, 2).Text
.Display
End With
myAttachments.Add "C:\R\" & mycell.Text
Set OutMail = Nothing
Set OutApp = Nothing
Next
End Sub
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다