vba - 当不是 "Popped Out"时出现 block 错误的 Outlook 2013 用户窗体

标签 vba userform outlook-2013

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

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

“运行时错误‘91’对象变量或未设置 block 变量”

我尝试先检查加载表单 - 但似乎对我的用户表单的任何调用,甚至是 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 事件调用。

最佳答案

首先,将该代码放入 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 的评论。

关于vba - 当不是 "Popped Out"时出现 block 错误的 Outlook 2013 用户窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26740281/

相关文章:

从单元格值列表中获取路径\文件\范围的封闭工作表中引用范围的 VBA 代码

Excel/VBA 在从 VBA 转置/粘贴时将日期转换为美国格式

vba - 在运行时使用 vba 将多个标签和文本框添加到 Excel 用户窗体

excel - 用户表单:在文本框中继续运行总计

excel - 在一行中引用多个工作表

vba - Excel - 将索引匹配公式更改为 VBA 操作

vba - Excel 未正确关闭 - 用户窗体挂起?

vba - 如何设置具有特定时间范围的自动外出回复

html - outlook 2013 中出现的图像下方的空间

c# - 如何显示指定附件的 Outlook 电子邮件附件预览