2014-09-30 87 views
0

我有一個宏,我寫在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 

回答

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