가장 좋은 제목을 지정하는 방법이 확실하지 않지만 각 행을 반복하고 각 행마다 전자 메일을 만드는 시트가 있습니다. 첨부 파일은 Division 이름을 기반으로합니다. 현재, 모든 행에 대해 전자 메일을 생성하므로 Name 아래에있는 한 사람이 8 개의 부서를 갖고 있으면 각각 다른 첨부 파일을 가진 8 개의 전자 메일을 받게됩니다. 이것은 짜증나는 사람들입니다. 그래서 지금 루프가 되길 원합니다 (아마도 중첩 된 것입니까?). 같은 이름을 찾으면, 그 이름에 대한 하나의 이메일을 만들고 모든 부서 보고서를 첨부하십시오.각 행에 일치하는받는 사람을 기준으로 첨부 파일이있는 행별로 Excel에서 전자 메일 만들기
더 쉽게하려면 모든 중복 이름이 모두 그룹화되도록 목록을 설정했습니다. 이 예제에서는 Widgets 및 Doorknobs 첨부 파일과 함께 Name Sample Sample1에 하나의 전자 메일을 생성하려고합니다. 나머지는 보통 이메일을 하나씩받습니다. 이 작업을 수행하는 데 몇 시간 씩 노력했지만이 작업을 수행하는 데 필요한 VBA 지식이 충분하지 않습니다. 공식적으로 Excel 자체에서 할 수 있습니다. 기본적으로 A2 = A3이면 다음과 같이 말합니다. 그러나 VBA에서이 문제가 발생하려면 도움이 필요합니다. 이미지를 참조하십시오.
업데이트 : Vityata로 표시된 인수 분해 방법을 사용하여 작성한 아래 코드를 업데이트했습니다. 그것은 실행되지만 각 이메일의 속임수를 만듭니다.
Option Explicit
Public Sub TestMe()
Dim name As String
Dim division As String
Dim mail As String
Dim dict As Object
Dim dictKey As Variant
Dim rngCell As Range
Set dict = CreateObject("Scripting.Dictionary")
For Each rngCell In Range("b2:b4")
If Not dict.Exists(rngCell.Value) Then
dict.Add rngCell.Value, rngCell.Offset(0, -1)
End If
Next rngCell
For Each dictKey In dict.keys
SendMail dictKey, dict(dictKey)
Next dictKey
End Sub
Public Sub SendMail(ByVal address As String, ByVal person As String)
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim strdir As String
Dim strFilename As String
Dim sigString As String
Dim strBody As String
Dim strName As String
Dim strName1 As String
Dim strDept As String
Dim strName2 As String
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
sigString = Environ("appdata") & _
"\Microsoft\Signatures\Test.htm"
If Dir(sigString) <> "" Then
signature = GetBoiler(sigString)
Else
signature = ""
End If
strdir = "z:\"
strBody = "<Font Face=calibri>Please review the attached report for your department."
For Each address In Columns("B").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "C").Value) = "yes" Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
strName = Cells(cell.Row, "a").Value
strName1 = Cells(cell.Row, "d").Value
strName2 = Left(strName, InStr(strName & " ", " ") - 1)
strFilename = Dir("z:\*" & strName1 & "*")
.To = cell.Value
.Subject = "Monthly Budget Deficit Report for " & strName1
.HTMLBody = "<Font Face=calibri>" & "Dear " & address & ",<br><br>"
.Attachments.Add strdir & strFilename
.Display 'Or use Send
End With
Set OutMail = Nothing
End If
Next cell
End Sub
Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.ReadAll
ts.Close
End Function
엑셀 테이블을 [ADO 데이터 소스] (https://stackoverflow.com/questions/2484516/vba-create-adodb-recordset-from-the-contents-of- 스프레드 시트)를 작성한 다음 각각의 사람과 전자 메일 주소를 선택하고 해당 항목과 일치하는 모든 부서를 선택하고 적절한 첨부 파일을 추가하는 루프를 작성하십시오. 엑셀에 머무르는 것이 요구됩니까? –