VBA 在 Outlook 中设置所选文本的格式
VBA to format selected text in Outlook
我想突出显示电子邮件中的文本并将其格式化为字体控制台并缩进一次。
我已经试过了,但出现错误:
Sub Code()
Selection.Font.Name = "Consolas"
Selection.Paragraphs.Indent
End Sub
Run-time error '429':
ActiveX component can't create object
您似乎在尝试将 Word 对象模型与 Outlook 混合使用。 Outlook 中的 Selection
class 与 Word 对象模型中的 Selection
class 不同。此外,在 Outlook 中没有这样的快捷方式。您必须在每次需要时取回它。
Outlook 对象模型提供了三种处理项目正文的主要方式:
-
Body
属性。原始文本。
-
HTMLBody
属性。正文由 html 标记表示。
- Word 对象模型。
Inspector
class returns 的 WordEditor
属性 和表示正文的 Word 文档 class 的实例。
您可以在 MSDN 的 Chapter 17: Working with Item Bodies 文章中阅读更多相关信息。它深入描述了所有这些属性。
您可以使用 WordEditor 编辑邮件中选定的文本:
Private Sub Code()
' Mail must be in edit mode - compose, reply, forward
' If reading mail then under Actions | Edit Message
' Select some text
Dim objDoc As Object
Dim objSel As Object
Set objDoc = ActiveInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.Font.name = "Consolas"
objSel.Paragraphs.Indent
End Sub
验证代码:
Sub FormatSelection()
' With extra validation for troubleshooting
' Code in Outlook
' Mail must be in edit mode - compose, reply, forward
' If reading mail then under Actions | Edit Message
' Select some text
Dim myInspector As Inspector
Dim myObject As Object
Dim myItem As mailItem
Dim myDoc As Word.Document
Dim mySelection As Word.Selection
Set myInspector = ActiveInspector
If myInspector Is Nothing Then
MsgBox "No inspector. Open a mailitem and select some text."
GoTo ExitRoutine
End If
If myInspector.EditorType <> olEditorWord Then
'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/oleditortype-enumeration-outlook
' olEditorWord / 4 / Microsoft Office Word editor
Debug.Print "EditorType: " & myInspector.EditorType
MsgBox "Editor is not Microsoft Office Word editor"
GoTo ExitRoutine
End If
' Probably not needed. EditorType should be enough
'If myInspector.IsWordMail = False Then
' MsgBox "myInspector.IsWordMail = False"
' GoTo ExitRoutine
'End If
On Error Resume Next
Set myObject = myInspector.currentItem
On Error GoTo 0
If myObject Is Nothing Then
MsgBox "Open a mailitem and select some text."
GoTo ExitRoutine
End If
If myObject.MessageClass = "IPM.Note" Then
'Should be equivalent to If myObject.Class = olMail Then
Set myItem = myObject
Set myDoc = myInspector.WordEditor
Set mySelection = myDoc.Application.Selection
Debug.Print "Selected text is: " & mySelection
MsgBox "Selected text is: " & vbCr & vbCr & mySelection
mySelection.Font.name = "Consolas"
mySelection.Paragraphs.Indent
Else
MsgBox "Not a mailitem. Open a mailitem and select some text."
GoTo ExitRoutine
End If
ExitRoutine:
Set myInspector = Nothing
Set myObject = Nothing
Set myItem = Nothing
Set myDoc = Nothing
Set mySelection = Nothing
End Sub
我想突出显示电子邮件中的文本并将其格式化为字体控制台并缩进一次。
我已经试过了,但出现错误:
Sub Code()
Selection.Font.Name = "Consolas"
Selection.Paragraphs.Indent
End Sub
Run-time error '429':
ActiveX component can't create object
您似乎在尝试将 Word 对象模型与 Outlook 混合使用。 Outlook 中的 Selection
class 与 Word 对象模型中的 Selection
class 不同。此外,在 Outlook 中没有这样的快捷方式。您必须在每次需要时取回它。
Outlook 对象模型提供了三种处理项目正文的主要方式:
-
Body
属性。原始文本。 -
HTMLBody
属性。正文由 html 标记表示。 - Word 对象模型。
Inspector
class returns 的WordEditor
属性 和表示正文的 Word 文档 class 的实例。
您可以在 MSDN 的 Chapter 17: Working with Item Bodies 文章中阅读更多相关信息。它深入描述了所有这些属性。
您可以使用 WordEditor 编辑邮件中选定的文本:
Private Sub Code()
' Mail must be in edit mode - compose, reply, forward
' If reading mail then under Actions | Edit Message
' Select some text
Dim objDoc As Object
Dim objSel As Object
Set objDoc = ActiveInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.Font.name = "Consolas"
objSel.Paragraphs.Indent
End Sub
验证代码:
Sub FormatSelection()
' With extra validation for troubleshooting
' Code in Outlook
' Mail must be in edit mode - compose, reply, forward
' If reading mail then under Actions | Edit Message
' Select some text
Dim myInspector As Inspector
Dim myObject As Object
Dim myItem As mailItem
Dim myDoc As Word.Document
Dim mySelection As Word.Selection
Set myInspector = ActiveInspector
If myInspector Is Nothing Then
MsgBox "No inspector. Open a mailitem and select some text."
GoTo ExitRoutine
End If
If myInspector.EditorType <> olEditorWord Then
'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/oleditortype-enumeration-outlook
' olEditorWord / 4 / Microsoft Office Word editor
Debug.Print "EditorType: " & myInspector.EditorType
MsgBox "Editor is not Microsoft Office Word editor"
GoTo ExitRoutine
End If
' Probably not needed. EditorType should be enough
'If myInspector.IsWordMail = False Then
' MsgBox "myInspector.IsWordMail = False"
' GoTo ExitRoutine
'End If
On Error Resume Next
Set myObject = myInspector.currentItem
On Error GoTo 0
If myObject Is Nothing Then
MsgBox "Open a mailitem and select some text."
GoTo ExitRoutine
End If
If myObject.MessageClass = "IPM.Note" Then
'Should be equivalent to If myObject.Class = olMail Then
Set myItem = myObject
Set myDoc = myInspector.WordEditor
Set mySelection = myDoc.Application.Selection
Debug.Print "Selected text is: " & mySelection
MsgBox "Selected text is: " & vbCr & vbCr & mySelection
mySelection.Font.name = "Consolas"
mySelection.Paragraphs.Indent
Else
MsgBox "Not a mailitem. Open a mailitem and select some text."
GoTo ExitRoutine
End If
ExitRoutine:
Set myInspector = Nothing
Set myObject = Nothing
Set myItem = Nothing
Set myDoc = Nothing
Set mySelection = Nothing
End Sub