VBA宏选定的文本Outlook 2010

问题描述:

我有一个宏,我写在vba中获取选定的文本在电子邮件中,现在显示它在一个MsgBox。VBA宏选定的文本Outlook 2010

这个工程很好,当我运行与选定的文本在电子邮件与自己的窗口中打开电子邮件消息的宏。

当我尝试这与在主Outlook程序的电子邮件窗格中选择文本它给了我一个错误“对象变量或带块变量未设置”这是从“设置督察”现身线

有任何想法吗?

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 

您需要检查检查器是否没有,如果它然后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