任务是将strikeout应用于所选文本区域中的当前字体。困难在于Outlook不支持动态记录宏-它希望手动编写代码。
例如,以下简单代码:
Selection.Font.Strikethrough = True
适用于Word,但对于Outlook却出现错误:
Run-time error '424':
Object required
这假设您的盒子上也安装了Word。如果是这样,则可以使用ActiveInspector.WordEditor对象从Outlook VBE访问大多数Word OM,而无需引用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
这里有一些关于打开邮件的提示,没有检查,只是假设您有一个打开的邮件。如果您想多说一些您想做的事情和版本,我也许可以提供更多帮助。
您需要访问检查器的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块来切换删除线(如果已设置)。