Vba 如何从WordEditor对象获取所选文本并对其进行更改';什么颜色?
我试图使用Vba 如何从WordEditor对象获取所选文本并对其进行更改';什么颜色?,vba,outlook,outlook-2010,Vba,Outlook,Outlook 2010,我试图使用WordEditor对象修改所选文本(Outlook VBA)的颜色,但找不到有关如何修改的文档或示例。有什么想法吗 我不想使用HTML编辑器,我需要WordEditor的解决方案 我试着调试代码并使用OutlookSpy,但每次我进入WordEditor.Content时,我的outlook都会冻结并重新启动:( 在Windows 7上使用Outlook 2010好的-我发现了一些有用的东西。很难看,但是有用: Sub EmphesizeSelectedText(color As L
WordEditor
对象修改所选文本(Outlook VBA)的颜色,但找不到有关如何修改的文档或示例。有什么想法吗
我不想使用HTML编辑器,我需要WordEditor
的解决方案
我试着调试代码并使用OutlookSpy,但每次我进入WordEditor.Content时,我的outlook都会冻结并重新启动:(
在Windows 7上使用Outlook 2010好的-我发现了一些有用的东西。很难看,但是有用:
Sub EmphesizeSelectedText(color As Long)
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 document = msg.GetInspector.WordEditor
Set rng = document.Application.Selection
With rng.font
.Bold = True
.color = color
End With
End If
End If
Set insp = Nothing
Set rng = Nothing
Set hed = Nothing
Set msg = Nothing
End Sub
最后,我找到了一个引用,该引用返回一个文档
对象。从那以后,我花了两个小时浏览了MSDN非常缓慢的web帮助,发现要获取所选文本,我需要升级一级到应用程序
。
重要提示-更改
rng.Style.Font
并没有达到我想要的效果,它改变了整个文档,当我开始将用于rng.Font
时,我的问题得到了解决(感谢Excel的marco recording功能向我展示了正确的语法)注释是德语的
Option Explicit
'Sub EmphesizeSelectedText(color As Long)
Sub EmphesizeSelectedText()
Dim om_msg As Outlook.MailItem
Dim oi_insp As Outlook.Inspector
Dim ws_selec As Word.Selection
Dim wd_Document As Word.Document
Dim str_test As String
Dim lng_color As Long
lng_color = 255
'Zugriff auf aktive E-Mail
Set oi_insp = Application.ActiveInspector()
'Überprüft ob es sich wirklich um eine E-Mail handelt
If oi_insp.CurrentItem.Class = olMail Then
Set om_msg = oi_insp.CurrentItem
If oi_insp.EditorType = olEditorWord Then
' es gibt noch "olEditorHTML", "olEditorRTF", "olEditorText" und "olEditorWord"
' ist bei mir aber immer "olEditorWord" (= 4) - egal was ich im E-Mail Editor auswähle
' Set wd_Document = om_msg.Getinspector.WordEditor ' macht das gleiche wie nächste Zeile
Set wd_Document = oi_insp.WordEditor
Set ws_selec = wd_Document.Application.Selection
str_test = ws_selec.Text
Debug.Print ws_selec.Text
ws_selec.Text = "foo bar"
If om_msg.BodyFormat <> olFormatPlain Then
' auch wenn om_msg.BodyFormat = olFormatPlain ist, kann oi_insp.EditorType = olEditorWord sein
' doch dann gehen Formatierungen nicht -> Error !!!
With ws_selec.Font
.Bold = True
.color = lng_color ' = 255 = red
.color = wdColorBlue
End With
End If
ws_selec.Text = str_test
End If
End If
Set oi_insp = Nothing
Set ws_selec = Nothing
Set om_msg = Nothing
Set wd_Document = Nothing
End Sub
选项显式
'子开发选择的文本(颜色与长度相同)
子开发SelectedText()
Dim om_msg作为Outlook.MailItem
Dim oi_督察作为前景督察
作为Word.Selection的Dim ws_selec
作为Word.Document的Dim wd_文档
作为字符串的Dim str_测试
暗淡的颜色和长的颜色一样
lng_颜色=255
“Zugriff auf aktive电子邮件
设置oi_insp=Application.ActiveInspector()
“我的工作是通过电子邮件完成的
如果oi_insp.CurrentItem.Class=olMail,则
设置om\U msg=oi\U insp.CurrentItem
如果oi_insp.EditorType=olEditorWord,则
'es gibt noch“olEditorHTML”、“olEditorRTF”、“olEditorText”和“olEditorWord”
贝米尔·阿伯·伊默是我的即时通讯编辑奥苏勒吗
'Set wd_Document=om_msg.Getinspector.WordEditor'macht das gleiche wie nächste Zeile
设置wd_Document=oi_insp.WordEditor
设置ws_selec=wd_Document.Application.Selection
str_test=ws_selec.Text
调试。打印ws_selec.Text
ws_selec.Text=“foo bar”
如果om_msg.BodyFormat为普通格式,则
'auch wenn om_msg.BodyFormat=olformatist,kann oi_insp.EditorType=olEditorWord sein
'doch dann gehen Formatierungen nicht->错误!!!
使用ws_selec.Font
.Bold=正确
.color=lng\u color'=255=红色
.color=wdColorBlue
以
如果结束
ws_selec.Text=str_测试
如果结束
如果结束
设置oi_insp=无
设置ws_selec=Nothing
设置om_msg=Nothing
Set wd_Document=Nothing
端接头
Verweise:(我不知道英文版怎么称呼它)
- Visual Basic for Applications
- Microsoft Outlook 15.0对象库
- OLE自动化
- Microsoft Office 15.0对象库
- Microsoft Word 15.0对象库
Option Explicit
Private Sub Test_It()
Dim om_Item As Outlook.MailItem
Dim oi_Inspector As Outlook.Inspector
Dim wd_Doc As Word.Document
Dim wd_Selection As Word.Selection
Dim wr_Range As Word.Range
Dim b_return As Boolean
Dim str_Text As String
str_Text = "Hello World"
'Zugriff auf aktive E-Mail
Set oi_Inspector = Application.ActiveInspector()
Set om_Item = oi_Inspector.CurrentItem
Set wd_Doc = oi_Inspector.WordEditor
'Zugriff auf Textmarkierung in E-Mail
Set wd_Selection = wd_Doc.Application.Selection
wd_Selection.InsertBefore str_Text
'Zugriff auf 'virtuelle' Markierung
'wr_Range muss auf das ganze Dokument gesetzt werden !
Set wr_Range = wd_Doc.Content
'Suche in E-Mail Text
With wr_Range.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindStop
.MatchWildcards = True
.Text = "#%*%#"
End With
b_return = True
Do While b_return
b_return = wr_Range.Find.Execute
If b_return Then
' Es wurde gefunden
str_Text = wr_Range.Text
'schneide den Anfangstext und das Ende ab
'str_TextID = Mid$(str_TextID, 11, Len(str_TextID) - 12)
MsgBox ("Es wurde noch folgender Schlüssel gefunden:" & vbCrLf & str_Text)
End If
Loop
'aktiv Range ändern
'wr_Range muss auf das ganze Dokument gesetzt werden !
Set wr_Range = wd_Doc.Content
wr_Range.Start = wr_Range.Start + 20
wr_Range.End = wr_Range.End - 20
'Text formatieren
With wr_Range.Font
.ColorIndex = wdBlue
.Bold = True
.Italic = True
.Underline = wdUnderlineDotDashHeavy
End With
'Freigeben der verwendeten Variablen
Set oi_Inspector = Nothing
Set om_Item = Nothing
Set wd_Doc = Nothing
Set wd_Selection = Nothing
Set wr_Range = Nothing
End Sub
Gruz$3v | \ |如果没有
Option Explicit
,这个答案真的很难使用-我认为这应该可以解决我遇到的一个类似问题,但是如果没有声明所有变量,它是否适用就一点也不清楚了。@enderland-我不熟悉“Option Explicit”-它是什么,它与问题有什么关系?Option Explicit是一个指示解析器坚持声明(标注)每个变量并为其分配类型的古老基本构造。如果复制上述示例的模块具有Option Explicit语句,它会大声抱怨“rng”未定义。Verweise=引用。