2013-03-08 84 views
2

我想拥有在Word中根据我选择的文本自动填充其格式设置。即如果我将光标放在粗体和斜体上,我希望能够找到与此格式匹配的所有文本,而不必实际执行格式中选择这些格式的手动过程 - >字体窗口中的查找对话框。VBA Word:更新查找当前选择的格式

使用Word的宏录制功能有一定的帮助,我是来工作的解决方案:

Sub FindFormat() 
    Selection.Find.ClearFormatting 
    Selection.Find.Replacement.ClearFormatting 
    With Selection.Find.Font 
     .Size = Selection.Font.Size 
     .Bold = Selection.Font.Bold 
     .Italic = Selection.Font.Italic 
     .Underline = Selection.Font.Underline 
     .StrikeThrough = Selection.Font.StrikeThrough 
     .DoubleStrikeThrough = Selection.Font.DoubleStrikeThrough 
     .Hidden = Selection.Font.Hidden 
     .SmallCaps = Selection.Font.SmallCaps 
     .AllCaps = Selection.Font.AllCaps 
     .Color = Selection.Font.Color 
     .Superscript = Selection.Font.Superscript 
     .Subscript = Selection.Font.Subscript 
    End With 
End Sub 

我可以在技术上使用它,并完成。问题在于它并不那么直观,因为很难看到它所应用的格式,因此Find字段的逗号分隔列表永远不会结束,并且会使用省略号切断窗口,导致无法读取所有应用的格式:

Microsoft Word Find

因此,要切入正题,我怎么有VBA仅更改格式选项比中性不同,切剩下的,即无下划线,字体颜色:自动等。不应该改变查找格式(离开其支票牛在中性状态)?

此外,我怎样称找到对话框来打开所有这些设置而没有任何实际的查找执行(例如,我可以手动添加文本或根据需要更改任何格式)?

欣赏。

回答

1

对于您的问题的第一部分,我会使用如下所示的IF语句。

.Size = Selection.Font.Size 
If Selection.Font.Bold = True Then .Bold = Selection.Font.Bold 
If Selection.Font.Italic = True Then .Italic = Selection.Font.Italic 
If Selection.Font.Underline <> wdUnderlineNone Then .Underline = Selection.Font.Underline 
If Selection.Font.StrikeThrough = True Then .StrikeThrough = Selection.Font.StrikeThrough 
If Selection.Font.DoubleStrikeThrough = True Then .DoubleStrikeThrough = Selection.Font.DoubleStrikeThrough 
If Selection.Font.Hidden = True Then .Hidden = Selection.Font.Hidden 
If Selection.Font.SmallCaps = True Then .SmallCaps = Selection.Font.SmallCaps 
If Selection.Font.AllCaps = True Then .AllCaps = Selection.Font.AllCaps 
If Selection.Font.ColorIndex <> wdAuto Then .ColorIndex = Selection.Font.ColorIndex 
If Selection.Font.Superscript = True Then .Superscript = Selection.Font.Superscript 
If Selection.Font.Subscript = True Then .Subscript = Selection.Font.Subscript 

等等,你想跟踪任何其他值。 (请注意,我使用ColorIndex而不是Color,我使用的是Word 2010,这对我来说属于正确的属性。)

我不确定第二部分如何操作。理论上你会设置wdEditFind对话框的Find参数,但实际上似乎只是将字符串作为其值。

另一件事;如果你想循环使用可能性,使用Find可能是一条可行的路。但是,如果您尝试获取具有特定格式的所有文本的列表,则更简单的方法是右键单击所选文本,然后选择“样式”>“选择所有带相似格式的文本”。然后你可以复制并粘贴到另一个文件。可能会或可能不会与您的需求相关,但从某些情况来看,这是一个方便的技巧。