简体   繁体   中英

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. 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.

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. 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.

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. I commented out the ElseIf statement because it is complaining about using wrong methods

    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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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