2017-11-15 24 views
-1

文書内の空の段落をすべて削除するマクロを書きましたが、奇妙な動作を示します。ドキュメントの約半分が削除されます。マクロを繰り返し実行すると、空の段落が1つだけ残るまで徐々に空の段落が削除されます。境界条件があっても最後の段落を削除するコード行が必要ですが、最後の空白段落の半分しか削除されない理由はまだ分かりません。なぜこれが起こっているのか、この動作を修正する方法を誰もが説明できますか?さておき、私は(^ P、^ 13、およびその他が、唯一vbCrは別のマイナーなパズルである、働い検索。)VBAを使用してWordで空の段落を削除する:空白の段落がすべて削除されていない

Sub Delete_Empty__Paras_2() 'This macro looks for empty paragraphs and deletes them. 
Dim original_num_of_paras_in_doc As Integer 
Dim num_of_deleted_paras As Integer 

original_num_of_paras_in_doc = ActiveDocument.Paragraphs.Count 'Count the number of paragraphs in the document to start 
num_of_deleted_paras = 0 'In the beginning, no paragraphs have been deleted 

Selection.HomeKey Unit:=wdStory 'Go to the beginning of the document. 
For current_para_number = 1 To original_num_of_paras_in_doc 'Process each paragraph in the document, one by one. 
    If current_para_number + num_of_deleted_paras > original_num_of_paras_in_doc Then 'Stop processing paragraphs when the loop has processed every paragraph. 
     Exit For 
    Else 'If the system just deleted the 3rd paragraph of the document because 
     ' it's empty, the next paragraph processed is the 3rd one again, 
     'so when we iterate the counter, we have to subtract the number of deleted paragraphs to account for this. 
     Set paraRange = ActiveDocument.Paragraphs(current_para_number - num_of_deleted_paras).Range 
     paratext = paraRange.Text 
     If paratext = vbCr Then 'Is the paragraph empty? (By the way, checking for vbCr is the only method that worked for checking for empty paras.) 
      paratext = "" 'Delete the paragraph. 
      ActiveDocument.Paragraphs(current_para_number - num_of_deleted_paras).Range.Text = paratext 
      num_of_deleted_paras = num_of_deleted_paras + 1 'Iterate the count of deleted paras. 
     End If 
    End If 
Next current_para_number 
End Sub 
+0

あなたの代わりにボトムアップからトップダウンで行っているので、それはだ - またはあなたのコードがあれば、段落をインクリメントないする必要がありますオケを削除するrs。上部に10個の空白の段落があるとします。最初は削除され、#2は#1になりました。しかし、あなたのポインタは今#2と言います...問題を見ますか? –

+0

私はすでにnum_of_deleted_pa​​ras変数を使用して、インクリメントしないようにする必要性を説明しました。また、文書の途中に空の段落の文字列がある場合、問題はないようです。それは具体的には問題を提起する最後にあり、そのうちのいくつかは実際に削除されます! – user6267463

+0

あなたは、ページのTOPで空白の段落を削除すると書いた同様のコードを見ることができます。 「Else Exit For」を削除するだけで済みます。他の項目はOK(空白のページなど)であると仮定します:https://stackoverflow.com/questions/42659628/how-toを参照してください。 -remove-top-blank-lines-in-vba-office-word –

答えて

1

このコードはすべて削除されます段落マーカーを検出に関する数多くの記事をオンラインで検索して見てきたように空白の段落...

Sub RemoveBlankParas() 
    Dim oDoc  As Word.Document 
    Dim i   As Long 
    Dim oRng  As Range 
    Dim lParas  As Long 

    Set oDoc = ActiveDocument 
    lParas = oDoc.Paragraphs.Count   ' Total paragraph count 
    Set oRng = ActiveDocument.Range 

    For i = lParas To 1 Step -1 
     oRng.Select 
     lEnd = lEnd + oRng.Paragraphs.Count       ' Keep track of how many processed 
     If Len(ActiveDocument.Paragraphs(i).Range.Text) = 1 Then 
      ActiveDocument.Paragraphs(i).Range.Delete 
     End If 
    Next i 

    Set para = Nothing 
    Set oDoc = Nothing 
    Exit Sub 
End Sub 
+0

おそらく、OPの問題は、空白だけを含む段落に関連している可能性があります。 'Text'を長さをチェックする前にLTrimすれば、あなたのコードもそれを削除します。 – Variatus

0

あなたは段落記号を置き換えることができます。

ActiveDocument.Range.Find.Execute FindText:="^p^p", ReplaceWith:="^p", Replace:=wdReplaceAll 

ActiveDocument.Range.Find.Execute "^p^p", , , , , , , , , "^p", wdReplaceAll ' might be needed more than once 
関連する問題