Finding the right style field in VBA

Joined
Feb 12, 2022
Messages
2
Reaction score
0
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 assign the right style to each paragraph, based on its serial within the two styles.

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.‎

Desired output Example

[008] paragraph 8 with style 1
[009] Paragraph 9 with style 1.‎
unnumbered 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 value to compare, but the text becomes garbage.

I understand that this may be due to the attempt to "add" something, while in reality I am trying to find the right field for style1 so I can get the proper list value from it.

How can I get the field relating to the style 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?

TNX
Saltman
 

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