VBA宏选定的文本Outlook 2010
我有一个用vba编写的宏,可以在电子邮件中获取选定的文本,现在将其显示在MsgBox中.
I have a macro i wrote in vba to get selected text in an email message and for now show it in a MsgBox.
当我在电子邮件中以选定的文本运行宏并且在其自己的窗口中打开电子邮件时,此方法非常有用.
This works great when i run the macro with selected text in an email message with the email message opened in its own window.
当我尝试在主Outlook程序的电子邮件窗格中选择的文本进行尝试时,它给我一个错误对象变量或未设置块变量",这是来自设置检查"行
when i try this with the text selected in the email pane of the main outlook program it gives me an error "Object variable or With block variable not set" this is coming from the "Set insp" line
有什么想法吗?
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
您需要检查检查器是否为空,如果是,则需要使用资源管理器对象.这是您编写的包含支票的代码
you need to check to see if the inspector is nothing and if it is then oyu need to utilize the explorer object. Here is your code written to include that check
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