VBA 清除所选段落的格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17256566/
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
VBA clear formatting from selected paragraph
提问by pavja2
Having a lot of trouble with this macro:
这个宏有很多麻烦:
ActiveDocument.CopyStylesFromTemplate NormalTemplate.FullName
With Selection
.Style = ActiveDocument.Styles("Normal")
.Range.HighlightColorIndex = 0
.Font.Shading.Texture = wdTextureNone
.Font.Name = ActiveDocument.Styles("Normal").Font.Name
.Font.Size = ActiveDocument.Styles("Normal").Font.Size
End With
It works perfectly with a line like: This textshould be formatted withoutbold or italics.
它与以下行完美配合:此文本的格式应不带粗体或斜体。
And converts it to" This text should be formatted without bold or italics.
并将其转换为” 此文本的格式应不带粗体或斜体。
However, it only works if I select up to right before the last character and run it. When I double click the line or select the entire line manually and run the macro it seems to have no effect at all.
但是,只有当我在最后一个字符之前选择 up to up 并运行它时,它才有效。当我双击该行或手动选择整行并运行宏时,它似乎根本没有效果。
Any ideas what might be causing this or if there is a different method I should use to remove bold/italicized text within a selection and convert all of the text to Normal style/font?
任何想法可能导致这种情况,或者是否有其他方法我应该使用来删除选择中的粗体/斜体文本并将所有文本转换为普通样式/字体?
Edit: This may have to do with running the macro from a keyboard shortcut as it works fine when run manually via the macro menu.
编辑:这可能与从键盘快捷键运行宏有关,因为它在通过宏菜单手动运行时工作正常。
采纳答案by GregHNZ
If you want to clear all formatting, I'd suggest this:
如果您想清除所有格式,我建议这样做:
With Selection
.ClearFormatting
End With
This resets the paragraph style to Normal and resets all fonts and attributes (that I've tried to set). Note that it resets the paragraph style for the paragraph that includes the selection (so if you've only selected a single word in the paragraph, it still resets the style for the paragraph).
这会将段落样式重置为 Normal 并重置所有字体和属性(我已尝试设置)。请注意,它会重置包含所选内容的段落的段落样式(因此,如果您只选择了段落中的一个单词,它仍会重置该段落的样式)。
ref: MSDN: Selection.ClearFormatting
参考:MSDN:Selection.ClearFormatting
I didn't see any difference when I added ActiveDocument.CopyStylesFromTemplate NormalTemplate.FullName
but I expect this would depend on the complexities of your documents.
我添加时没有发现任何区别, ActiveDocument.CopyStylesFromTemplate NormalTemplate.FullName
但我希望这将取决于您的文档的复杂性。
回答by pavja2
Turns out that the problem was poor documentation by a previous developer of the template I was using - I was merely editing the wrong macro and thus got partially correct results from a flawed macro despite my fixes not showing anything.
事实证明,问题是我使用的模板的前任开发人员的文档很差 - 我只是编辑了错误的宏,因此尽管我的修复没有显示任何内容,但从有缺陷的宏中得到了部分正确的结果。