Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook Automation using Template

Status
Not open for further replies.

weaselthend

IS-IT--Management
Oct 30, 2001
13
US
Hello All,

I use automation to send email messages from Access using Outlook 2000. I would like these emails to be based on an Outlook template, .oft file, which is already created. Basically, I just need to define the recipients of the template then send it. Does anyone know how to program this?

Thanks in advance.

weaselthend
 
Something like this:

Code:
Public Function SendEmailFromTemplate(ByVal strFile As String, ByVal strRecipients As String) As Boolean
On Error GoTo ErrHandler
  Dim ol As Outlook.Application
  Dim msg As Outlook.MailItem
  Dim fldOut As Outlook.MAPIFolder
  Dim cbrMenu As Office.CommandBar
  Dim cbrTools As Office.CommandBarPopup
  Dim cbrSend As Office.CommandBarControl
  Dim lngCount As Long
  Dim blnOpen As Boolean
  
  If Dir(strFile) <> &quot;&quot; Then
    
    On Error Resume Next
    Set ol = GetObject(, &quot;Outlook.Application&quot;)
    
    If Err = 0 Then
      blnOpen = True
    Else
      Set ol = CreateObject(&quot;Outlook.Application&quot;)
    End If
    
    On Error GoTo ErrHandler
    
    Set msg = ol.CreateItemFromTemplate(strFile)
    
    With msg
      .Subject = &quot;Email auto-generated using template&quot;
      .To = strRecipients  'colon-separated list of email addresses
      .Send   'This just places mail in outbox
    End With
    
    'open an explorer
    Call ol.Session.GetDefaultFolder(olFolderOutbox).GetExplorer.ShowPane(olFolderList, False)
    Set fldOut = ol.Session.GetDefaultFolder(olFolderOutbox)
    lngCount = fldOut.Items.Count

    'capture the send action menu
    Set cbrMenu = ol.ActiveExplorer.CommandBars(&quot;Menu Bar&quot;)
    Set cbrTools = cbrMenu.Controls(&quot;Tools&quot;)
    Set cbrSend = cbrTools.Controls(&quot;Send&quot;)
    
    'Send now
    cbrSend.Execute
    
    If Not blnOpen Then
      Do While fldOut.Items.Count >= lngCount
        DoEvents
      Loop
    End If
    
    SendEmailFromTemplate = True
    
  End If

ExitHere:
  On Error Resume Next
  Set msg = Nothing
  Set cbrSend = Nothing
  Set cbrTools = Nothing
  Set cbrMenu = Nothing
  If Not blnOpen Then
    ol.Quit
  End If
  Set fldOut = Nothing
  Set ol = Nothing
  Exit Function
ErrHandler:
  MsgBox &quot;Error (&quot; & Err & &quot;) - &quot; & Err.Description
  Resume ExitHere

End Function


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Thanks much VBslammer.

All I needed was the ol.CreateItemFromTemplate(strFile)
and it worked fine using my existing email generator.

Thanks again,

weaselthend
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top