내 문서에서 모든 빈 단락을 삭제하는 매크로를 작성했지만 이상한 동작이 나타납니다. 빈 끝에 단락이 여러 개있는 경우 문서의 경우 약 절반이 삭제됩니다. 반복적으로 매크로를 실행하면 단락이 하나만 남을 때까지 빈 단락이 제거됩니다. 경계 조건이 있어도 마지막 단락을 삭제하는 코드 줄이 필요하지만 끝에있는 빈 단락의 절반 만 삭제되는 이유는 여전히 이해할 수 없습니다. 왜 이런 일이 일어나고 어떻게 해결할 수 있는지 설명 할 수 있습니까? 여담, 나는 (^ 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
대신 아래에서 위로 위에서 아래로가는 때문이다 - 또는 코드가있는 경우 단락을 증가시키지 필요 occu 삭제 rs. 상단에 10 개의 빈 단락을 상상해보십시오. 먼저 삭제되어 # 2가 # 1이됩니다. 하지만 포인터에 # 2가 표시됩니다. 문제가 보이십니까? –
나는 num_of_deleted_paras 변수를 사용하여 증가하지 않을 필요성을 이미 설명했다. 또한 문서 중간에 빈 단락 문자열이 있으면 문제가없는 것 같습니다. 특히 문제를 제기하는 것이 마지막에 있으며 일부는 실제로 삭제됩니다! – user6267463
페이지 상단에서 빈 단락을 삭제하는 것과 비슷한 코드를 살펴볼 수 있습니다. 'Else Exit For'를 제거하면됩니다. 다른 항목이 정상적으로 작동한다고 가정하면 (공백 페이지 등) : https://stackoverflow.com/questions/42659628/how-to를 참조하십시오. -remove-top-blank-lines-in-vba-office-word –