MS Outlook 宏删除选定的文本
Posted
技术标签:
【中文标题】MS Outlook 宏删除选定的文本【英文标题】:MS Outlook macro to strikeout selected text 【发布时间】:2009-11-19 13:19:38 【问题描述】:任务是将 strikeout 应用于所选文本区域中的当前字体。 困难在于 Outlook 不支持动态录制宏 - 它希望手动编写代码。
例如下面的简单代码:
Selection.Font.Strikethrough = True
适用于 Word,但适用于 Outlook:
Run-time error '424':
Object required
【问题讨论】:
我正在使用 MS Outlook 2003。这个想法是将字体应用于预定义的文本块(例如“句子是粗体”,或匹配邮件正文中的某些模式),而是手动选择文本(我的意思是,用鼠标)。 只是想跟进看看以下是否回答了您的问题。 【参考方案1】:这假设您的盒子上还安装了 Word。如果是这样,您可以从 Outlook VBE 访问大部分 Word OM,而无需使用 ActiveInspector.WordEditor 对象引用 Word。
Sub StrikeThroughinMailItem()
Dim objOL As Application
Dim objDoc As Object
Dim objSel As Object
Set objOL = Application
Set objDoc = objOL.ActiveInspector.WordEditor
Set objSel = objDoc.Windows(1).Selection
objSel.Font.Strikethrough = True
End Sub
【讨论】:
+1,如果您没有 Word,或者 Outlook 设置为使用 html,那么您可以通过ActiveInspector.HTMLEditor
访问它。【参考方案2】:
这里有一些关于弄乱打开邮件的注意事项,没有检查,它只是假设你有一个打开的邮件项目。如果你想多说一点你想做什么,在什么版本,我也许可以提供更多帮助。
Dim ActiveMessage As MailItem
Dim strHTML As String
Set ActiveMessage = ActiveInspector.CurrentItem
Debug.Print ActiveMessage.Body
Debug.Print ActiveMessage.HTMLBody
strHTML = Replace(ActiveMessage.Body, "This sentence is bold", _
"<STRONG>This sentence is bold</STRONG>")
ActiveMessage.HTMLBody = strHTML
Debug.Print ActiveMessage.HTMLBody
【讨论】:
这个想法是将字体应用于手动选择(例如,使用鼠标)文本;并且消息正文没有任何有关选择的信息。【参考方案3】:您需要访问 Inspector 的 HTMLEditor 或 WordEditor。检查帮助文件以获取示例代码。如果您使用的是 WordEditor,那么您可以在 Word 中录制宏,然后使用 WordEditor 将生成的代码合并到 Outlook 宏中。
Public Sub DoIt()
'must set word as mail editor
'must set reference to word object library
Dim oInspector As Outlook.Inspector
Dim oDoc As Word.Document
Dim oItem As Outlook.MailItem
Set oItem = Outlook.Application.CreateItem(olMailItem)
oItem.BodyFormat = olFormatRichText 'must set, unless default is rich text
Set oInspector = oItem.GetInspector
oInspector.Display 'must display in order for selection to work
Set oDoc = oInspector.WordEditor
'better to use word document instead of selection
'this sample uses selection because word's macro recording using the selection object
Dim oSelection As Word.Selection
Set oSelection = oDoc.Application.Selection
oSelection.TypeText Text:="The task is to apply strikethroughout."
oSelection.MoveLeft Unit:=wdCharacter, Count:=4
oSelection.MoveLeft Unit:=wdCharacter, Count:=7, Extend:=wdExtend
oSelection.Font.Strikethrough = True
End Sub
【讨论】:
【参考方案4】:从上面 Todd Main 的优秀示例开始。 我稍微修改了代码以在内联回复窗格中工作,因为我们找不到将删除线添加到 QAT 或功能区的简单方法。 如果已设置删除线,我还添加了一个 if 块来切换删除线。
Sub StrikeThroughinInlineReply()
Dim objOL As Application
Dim objDoc As Object
Dim objSel As Object
Set objOL = Application
Set objDoc = objOL.ActiveExplorer.ActiveInlineResponseWordEditor
Set objSel = objDoc.Windows(1).Selection
If objSel.Font.Strikethrough = False Then
objSel.Font.Strikethrough = True
Else
objSel.Font.Strikethrough = False
End If
End Sub
【讨论】:
以上是关于MS Outlook 宏删除选定的文本的主要内容,如果未能解决你的问题,请参考以下文章