VBA设置Outlook中选定文本的格式
我想突出显示电子邮件中的文本,将其格式化为字体控制台,并缩进一次 我已尝试此操作,但出现错误:VBA设置Outlook中选定文本的格式,vba,outlook,outlook-2016,Vba,Outlook,Outlook 2016,我想突出显示电子邮件中的文本,将其格式化为字体控制台,并缩进一次 我已尝试此操作,但出现错误: Sub Code() Selection.Font.Name = "Consolas" Selection.Paragraphs.Indent End Sub 运行时错误“429”: ActiveX组件无法创建对象 看起来您正在尝试将Word对象模型与Outlook混合使用。Outlook中的选择类与Word对象模型中的选择类不同。此外,Outlook中没有此类快捷方式。您必须在
Sub Code()
Selection.Font.Name = "Consolas"
Selection.Paragraphs.Indent
End Sub
运行时错误“429”:
ActiveX组件无法创建对象
看起来您正在尝试将Word对象模型与Outlook混合使用。Outlook中的
选择
类与Word对象模型中的选择
类不同。此外,Outlook中没有此类快捷方式。您必须在每次需要时检索它
Outlook对象模型提供了三种处理项目主体的主要方法:
正文
属性。原始文本HTMLBody
属性。正文由html标记表示李>
Inspector
类的WordEditor
属性返回表示主体的Word文档类的实例李>
您可以在MSDN中的文章中阅读更多有关这方面的内容。它深入描述了所有这些属性 看起来您正试图将Word对象模型与Outlook混合使用。Outlook中的
选择
类与Word对象模型中的选择
类不同。此外,Outlook中没有此类快捷方式。您必须在每次需要时检索它
Outlook对象模型提供了三种处理项目主体的主要方法:
正文
属性。原始文本HTMLBody
属性。正文由html标记表示李>
Inspector
类的WordEditor
属性返回表示主体的Word文档类的实例李>
您可以在MSDN中的文章中阅读更多有关这方面的内容。它深入描述了所有这些属性 您可以使用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
子格式选择()
'进行额外的故障排除验证
'Outlook中的代码
'邮件必须处于编辑模式-撰写、回复、转发
'如果正在阅读邮件,则在“操作”下编辑邮件
'选择一些文本
我的督察是督察
将myObject设置为对象
将myItem设置为mailItem
将myDoc设置为Word.Document
选择作为Word.Selection
设置myInspector=ActiveInspector
如果我的检查员什么都不是
MsgBox“无检查器。打开邮件项并选择一些文本。”
后藤
如果结束
如果myInspector.editor键入olEditorWord,则
'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/oleditortype-enumeration-outlook
'Word/4/Microsoft Office Word编辑器
Debug.Print“EditorType:&myInspector.EditorType
MsgBox“编辑器不是Microsoft Office Word编辑器”
后藤
如果结束
“可能不需要。EditorType应该足够了
'如果myInspector.IsWordMail=False,则
'MsgBox“myInspector.IsWordMail=False”
“快走
"完"
出错时继续下一步
设置myObject=myInspector.currentItem
错误转到0
如果myObject什么都不是,那么
MsgBox“打开邮件项并选择一些文本。”
后藤
如果结束
如果myObject.MessageClass=“IPM.Note”,则
'应等同于如果myObject.Class=olMail
设置myItem=myObject
设置myDoc=myInspector.WordEditor
设置mySelection=myDoc.Application.Selection
Debug.Print“所选文本为:”&mySelection
MsgBox“所选文本为:”&vbCr&vbCr&mySelection
mySelection.Font.name=“控制台”
mySelection.parations.Indent
其他的
MsgBox“不是邮件项目。请打开邮件项目并选择一些文本。”
后藤
如果结束
现存的:
设置myInspector=Nothing
设置myObject=Nothing
设置myItem=Nothing
设置myDoc=Nothing
设置mySelection=Nothing
端接头
您可以使用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
子格式选择()
'进行额外的故障排除验证
'Outlook中的代码
'邮件必须处于编辑模式-撰写、回复、转发
'如果正在阅读邮件,则在“操作”下编辑邮件
'选择一些文本
我的督察是督察
将myObject设置为对象
将myItem设置为mailItem
将myDoc设置为Word.Document
选择作为Word.Selection
设置myInspector=ActiveInspector
如果我的检查员什么都不是
MsgBox“无检查器。打开邮件项并选择一些文本。”
后藤
如果结束
如果myInspector.editor键入olEditorWord,则
'https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/oleditortype-enumeration-outlook
'Word/4/Microsoft Office Word编辑器
Debug.Print“EditorType:&myInspector.EditorType
MsgBox“编辑器不是Microsoft Office Word编辑器”
后藤
如果结束
“可能不需要。EditorType应该足够了
'如果myInspector.IsWordMail=False,则
'MsgBox“myInspector.IsWordMail=False”
“快走
"完"
出错时继续下一步
设置myObject=myInspector.currentItem
错误转到0
如果myObject什么都不是,那么
MsgBox“打开邮件项并选择一些文本。”
后藤
如果结束
如果myObject.MessageClass=“IPM.Note”,则
'应等同于如果myObject.Class=olMail
设置myItem=myObject
设置myDoc=myInspector.WordEditor
设置mySelection=myDoc.Application.Selection
Debug.Print“所选文本为:”&mySelection
MsgBox“所选文本为:”&vbCr&vbCr&mySelection
mySelection.Font.name=“控制台”
mySelection.parations.Indent
其他的
MsgBox“不是邮件项目。请打开邮件项目并选择一些文本。”
后藤
如果结束
现存的