![]() |
#1
|
|||
|
|||
![]()
Hi all.
The code below will just not move on to the next paragraph no matter what I do. It just loops over and over on the same paragraph. What am I missing? Code:
Private Sub secHeading() Dim para As Paragraph Dim firstword As String For Each para In ActiveDocument.Range.Paragraphs If InStr(1, para.Range.Words(1), "RCW") <> 0 Then With para.Range .Collapse (wdCollapseEnd) .Move Unit:=wdCharacter, Count:=-1 .Delete .InsertAfter (" -- ") .Style = "Heading 1" End With End If Next End Sub |
#2
|
||||
|
||||
![]()
Whenever you are iterating through objects it is not good to delete any objects that are in the remainder of that collection.
You can fix it by moving backwards so that any deletions don't matter because the next loop only hits paragraphs you haven't changed yet. Code:
Private Sub secHeading() Dim para As Paragraph, iPar As Long For iPar = ActiveDocument.Paragraphs.Count - 1 To 1 Step -1 Set para = ActiveDocument.Paragraphs(iPar) If Trim(UCase(para.Range.Words(1))) = "RCW" Then With para.Range .Collapse (wdCollapseEnd) .Move Unit:=wdCharacter, Count:=-1 .Delete .InsertAfter (" -- ") .Style = "Heading 1" End With End If Next End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Loop to Insert Comments Moving Slowly | AntCTAZ | Word VBA | 1 | 11-02-2022 07:17 PM |
Inserting text from a Userform into a Field in a paragraph in a paragraph in a word document | storemaz | Word VBA | 1 | 03-13-2020 08:11 AM |
![]() |
Joey Cheung | Word Tables | 1 | 08-12-2014 05:19 PM |
![]() |
unittwentyfive | Excel | 1 | 10-25-2013 12:56 PM |
![]() |
FraserKitchell | Excel | 4 | 02-26-2010 10:38 AM |