2017-11-15 8 views
-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

대신 아래에서 위로 위에서 아래로가는 때문이다 - 또는 코드가있는 경우 단락을 증가시키지 필요 occu 삭제 rs. 상단에 10 개의 빈 단락을 상상해보십시오. 먼저 삭제되어 # 2가 # 1이됩니다. 하지만 포인터에 # 2가 표시됩니다. 문제가 보이십니까? –

+0

나는 num_of_deleted_paras 변수를 사용하여 증가하지 않을 필요성을 이미 설명했다. 또한 문서 중간에 빈 단락 문자열이 있으면 문제가없는 것 같습니다. 특히 문제를 제기하는 것이 마지막에 있으며 일부는 실제로 삭제됩니다! – user6267463

+0

페이지 상단에서 빈 단락을 삭제하는 것과 비슷한 코드를 살펴볼 수 있습니다. 'Else Exit For'를 제거하면됩니다. 다른 항목이 정상적으로 작동한다고 가정하면 (공백 페이지 등) : 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