[英]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.