Outlook VBA,在MsgBox中显示标题

时间:2014-12-30 19:16:35

标签: outlook outlook-vba

我编写了以下模块,以在MsgBox中显示当前在Outlook中打开的电子邮件的主题。

Outlook 2010.版本14.0.7140.5002。

我收到运行时错误'424'对象必需。

调试器突出显示:

Set objItem = objApp.ActiveInspector.CurrentItem

代码如下:

Sub ShowTitle()

    Dim objMail As Object
    Set objItem = objApp.ActiveInspector.CurrentItem
    Dim Title As String
    Set objMail = objItem.Subject

    Title = objItem
    MsgBox (Title)

End Sub

3 个答案:

答案 0 :(得分:3)

Sub ShowTitle()

Dim objMail As Object
Set objItem = objApp.ActiveInspector.CurrentItem

Dim Title As String
Title = objItem.Subject

MsgBox (Title)

End Sub

答案 1 :(得分:0)

试试这个未经测试的代码。

Option Explicit

Sub ShowTitle()

    'Dim objApp as Outlook.Application
    Dim objItem As Object
    Dim Title As String

    ' If exclusively in Outlook there is no need for objApp
    'Set objApp = Outlook.Application
    'Set objItem = objApp.ActiveInspector.CurrentItem

    ' or simpler
    'Set objItem = Application.ActiveInspector.CurrentItem

    ' or simplest
    Set objItem = ActiveInspector.CurrentItem

    If objItem is mailitem then
        Title = objItem.Subject
        MsgBox (Title)
    End if

    'Set objApp = Nothing
    Set objItem = Nothing

End Sub

答案 2 :(得分:0)

Diane Poremsky在Slipstick上回答了this article中的这个问题。根据她的回答,制作一个包含此功能的模块:

#include <stdio.h>
#include <stdlib.h>

void tf(int** ptr);

int main(int argc, char* argv[])
{
    int *arr;
    tf(&arr);
    free(arr);    
}

void tf(int** ptr)
{    
    *ptr = (int *) calloc(2, sizeof(int));
    if(*ptr!=NULL)
    {
        *ptr[0]=10; 
        *ptr[1]=20;
    }
}

然后,根据她的回答,在你的宏中用这一行调用该函数:

Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application

    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select

    Set objApp = Nothing 
End Function

所以,修改你的代码,我做了第二个宏,这对我有用:

Set objItem = GetCurrentItem()

这样,您可以在选定的电子邮件或打开的电子邮件中运行subjectLine()。