简体   繁体   English

使用 VBA 宏删除对 MS Word 文档的直接格式设置

[英]Remove direct formatting to MS Word document with VBA macro

In the past I have been frustrated by the absence of "clean direct formatting" only in MS Word.过去,我一直对 MS Word 中没有“干净的直接格式”感到沮丧。 I must admit it may exist hidden in some menu I can't find, but since I found the methods for that purpose in VBA I decided to create three small macro that will simply remove either direct paragraph formatting, character formatting or both to the selected text.我必须承认它可能隐藏在我找不到的某些菜单中,但是由于我在 VBA 中找到了用于该目的的方法,因此我决定创建三个小宏,它们将简单地删除直接段落格式、字符格式或两者都被选中文本。

Sub Clean_Direct()
'
' Delete direct formatting to paragraph and character
'
'
    Selection.ClearCharacterDirectFormatting
    Selection.ClearParagraphDirectFormatting
End Sub
Sub Clean_Direct_character()
'
' Delete direct formatting to character
'
'
    Selection.ClearCharacterDirectFormatting
End Sub

Sub Clean_Direct_paragraph()
'
' Delete direct formatting to paragraph
'
'
    Selection.ClearParagraphDirectFormatting
End Sub

All of them works nicely, unless I try to select all footnotes in a document, where it complain that I am crossing the boundaries :DI was thinking about a loop that select each story range, but all examples I could find had some kind of find and ranges and the ClearFormatting methods where not available.所有这些都运行良好,除非我尝试选择文档中的所有脚注,否则它会抱怨我越界了:DI 正在考虑选择每个故事范围的循环,但我能找到的所有示例都有某种发现和范围和 ClearFormatting 方法(如果不可用)。 So far my code is到目前为止我的代码是

Sub Cleanup()

Dim Rng As Range
For Each Rng In ActiveDocument.StoryRanges
  With Rng
    With Selection.Range
        Selection.ClearCharacterDirectFormatting
        Selection.ClearParagraphDirectFormatting
    End With
  End With
Next

End Sub

But I see it is not working at all and I am stuck.但我发现它根本不起作用,我被卡住了。 As a side note, ClearCharacterDirectFormatting is not clearing highlighted text.作为旁注,ClearCharacterDirectFormatting 不会清除突出显示的文本。

EDIT: I have started to create some If clause to figure out how to select the text of each endnote or footnote for selection, but I seem unable to grab the proper object.编辑:我已经开始创建一些 If 子句来弄清楚如何选择每个尾注或脚注的文本进行选择,但我似乎无法抓住正确的对象。 I commented out the ElseIf statement because it is complaining about using wrong methods我注释掉了 ElseIf 语句,因为它抱怨使用了错误的方法

    Sub Cleanup()

For Each myStory In ActiveDocument.StoryRanges
    If myStory.StoryType = wdMainTextStory Then
        myStory.Select
        Selection.ClearCharacterDirectFormatting
        Selection.ClearParagraphDirectFormatting
        Selection.Collapse

'    ElseIf myStory.StoryType = wdEndnotesStory Then
'        For Each myEndnote In myStory.Endnotes
'            myEndnote.Text.Select
'            Selection.ClearCharacterDirectFormatting
'            Selection.ClearParagraphDirectFormatting
'        Next myEndnote
'    ElseIf myStory.StoryType = wdFootnotesStory Then
'        For Each myFootnote In myStory.Footnotes
'            myFootnote.FormattedText.Select
'            Selection.ClearCharacterDirectFormatting
'            Selection.ClearParagraphDirectFormatting
'        Next myFootnote
    End If
Next myStory

End Sub

This is the general idea so far.这是目前的总体思路。

Your code isn't working simply because you've forgotten to Select the range.您的代码无法正常工作,因为您忘记了Select范围。

Sub Cleanup()

Dim Rng As Range
For Each Rng In ActiveDocument.StoryRanges
  Rng.select
  With Selection.Range
    Selection.ClearCharacterDirectFormatting
    Selection.ClearParagraphDirectFormatting
  End With
Next

End Sub

After some work on selcting the right content in each story I managed to write this code that may not be as clean as other solutions but works nicely both on character and paragraph direct formatting in main text, endnotes or footnotes.在为每个故事中选择正确的内容做了一些工作之后,我设法编写了这段代码,它可能不像其他解决方案那样干净,但在正文、尾注或脚注中的字符和段落直接格式方面都可以很好地工作。

Sub Cleanup()

For Each myStory In ActiveDocument.StoryRanges
    If myStory.StoryType = wdMainTextStory Then
        myStory.Select
        Selection.ClearCharacterDirectFormatting
        Selection.ClearParagraphDirectFormatting
        Selection.Collapse

    ElseIf myStory.StoryType = wdEndnotesStory Then
        With myStory.Endnotes
            If myStory.Endnotes.Count >= 1 Then
                For Each Endnote In myStory.Endnotes
                    Endnote.Range.FormattedText.Select
                    Selection.ClearCharacterDirectFormatting
                    Selection.ClearParagraphDirectFormatting
                    Selection.Collapse
                Next
            End If
        End With
        ElseIf myStory.StoryType = wdFootnotesStory Then
        With myStory.Footnotes
            If myStory.Footnotes.Count >= 1 Then
                For Each Footnote In myStory.Footnotes
                    Footnote.Range.FormattedText.Select
                    Selection.ClearCharacterDirectFormatting
                    Selection.ClearParagraphDirectFormatting
                    Selection.Collapse
                Next
            End If
        End With
    End If
Next myStory

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM