首页 文章

VBA宏选定文本Outlook 2010

提问于
浏览
1

我有一个宏,我在vba中写道,以获取电子邮件中的选定文本,现在在MsgBox中显示它 .

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

当我尝试使用在主Outlook程序的电子邮件窗格中选择的文本时,它给出了一个错误“对象变量或没有设置块变量”这来自“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 回答

  • 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
    

相关问题