没有“弹出”时Outlook 2013 Userform与块错误

时间:2014-11-04 16:33:56

标签: vba userform outlook-2013

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

每当您尝试发送电子邮件时,宏都会弹出一个对话框 - 用关键字标记主题行。问题是,如果我刚刚启动Outlook,并且我提出了新的电子邮件或回复 - Outlook 2013中的默认设置是将它带入前者"阅读窗格"而不是在一个新的窗口。如果我没有击中" Pop Out"我尝试发送,我的宏崩溃了这个错误:

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

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

奇怪的是,如果我记得" Pop Out"我的第一封电子邮件,它每次都运行正常,直到我关闭/重新打开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事件正在调用该表单。

1 个答案:

答案 0 :(得分:1)

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

然后,我从其他两个帖子中找到了我需要的代码,合并并缩短了它们。

Working with current open email

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的评论。