vba MS Outlook 宏删除选定的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1763240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Outlook macro to strikeout selected text
提问by Andy
The task is to apply strikeoutto current font in selected text area. The difficulty is that Outlook doesn't support recording macros on the fly - it wants code to be written by hand.
任务是将删除线应用于所选文本区域中的当前字体。困难在于 Outlook 不支持动态录制宏——它希望代码手动编写。
For example, the following simple code:
例如下面的简单代码:
Selection.Font.Strikethrough = True
works for Word, but gives an error for Outlook:
适用于 Word,但在 Outlook 中出现错误:
Run-time error '424':
Object required
回答by Todd Main
This assumes that you also have Word installed on your box. If so, you can access most of the Word OM from the Outlook VBE without referencing Word by using the ActiveInspector.WordEditorobject.
这假设您的机器上还安装了 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
回答by Fionnuala
Here are a few notes on messing around with the open message, there are no checks, it just assumes that you have an open mail item. If you would like to say a little more about what you want to do, and in what version, I may be able to help a little more.
这里有一些关于处理打开邮件的注意事项,没有检查,它只是假设你有一个打开的邮件项目。如果你想多说一点你想做什么,在什么版本中,我可能可以提供更多帮助。
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
回答by AMissico
You need to access the Inspector's HTMLEditor or WordEditor. Check the help file for sample code. If you are using WordEditor then you can record macro in Word and incorporate the resultant code into the Outlook macro by using the WordEditor.
您需要访问 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
回答by dmkerr
Jumping off from Todd Main's excellent example above.
I slightly modified the code to work in the inline reply pane as we couldn't find a simple way to add strikethrough to the QAT or ribbon.
I also added an if block to toggle the strikethrough if it was already set.
从上面 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