0
votes

In my word-doc there are Automatically imported lists like lookin like that:

- Listitem one
- Listitem two
- Listitem three
- ...

They are only pagraphs starting with a dash '-'. So im trying to convert them into lists:

Selection.WholeStory
Selection.MoveLeft Unit:=wdCharacter, count:=1

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Aufzählungszeichen")

         With Selection.Find
             .Text = "^p- "
             .Replacement.Text = "^p "
             .Forward = True
             .Wrap = wdFindStop
             .Format = True
             .MatchCase = False
             .MatchWholeWord = False
             .MatchWildcards = False
             .MatchSoundsLike = False
             .MatchAllWordForms = False
         End With

Selection.Find.Execute Replace:=wdReplaceAll

However this results in lists that are only styled like a list but don't behave like real lists in word. How can i replace the paragraphs (starting ^p-) with real list items through a macro?

2

2 Answers

2
votes

Word actually has a built-in feature that can convert text lists into bullet lists. It's disappeared from the UI in recent years, but is still available in the list of "Commands not in the Ribbon" and in the object model. It's called AutoFormat. These can be added to the Ribbon or the QAT, or used with code.

For example select the list then

Selection.Range.AutoFormat

or, to use the UI functionality (which will show and interactive dialog allowing finer control):

Application.CommandBars.ExecuteMso("AutoFormat")

There are also options settings for controlling what AutoFormat does

Application.CommandBars.ExecuteMso("AutoFormatOptions")

It's also possible to apply a style using Find/Replace, but it's important that the style is linked to a list. Create a new style if you don't have one already, then...

Go to the Multilevel list control on the Ribbon and choose Define new list style. Assign a name, click Format, choose Numbering and define the list properties (assign a bullet symbol from Number style for this level). Click the More button and from Link level to style choose the style name that should be used.

Now, when you run the Find/Replace code in the question, using the style name (not the list style name) it should apply the list as well as the style formatting.

If you run into issues with defining the style pair, best to ask in an end-user venue such as Super User.

-2
votes

You're on the right track with applying a style. If the style applied is a member of a list numbering style, the text will behave as a list. The trick is to apply one of the styles that are a member of the list style, since you can't apply the list style directly.

To clarify this difference, here is Shauna Kelly's article about creating numbering lists: How to create numbered headings or outline numbering in Word In her article, "Headings" is the list style used to organize the sub-styles. If you created a list style using this article, you would apply Heading 1, Heading 2, etc. as a style.