如何从WordEditor对象中获取所选文本并更改其颜色? [英] How do I get the selected text from a WordEditor Object and change it's color?

查看:107
本文介绍了如何从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)

这篇关于如何从WordEditor对象中获取所选文本并更改其颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆