0
votes

I need to loop through all the headings in the document and need to add new heading between Heading-2 and Heading-3. All existing headings are Heading 1 and I know the text.

I need to delete Heading-5.

The original file has 30000 more paragraphs; using old method takes too much time

for example here are the headings 
     1. Heading-1 
     2. Heading-2
     3. Heading-3
     4. Heading-4
     5. Heading-5
     6. Heading-6
    
 For P = 1 To ActiveDocument.Paragraphs.Count - 1
   ptext=ActiveDocument.Paragraphs(p).text
If Left(ActiveDocument.Paragraphs(P).Style, 9) = "Heading 1" Then
            If InStr(1, ptext, "Heading-2") > 0 Then
                ActiveDocument.Paragraphs.Add _
                Range:=ActiveDocument.Paragraphs(P).Range
                Selection.MoveLeft Unit:=wdCharacter, Count:=1
                Selection.TypeText Text:="Heading New"                    
            End If         
    End If
Next
1

1 Answers

0
votes

In that case, record a macro in Word for the following steps: In the Find dialog enter the text for "Heading-3" then click "find". The selection should jump to Heading-3

This will give you the basic syntax for going to Heading 3. You can edit it to remove parameters you don't want/need (you only need the text search, really).

Following the Find you insert code that will create a new paragraph with the text you want. There are a number of ways this can be done, my preference:

Dim rng as Word.Range
Set rng = Selection.Range
rng.InsertBefore vbCr 'paragraph mark
rng.Collapse wdCollapseStart
rng.Text = "NEW STUFF"