2014-09-30 2 views
0

У меня есть макрос, который я написал в vba, чтобы получить выделенный текст в сообщении электронной почты и пока показать его в MsgBox.VBA Macro Выбранный текст Outlook 2010

Это отлично работает, когда я запускаю макрос с выделенным текстом в сообщении электронной почты с сообщением электронной почты, открытым в его собственном окне.

, когда я пытаюсь это с текстом, выбранным в электронной панели основной программы внешнего вида она дает мне ошибку «Объект переменная или переменная блока не установлен» это исходит из «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

вам нужно проверить, если инспектор ничего, и если это то ою нужно использовать объект проводника. Вот ваш код, который должен содержать эту проверку.

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 
Смежные вопросы