Vba Outlook 2013 Userform在不使用时出现块错误";“突然出现”;

Vba Outlook 2013 Userform在不使用时出现块错误";“突然出现”;,vba,userform,outlook-2013,Vba,Userform,Outlook 2013,我有一组宏,它们在Outlook 2003、2007和2010中工作过。事实上,它在2013年仍然有效,除了一个特定的案例 每当你试图发送电子邮件时,宏就会弹出一个对话框,用关键字标记主题行。问题是,如果我刚启动Outlook,并打开一封新的电子邮件或回复,Outlook 2013的默认设置是将其放入以前的“阅读窗格”,而不是新窗口。如果我没有点击“弹出”并尝试发送,我的宏将崩溃,并出现以下错误: “运行时错误'91'对象变量或未设置块变量” 我试图先检查是否加载表单,但似乎对我的userfor

我有一组宏,它们在Outlook 2003、2007和2010中工作过。事实上,它在2013年仍然有效,除了一个特定的案例

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

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

我试图先检查是否加载表单,但似乎对我的userform的任何调用,甚至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方法返回类型进行任何假设;您的代码应该能够处理多个Item类型或ConversationHeader对象。”


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

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

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

最终结果

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

如果你在
初始化
激活
布局
处理程序中有任何代码,如果你把它包含在这里,它可能会帮助我们帮助你。当你“弹出”时会运行什么代码一封电子邮件?它可能有助于在你的文章中嵌入一些代码…随时都可以自由发表你的文章;目前(如果没有代码),您的问题不太可能得到回答:(