任务是将删除线应用于选定文本区域中的当前字体。困难在于 Outlook 不支持动态录制宏 - 它希望手动编写代码。
例如下面的简单代码:
Selection.Font.Strikethrough = True
适用于 Word,但在 Outlook 中出现错误:
Run-time error '424':
Object required
任务是将删除线应用于选定文本区域中的当前字体。困难在于 Outlook 不支持动态录制宏 - 它希望手动编写代码。
例如下面的简单代码:
Selection.Font.Strikethrough = True
适用于 Word,但在 Outlook 中出现错误:
Run-time error '424':
Object required
这假设您的盒子上还安装了 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
这里有一些关于处理打开邮件的注意事项,没有检查,它只是假设你有一个打开的邮件项目。如果你想多说一点你想做什么,在什么版本,我也许可以提供更多帮助。
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
您需要访问 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
从上面 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