![](/img/trans.png)
[英]How to merge a remote MS Word document with the active document in a VBA macro?
[英]Remove direct formatting to MS Word document with VBA macro
过去,我一直对 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
所有这些都运行良好,除非我尝试选择文档中的所有脚注,否则它会抱怨我越界了:DI 正在考虑选择每个故事范围的循环,但我能找到的所有示例都有某种发现和范围和 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
这是目前的总体思路。
您的代码无法正常工作,因为您忘记了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
在为每个故事中选择正确的内容做了一些工作之后,我设法编写了这段代码,它可能不像其他解决方案那样干净,但在正文、尾注或脚注中的字符和段落直接格式方面都可以很好地工作。
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.