如何从WordEditor对象中获取所选文本并更改其颜色?
我正在尝试使用WordEditor
对象修改所选文本的颜色(Outlook VBA),但是我找不到有关此操作的文档或示例.有什么想法吗?
I'm trying to use the WordEditor
object to modify the color of the selected text (Outlook VBA) but i'm unable to find documentation or examples on how to do it. Any ideas?
我不想使用HTML编辑器,我需要WordEditor
的解决方案.
I don't want to use the HTML editor, i need a solution for WordEditor
.
我尝试调试代码并使用OutlookSpy,但是每次进入WordEditor.Content时,Outlook都会冻结并重新启动:(.
I tried debuging the code and using OutlookSpy, but everytime i go into WordEditor.Content my outlook freezes and restarts :(.
在Windows 7上使用Outlook 2010
Using Outlook 2010 on Windows 7
确定-我发现了一些可行的方法.丑陋,但可行:
OK - I found something that works. Ugly, but works:
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
最终,我找到了一个引用,该引用是 WordEditor 返回一个 Document
对象.从那里经过2个小时,浏览了MSDN非常慢的网络帮助,以找出要获取所选文本的方法,我需要将文本上移至 Application
.
重要说明-更改rng.Style.Font
并没有完成我想要的操作,它更改了整个文档,当我开始使用with rng.font
时,我的问题就解决了(感谢Excel的marco录制功能,为我显示了正确的语法)
Eventually I found a reference that WordEditor returns a Document
object. From there it was 2 hrs of going over MSDN's very slow web-help to find out that to get the selected text i needed to go up one level to the Application
.
Important note - changing rng.Style.Font
did not do what i wanted it to do, it changed the entire document, when i started using the with rng.font
my problem was solved (Thanks to Excel's marco recording abilities for showing me the correct syntax)