使用Access VBA创建动态Outlook电子邮件

时间:2015-09-15 19:43:23

标签: vba email access-vba outlook-vba

这是我目前的设置......

我有一个Access项目跟踪数据库,其中包含项目描述值(例如:Project_ID),我有一个“创建电子邮件”按钮,我已成功打开Outlook,选择Outlook草稿,并自动生成相应的主题行

但是,我不知道从下一步开始的位置。任何帮助表示赞赏!

我希望Outlook电子邮件包含链接到Access数据库中字段的动态字段。我甚至不确定我是否应该尝试在Access或Outlook中进行此步骤的编码!

我想要的例子:

  

NAME,

     

附上您的文件:

     

Project_ID Project_Name Project_Type< - 来自Access的值   数据库

     

此致

     

我的签名

1 个答案:

答案 0 :(得分:1)

你应该在MS Access中编码。 伪代码将是:

  1. 读取访问表中的所有值
  2. 创建Outlook对象,邮件对象
  3. 打开Outlook邮件并添加主题,内容,附件
  4. 发送或留给员工发送
  5. 称之为

    send_email_message "to@to.to","","","Email subject","Add your email content/body like project id= &field1, project name = field2 and so on"
    

    您可以附加多个文件,只需将文档名称加入“;”即可分隔符。像

    “C; \ FILE1.TXT; C:\ FILE2.TXT”

    这里是一个发送邮件功能: 此函数使用outlook对象(在引用中导入它们) 或者通过vba.createobject(“outlook.application”)将其更改为后期绑定

    Function SEND_EMAIL_MESSAGE(mTo As String, mCC As String, mBC As String, mSubject As String, mBody As String, Optional useOwnSignature As Boolean = False, Optional DisplayMsg As Boolean = False, Optional isHTML As Boolean = False, Optional AttachmentPath = "") As Boolean
    '---------------------------------------------------------------------------------------
    ' Procedure : SEND_EMAIL_MESSAGE
    ' Author    : KRISH KM
    ' Date      : 01/09/2013
    ' Purpose   : Send emails using outlook
    '---------------------------------------------------------------------------------------
    '
    ' Please check the reference for Microsoft Outlook 14.0 object library for outlook 2010.
    
        Dim oAPP As Outlook.Application
        Dim oMail As Outlook.MailItem
        Dim oAPPAttach As Outlook.Attachment
        Dim mSignature As String
    
        On Error GoTo ERROR_EMAIL
        ' Create the Outlook session.
        Set oAPP = New Outlook.Application
    
        ' Create the message.
        Set oMail = oAPP.CreateItem(olMailItem)
    
        With oMail
            ' Add the To recipient(s) to the message.
            .to = mTo
            .cc = mCC
            .BCC = mBC
            .Subject = mSubject
    
            If useOwnSignature Then .BodyFormat = olFormatHTML
            .Display
    
            If useOwnSignature Then
                If isHTML Then
    
                    mSignature = .HTMLBody
                    .HTMLBody = mBody & mSignature
                Else
                    mSignature = .body
                    .body = mBody & mSignature
                End If
            Else
                .body = mBody
            End If
    
            ' Add attachments to the message.
            If Not IsMissing(AttachmentPath) Then
                Dim mFiles() As String
                If (VBA.Right(AttachmentPath, 1)) <> ";" Then AttachmentPath = AttachmentPath & ";"
                mFiles = VBA.Split(AttachmentPath, ";")
                Dim i As Integer
                For i = 0 To UBound(mFiles) - 1
                    If Not mFiles(i) = "" Then .Attachments.Add (mFiles(i)) 'Set oAPPAttach = .Attachments.Add(mFiles(i))
                Next i
    
            End If
    
            ' Should we display the message before sending?
            If DisplayMsg Then
                .Display
            Else
                .Send
            End If
        End With
    
        SEND_EMAIL_MESSAGE = True
    EXIT_ROUTINE:
        On Error GoTo 0
        Set oAPP = Nothing
        Set oMail = Nothing
        Exit Function
    
    ERROR_EMAIL:
        SEND_EMAIL_MESSAGE = False
        GoTo EXIT_ROUTINE
    End Function