【问题标题】:How to run code to automate email template?如何运行代码来自动化电子邮件模板?
【发布时间】:2021-10-13 06:39:55
【问题描述】:

我正在尝试在 Outlook 电子邮件中填充占位符文本。

我从https://www.ablebits.com/office-addins-blog/2021/04/21/outlook-email-templates-fillable-fields-dropdown/ 的代码的简化版本开始。
当我点击“运行”时,它会提示我“宏名称”对话框。
如果我输入一个名称并点击“创建”,它会创建一个新的空白模块,与代码没有关联。

如何让代码从“ThisOutlookSession”运行?

我的目标是使用我创建的特定电子邮件模板“NewOrderNotification”运行此宏,以方便填写标准数据。

Private WithEvents m_Inspectors As Outlook.Inspectors
Private WithEvents m_Inspector As Outlook.Inspector
  
Private Sub Application_Startup()
  Set m_Inspectors = Application.Inspectors
End Sub
  
Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If TypeOf Inspector.CurrentItem Is Outlook.MailItem Then
   'Handle emails only
  Set m_Inspector = Inspector
End If
End Sub
  
Private Sub m_Inspector_Activate()
  
Dim Item As MailItem
Dim Value As String
  
If TypeOf m_Inspector.CurrentItem Is MailItem Then
Set mail = m_Inspector.CurrentItem
  
'Identify the message subject
If mail.Subject = "New SO XXXXX for Quote XXXXXX / [CUSTOMER] - [CITY, STATE]" Then
     
'Check message format
If mail.BodyFormat = OlBodyFormat.olFormatPlain Then

'--------
'Replace [_SO] with the entered value
If InStr(mail.Body, "[_SO]") > 0 Then
    Value = InputBox("Enter the new sales order #")
         
If Value <> "" Then
    mail.Body = Replace(mail.Body, "[_SO]", Value)
End If
End If
'---------
         
'Repeat the code block above for the other fields in the email

Set mail = Nothing
  
End If
End Sub

【问题讨论】:

  • 这个宏似乎设计为自动运行,而不是手动运行。您发布的代码缺少全局 m_Inspectors 变量 - 您是否忽略了它?
  • 好的。谢谢蒂姆。事实上,我注意到,在我关闭 Outlook 并重新打开它之后,它似乎想在简单地打开 Outlook 时运行我的代码,这不是我想要做的。要回答您的问题,是的,我无意中从论坛帖子中遗漏了 m_Inspectors 变量,但它在我的代码中。我有一个名为“NewOrderNotification”的电子邮件模板。我希望此代码仅手动运行,特定于该模板 - 通过功能区中的按钮单击。关于如何为此目的编写代码而不是尝试在一般启动 Outlook 时运行的任何建议?

标签: vba email outlook


【解决方案1】:

这应该很接近:

Sub ReplaceMailText()
    
    Dim mail As MailItem, oInspector As Inspector, txt
    Set oInspector = Application.ActiveInspector
    
    If oInspector Is Nothing Then
        MsgBox "No active inspector"
    Else
        Set mail = oInspector.CurrentItem
        If Not mail.Sent Then
        
            'Check message format
            If mail.BodyFormat = OlBodyFormat.olFormatPlain Then
                If InStr(mail.Body, "[_SO]") > 0 Then
                    txt = InputBox("Enter the new sales order #")
                         
                    If txt <> "" Then
                        mail.Body = Replace(mail.Body, "[_SO]", txt)
                    End If
                End If
            End If 'plain text
        
        End If     'not Sent
    End If         'have mail
End Sub

【讨论】:

  • 谢谢。此代码有效,但前提是我的电子邮件是纯文本格式。有什么方法可以让它使用 HTML 格式,以便我可以保留我想要的字体等?
  • 没关系。看起来就像更改为“olFormatHTML”并将“mail.Body”更改为“mail.HTMLBody”一样简单谢谢您的帮助!
【解决方案2】:

首先,不需要检查邮件正文格式。 Body 无论如何都会返回纯文本正文表示:

  
Private Sub RunForInspector()
  Dim m_Inspector As Outlook.Inspector
  Dim Item As MailItem
  Dim Value As String
  
  Set m_Inspector = Application.ActiveInspector
  If TypeOf m_Inspector.CurrentItem Is MailItem Then
    Set mail = m_Inspector.CurrentItem
  
    'Identify the message subject
    If mail.Subject = "New SO XXXXX for Quote XXXXXX / [CUSTOMER] - [CITY, STATE]" Then
     
    '--------
    'Replace [_SO] with the entered value
    If InStr(mail.Body, "[_SO]") > 0 Then
      Value = InputBox("Enter the new sales order #")
         
    If Value <> "" Then
      mail.Body = Replace(mail.Body, "[_SO]", Value)
    End If
    '---------
         
    'Repeat the code block above for the other fields in the email

    Set mail = Nothing
  End If
End Sub

此外,如果您处理基于 HTML 的消息正文并希望保留标记,则需要处理 HTMLBody 属性。 Body 代表纯消息正文。

【讨论】:

  • 感谢您提供额外的视角!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
  • 2016-09-10
相关资源
最近更新 更多