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

问题描述 投票:0回答:2

过去,我对仅在MS Word中缺少“干净的直接格式化”感到沮丧。我必须承认它可能隐藏在某些我找不到的菜单中,但是由于我在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

它们都很不错,除非我尝试选择文档中的所有脚注,否则它会抱怨我越界了:D我当时正在考虑选择每个故事范围的循环,但是我可以找到的所有示例都有某种类型的find和ranges和ClearFormatting方法(如果没有)。到目前为止,我的代码是

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

但是我看到它根本不起作用,我被卡住了。另外,ClearCharacterDirectFormatting不会清除突出显示的文本。

编辑:我已经开始创建一些If子句,以弄清楚如何选择每个尾注或脚注的文本进行选择,但是我似乎无法抓住适当的对象。我注释掉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

这是到目前为止的总体思路。

ms-word formatting word-vba
2个回答
0
投票

您的代码无法正常运行,仅仅是因为您忘记了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

0
投票

[在完成每个故事中正确内容的选择之后,我设法编写了这段代码,虽然它不如其他解决方案那么干净,但是在正文,尾注或脚注中的字符和段落直接格式上都可以很好地工作。

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
© www.soinside.com 2019 - 2024. All rights reserved.