Как получить выбранный текст из объекта Wordeditor и измените его цвет?
-
08-10-2019 - |
Вопрос
Я пытаюсь использовать WordEditor
Объект для изменения цвета выбранного текста (Outlook VBA), но я не могу найти документацию или примеры о том, как это сделать. Есть идеи?
Я не хочу использовать HTML-редактор, мне нужно решение для WordEditor
.
Я попытался отладить код и использовать OutlooksPy, но каждый раз, когда я вхожу в WordeDitor.content My Outlook замерзает и перезапускается :(.
Использование Outlook 2010 в Windows 7
Решение
ОК, - я нашел что-то, что работает. Уродливый, но работает:
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
В конце концов я нашел ссылку, что Слово Возвращает А. Document
объект. Оттуда это было 2 часа перехода на очень медленную веб-помощь MSDN, чтобы выяснить, что для того, чтобы получить выбранный текст, который мне нужно было подняться на один уровень к Application
. Отказ Важное примечание - смена rng.Style.Font
не сделал то, что хотел, чтобы это сделал, он изменил весь документ, когда я начал использовать with rng.font
Моя проблема была решена (благодаря возможностям записи Marco Excel для отображения мне правильный синтаксис)
Другие советы
Аннотации на немецком языке
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
Verweise: (Я не знаю, как это называется в английской версии)
- Visual Basic для приложений
- Библиотека объектов Microsoft Outlook 15.0
- Ол-Автоматизация
- Библиотека объектов Microsoft Office 15.0
- Библиотека объектов Microsoft Word 15.0
Gruz $ 3V | |
другой пример:
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 | |