VBA宏选定文本Outlook 2010

时间:2014-09-30 20:10:51

标签: vba outlook

我有一个我在vba中编写的宏,用于在电子邮件中获取所选文本,现在将其显示在MsgBox中。

当我在电子邮件中运行带有所选文本的宏并且在自己的窗口中打开电子邮件消息时,这非常有用。

当我尝试使用在主Outlook程序的电子邮件窗格中选择的文本时,它会给我一个错误"对象变量或With block变量未设置"这是来自" Set insp"线

任何想法?

Sub showseltext()

Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector

Set insp = Application.ActiveInspector

If insp.CurrentItem.Class = olMail Then
Set msg = insp.CurrentItem

If insp.EditorType = olEditorWord Then
Set hed = msg.GetInspector.WordEditor
Set appWord = hed.Application
Set rng = appWord.Selection
MsgBox (rng)
End If

End If

Set appWord = Nothing
Set insp = Nothing
Set rng = Nothing
Set hed = Nothing
Set msg = Nothing

End Sub

1 个答案:

答案 0 :(得分:1)

您需要检查检查员是否一无所知,如果是,那么oyu需要使用资源管理器对象。以下是您编写的代码以包含该检查

Sub showseltext()

Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector

If Application.ActiveInspector Is Nothing Then
    If Application.ActiveExplorer.Selection.Count = 1 Then
        If Application.ActiveExplorer.Selection.Item(1).Class = olMail Then
            Set msg = Application.ActiveExplorer.Selection.Item(1)
        End If
    Else
        'to many items selected
        MsgBox "Please select one email"
    End If
Else
    Set insp = Application.ActiveInspector
    If insp.CurrentItem.Class = olMail Then
        Set msg = insp.CurrentItem
    End If
End If

If msg Is Nothing Then
    MsgBox "could not determine the mail item"
Else
    If msg.GetInspector.EditorType = olEditorWord Then
        Set hed = msg.GetInspector.WordEditor
        Set appWord = hed.Application
        Set Rng = appWord.Selection
        MsgBox (Rng)
    End If
End If

Set appWord = Nothing
Set insp = Nothing
Set Rng = Nothing
Set hed = Nothing
Set msg = Nothing

End Sub
相关问题