【发布时间】: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 时运行的任何建议?