2016-08-04 3 views
1

사람들 목록에 전자 메일을 보내는 데 도움을 찾고 있습니다. 내 코드에는 간단한 루프가 있으며 이메일을 보낼 때마다 값을 매번 가져옵니다. 테스트하는 동안 첫 번째 이메일이 항상 전송됩니다. 그 후, I를 통해 2 시간 항목이 이동 또는 삭제 된 ".TO"전자 메일 여러 수신자 VBA 오류

Run-time error - '-2147221238 (8004010a): 에 오류가 발생합니다.

코드가 다음 전자 메일 값을 정확하게 포착하기 때문에 나에게 당황 스럽습니다.

수신자를 숨은 참조 목록에 추가하는 대신 전자 메일을 하나씩 보내야합니다. VBA로 가능합니까? 미리 감사드립니다!

Sub TestingAgain() 

'Setting up the Excel variables. 
Dim outApp As Object 
Dim outMailItem As Object 
Dim iCounter As Integer 
Dim sDest As String 
Dim sName As String 

'Create the Outlook application and the empty email. 
Set outApp = CreateObject("Outlook.Application") 
Set outMailItem = outApp.CreateItem(0) 

With outMailItem 
    sDest = "" 
For i = 2 To WorksheetFunction.CountA(Columns(1)) 
    If i <> "" Then 
     'Grab first name and email 
     sDest = Cells(i, 5).Value 
     sName = Cells(i, 1).Value 

     'Send each email 
     .To = sDest 
     .Subject = "FYI" 
     .htmlbody = "Some stuff" 
     .Send 
    Else 
    MsgBox ("Error") 

    End If 
Next i 

End With 

'Clean up the Outlook application. 
Set outMailItem = Nothing 
Set outApp = Nothing 

End Sub 

답변

1

전자 메일을 보내면 mailItem 인스턴스가 완료되어 더 이상 사용할 수 없습니다. 다음과 같이 코드를 리팩터링하십시오.

Sub TestingAgain() 

    'Setting up the Excel variables. 
    Dim outApp As Object 
    Dim outMailItem As Object 
    Dim iCounter As Integer 
    Dim sDest As String 
    Dim sName As String 

    'Create the Outlook application and the empty email. 
    Set outApp = CreateObject("Outlook.Application") 



     sDest = "" 
    For i = 2 To WorksheetFunction.CountA(Columns(1)) 
     If i <> "" Then 
     '/ Create the mail item instance. 
     Set outMailItem = outApp.CreateItem(0) 
     With outMailItem 
       'Grab first name and email 
       sDest = Cells(i, 5).Value 
       sName = Cells(i, 1).Value 

       'Send each email 
       .To = sDest 
       .Subject = "FYI" 
       .htmlbody = "Some stuff" 
       .send 
       '/ Once sent, mail item is no more available. 
      End With 
      Else 
      MsgBox ("Error") 

      End If 

    Next 


    'Clean up the Outlook application. 
    Set outMailItem = Nothing 
    Set outApp = Nothing 
End Sub 
+0

완벽합니다. 고맙습니다. 따라서이 코드 줄에서 : outMailItem = outApp.CreateItem (0) 을 설정하십시오. CreateItem의 "0"은 일을 시작하기위한 출발점 일뿐입니다? – Clint

+1

yw. 아니요 '0'은 itemtype 매개 변수 값입니다. 자세한 내용을 보려면 다음을 참조하십시오. https://msdn.microsoft.com/en-us/library/office/ff869291.aspx – cyboashu

관련 문제