3

我有一组在 Outlook 2003、2007 和 2010 中有效的宏。事实上,除了特定情况外,它在 2013 年仍然有效。

每当您尝试发送电子邮件时,宏都会弹出一个对话框 - 用关键字标记主题行。问题是,如果我刚刚启动 Outlook,然后我打开了一封新电子邮件或回复 - Outlook 2013 中的默认设置是将其带入以前的“阅读窗格”而不是新窗口。如果我没有点击“弹出”并尝试发送,我的宏会因以下错误而崩溃:

“运行时错误 '91' 对象变量或未设置块变量”

我尝试先检查是否加载表单 - 但似乎对我的用户表单的任何调用,甚至是 userform.show,都会生成此错误。

奇怪的是,如果我记得“弹出”我的第一封电子邮件,它每次都运行良好,直到我关闭/重新打开 Outlook。即使我不“弹出”其他电子邮件。这只发生在第一个。

这是我的初始化事件的开始:

Dim Tags() As String
Dim T As Variant
Dim PC As Variant
Dim Rent As String
Dim Child As String
Dim nsourcefile As Integer
Dim email As MailItem

Dim PD As Variant
Dim Proj As String
Dim Desc As String

'Set email = Application.ActiveInspector.CurrentItem
Set email = Application.ActiveExplorer.Selection.Item(1)

'Checks to see if a project number (that's not on the list) may be in the subject already
If Val(email.Subject) > 10000 Then
    TagMsg.Height = tall
    TagMsg.NewProjID = Format(Val(email.Subject), "00000")
    TagMsg.NewProjDesc.SetFocus
Else
    'Set height of form (prior to pressing "More" button
    TagMsg.Height = short
End If

注意到我将 Set email = Application.ActiveInspector.CurrentItem 更改为 Set email = Application.ActiveExplorer.Selection.Item(1)。这似乎已修复它,但 VBA 帮助声明“不要对 Item 方法返回类型做任何假设;您的代码应该能够处理多个项目类型或 ConversationHeader 对象。”

请注意,表单正在由 ItemSend 事件调用。

4

1 回答 1

1

首先,将该代码放入 Initialize 事件中并不是一个好的举措。需要移动到实际需要的点击事件中。

然后,我从另外两篇文章中找到了我需要的代码,将它们合并并缩短了。

使用当前打开的电子邮件

https://superuser.com/questions/795831/outlook-2013-vba-refer-to-editor-in-reading-pane

最后结果

Dim oInspector As Inspector
Dim email As MailItem
Dim oexp As Explorer

Set oInspector = Application.ActiveInspector
Set oexp = Application.ActiveExplorer

If oInspector Is Nothing Then
     'Set email = Application.ActiveExplorer.Selection.Item(1)
     Set email = oexp.ActiveInlineResponse
     If email Is Nothing Then
        'MsgBox "No active inspector or inline response"
        Exit Sub
     End If
Else
    Set email = oInspector.CurrentItem
End If 'oInspector is Nothing

If email.Sent Then
   'MsgBox "This is not an editable email"
Else
    'Checks to see if a project number (that's not on the list) may be in the subject already
    If Val(email.Subject) > 10000 Then
        TagMsg.Height = tall
        TagMsg.NewProjID = Format(Val(email.Subject), "00000")
        TagMsg.NewProjDesc.SetFocus
    Else
        'Set height of form (prior to pressing "More" button
        TagMsg.Height = short
    End If
End If 'email.sent

注意:这仍然依赖于 ItemSend 事件调用它的事实,并且活动或当前项目将是我刚刚按下“发送”的电子邮件。

谢谢你,retailcoder 的评论。

于 2014-12-09T14:44:27.347 回答