主题未被 VBA 宏替换
好吧,我正在尝试编辑这段代码,我发现它允许我将变量信息输入到电子邮件的弹出框中。这很棒并且工作完美(虽然有点慢),但是我在尝试对主题行做同样的事情时遇到了一个奇怪的问题。
使用我设置的一个模板,工作模板我得到了我正在寻找的东西,它会遍历所有 4 个变量,包括主题行中的变量。
但是,如果我使用具有相同变量的不同模板,不工作模板,它不会t 替换主题行。损坏的模板中的另外两个变量弹出对话框 - 示例
有人可以帮我填写吗为什么它适用于一个模板但不适用于另一个模板?
完整代码:
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 = "FMAudit Legacy Install [custbusiness]" Or mail.Subject = "FMAudit Install [custbusiness]" Then
'Check message format
If mail.BodyFormat = OlBodyFormat.olFormatPlain Then
'Replace [date] with the entered value
If InStr(mail.Body, "[custname]") > 0 Then
Value = InputBox("Enter the customer name")
If Value <> "" Then
mail.Body = Replace(mail.Body, "[custname]", Value)
End If
End If
'Replace [percent] with the entered value
If InStr(mail.Body, "[custbusiness]") > 0 Then
Value = InputBox("Enter business name")
If Value <> "" Then
mail.Body = Replace(mail.Body, "[custbusiness]", Value)
End If
End If
'Replace [percent] with the entered value
If InStr(mail.Body, "[custhost]") > 0 Then
Value = InputBox("Enter host name")
If Value <> "" Then
mail.Body = Replace(mail.Body, "[custhost]", Value)
End If
End If
Else
'Replace [date] with the entered value
If InStr(mail.HTMLBody, "[custname]") > 0 Then
Value = InputBox("Enter the customer name")
If Value <> "" Then
mail.HTMLBody = Replace(mail.HTMLBody, "[custname]", Value)
End If
End If
'Replace [percent]; with the entered value
If InStr(mail.HTMLBody, "[custbusiness]") > 0 Then
Value = InputBox("Enter business name")
If Value <> "" Then
mail.HTMLBody = Replace(mail.HTMLBody, "[custbusiness]", Value)
End If
End If
'Replace [percent]; with the entered value
If InStr(mail.HTMLBody, "[custhost]") > 0 Then
Value = InputBox("Enter host name")
If Value <> "" Then
mail.HTMLBody = Replace(mail.HTMLBody, "[custhost]", Value)
'Replace [percent] with the entered value
If InStr(mail.Subject, "[custbusiness]") > 0 Then
Value = InputBox("Enter business name subject")
If Value <> "" Then
mail.Subject = Replace(mail.Subject, "[custbusiness]", Value)
End If
End If
End If
End If
End If
End If
Set mail = Nothing
End If
End Sub
Okay so, I'm trying to edit this code I found that allows me to input variable information into a pop up box for emails. This is great and works flawlessly (although kinda slow), however I'm running into a weird issue trying to do the same thing with the subject line as well.
Using one template that I have set up, working template I get exactly what I'm looking for, it goes through all 4 of the variables INCLUDING the one on the subject line.
However, if I use a different template with the same variables, Not working template, it doesn't replace the subject line. The other two variables in the broken template pop up the dialog box - example
Can someone help fill me in on why it works on one template but not the other?
Full code:
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 = "FMAudit Legacy Install [custbusiness]" Or mail.Subject = "FMAudit Install [custbusiness]" Then
'Check message format
If mail.BodyFormat = OlBodyFormat.olFormatPlain Then
'Replace [date] with the entered value
If InStr(mail.Body, "[custname]") > 0 Then
Value = InputBox("Enter the customer name")
If Value <> "" Then
mail.Body = Replace(mail.Body, "[custname]", Value)
End If
End If
'Replace [percent] with the entered value
If InStr(mail.Body, "[custbusiness]") > 0 Then
Value = InputBox("Enter business name")
If Value <> "" Then
mail.Body = Replace(mail.Body, "[custbusiness]", Value)
End If
End If
'Replace [percent] with the entered value
If InStr(mail.Body, "[custhost]") > 0 Then
Value = InputBox("Enter host name")
If Value <> "" Then
mail.Body = Replace(mail.Body, "[custhost]", Value)
End If
End If
Else
'Replace [date] with the entered value
If InStr(mail.HTMLBody, "[custname]") > 0 Then
Value = InputBox("Enter the customer name")
If Value <> "" Then
mail.HTMLBody = Replace(mail.HTMLBody, "[custname]", Value)
End If
End If
'Replace [percent]; with the entered value
If InStr(mail.HTMLBody, "[custbusiness]") > 0 Then
Value = InputBox("Enter business name")
If Value <> "" Then
mail.HTMLBody = Replace(mail.HTMLBody, "[custbusiness]", Value)
End If
End If
'Replace [percent]; with the entered value
If InStr(mail.HTMLBody, "[custhost]") > 0 Then
Value = InputBox("Enter host name")
If Value <> "" Then
mail.HTMLBody = Replace(mail.HTMLBody, "[custhost]", Value)
'Replace [percent] with the entered value
If InStr(mail.Subject, "[custbusiness]") > 0 Then
Value = InputBox("Enter business name subject")
If Value <> "" Then
mail.Subject = Replace(mail.Subject, "[custbusiness]", Value)
End If
End If
End If
End If
End If
End If
Set mail = Nothing
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过创建一个仅执行替换的单独方法,可以大大简化您的代码:
Your code could be simplified a lot by creating a separate method which just does the replacements: