Results 1 to 2 of 2

Thread: Send Multiple Outlook Mails Using The Same Template

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Send Multiple Outlook Mails Using The Same Template

    Hello, I am having trouble getting the below code to work. When I compile the code it says: User-defined type not defined. In this code, I am sending an email through excel. I am also using an outlook template file. Also, I have created a loop at the end. Any assistance is greatly appreciated. Thank you,



    Code:
    Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
    
    
      Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
        "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
    Sub TicketResolved()
    Dim myOlApp As Outlook.Application
    Dim MyItem As Outlook.MailItem
    On Error GoTo ErrorHandler1
    
    Set myOlApp = CreateObject("Outlook.Application")
    Set MyItem = myOlApp.CreateItemFromTemplate("C:\OPEN -ALL\TICKET RESOLVED\Ticket Resolved Template")
        With MyItem
            .To = Cells(3, 2) & "; forums1167@gmail.com"
            .Subject = "Ticket Resolved #" & Cells(3, 1)
                 
      .HTMLBody = Replace(.HTMLBody, "Issue1", Cells(3, 1))
      .HTMLBody = Replace(.HTMLBody, Lastfirstname1, Cells(3, 2))
           .Display
           
        End With
    
    Set MyItem = Nothing
    Set myOlApp = Nothing
    
    
    Rows("3:3").Select
        Selection.Delete Shift:=xlUp
        
        While Not IsEmpty(Cells(3, 3))
             
      Call TicketResolved
    
    Wend
    
    End Sub

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Don't think there's any reason for having those API calls in your code, unless you know it is there for a certain reason.

    So here's what I think you are after.

    Code:
    Sub TicketResolved()
    
        Dim myOlApp As Object 'Outlook.Application
        Dim MyItem As Object 'Outlook.MailItem
        Dim Lastfirstname1 As String
        
        Lastfirstname1 = "SOMETHING"
        Set myOlApp = CreateObject("Outlook.Application")
        While Not IsEmpty(Cells(3, 3))
            Set MyItem = myOlApp.CreateItemFromTemplate("C:\OPEN -ALL\TICKET RESOLVED\Ticket Resolved Template")
            With MyItem
                .To = Cells(3, 2) & "; forums1167@gmail.com"
                .Subject = "Ticket Resolved #" & Cells(3, 1)
                .HTMLBody = Replace(.HTMLBody, "Issue1", Cells(3, 1))
                .HTMLBody = Replace(.HTMLBody, Lastfirstname1, Cells(3, 2))
                .Display
            End With
            Set MyItem = Nothing
            Set myOlApp = Nothing
            Rows("3:3").Delete Shift:=xlUp
        Wend
    
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 12
    Last Post: 12-10-2019, 09:56 PM
  2. Replies: 26
    Last Post: 10-22-2019, 02:39 PM
  3. Replies: 6
    Last Post: 06-05-2013, 11:33 PM
  4. Outlook Send Mail With Multiple Recipient and CC
    By noobtron in forum Excel Help
    Replies: 2
    Last Post: 10-31-2012, 07:14 PM
  5. Replies: 7
    Last Post: 05-09-2012, 11:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •