Finding the proper style field and the numbered paragraph value within the style

Feb 12, 2022
Reaction score
I am trying to number paragraphs is very specific way: I have two numbering styles, and I ‎would like to select a bunch of paragraphs and run a macro
that will set the proper style according to the paragraph number.

If the paragraph numbers – within those ‎two styles only – is < 10, I would like it to be style 1, and if => 10, switch to style 2.‎
[009] Paragraph 9 with style 1.‎
not numbered paragraph
(((010))) paragraph 10 with style 2.‎
(((011))) style 2, etc.

In order to do that I . I tried the following:‎

1. Selection.Style = ActiveDocument.Styles("style1")

2. Set stylefield = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldStyleRef, Text:="style1")

3. if stylefield.Code.ListFormat.ListValue >9 then
4. Selection.Style = ActiveDocument.Styles("style2")
5. endif

The problem is that when I run line 2 I get garbage text appearing in the document. I do get the right paragraph number value to compare, but the document text becomes garbage.

I understand that this may be due to the attempt to "add" something, while in reality I ma trying to find the right field for style1 so I can get the proper listvalue for where the cursor is, and decide which style to assign to it.

How can I get the style field relating to the paragraph fast, without going through the whole list every time (the names may differ between runs) and without having the 'add' method add garbage to my text?


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