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

查看:26
本文介绍了如何从 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 的宏记录能力向我展示了正确的语法)

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天全站免登陆