Looping through paragraphs and inserting a marker?

M

ML

I need to be able to loop through all paragraphs and bulleted lists in a
document, check the first character to see if it is a marker and if not
insert an autotext item.

What is the easiest way to handle this?
 
J

Jay Freedman

ML said:
I need to be able to loop through all paragraphs and bulleted lists
in a document, check the first character to see if it is a marker and
if not insert an autotext item.

What is the easiest way to handle this?

You don't say what your marker character is, so you'll have to modify that
line in the code. You also need to specify the correct name for the autotext
entry that should be inserted.

Sub InsertIfNoMarker()
Dim oPara As Paragraph
Dim Marker As String
Dim oRg As Range

Marker = Chr$(254) ' or whatever you have

' loop through all paragraphs
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Characters.First <> Marker Then
' create a Range object at the start
' of the paragraph
Set oRg = oPara.Range
oRg.Collapse wdCollapseStart

' insert the autotext there
With ActiveDocument.AttachedTemplate
.AutoTextEntries("entry1").Insert _
Where:=oRg, RichText:=True
End With
End If
Next oPara

' clean up
Set oRg = Nothing
End Sub

Whether or not a paragraph is part of a bulleted list has no effect -- the
bullet is not a character in the paragraph, so the first character is the
letter to the right of the bullet. If that character isn't a marker, the
macro will insert the autotext between the bullet and the (former) first
character.
 
M

ML

My marker is actually a sequence number field with a text component. Is it
possible to search for a seq field or just text?
 
J

Jay Freedman

I don't understand what you mean by "with a text component" so I'm
going to ignore that unless you tell me this version of the macro
doesn't work on your document...

Sub InsertIfNoMarker()
Dim oPara As Paragraph
Dim oRg As Range

' loop through all paragraphs
For Each oPara In ActiveDocument.Paragraphs
' create a Range object at the start
' of the paragraph and make it 2 chars long
Set oRg = oPara.Range
oRg.End = oRg.Start + 2

' assumption: if there is a field within
' the 2-char range oRg, then it is the
' marker SEQ field
If oRg.Fields.Count = 0 Then
oRg.Collapse wdCollapseStart

' insert the autotext there
With ActiveDocument.AttachedTemplate
.AutoTextEntries("entry1").Insert _
Where:=oRg, RichText:=True
End With
End If
Next oPara

' clean up
Set oRg = Nothing
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top