PDA

View Full Version : Formatting bullets in Word



noclass1980
10-17-2017, 09:13 AM
I have a Word document with a large number of bulletted lists with Normal text between the lists. I have code that formats all bullets in the same way (line spacing etc,) . What I want to do is look at the last character of each bullet and determine if it is a ";" but also have a "." at the end of the last bullet in a list. The intent is to have all the bullets formatted in the same way, that is, semi-colons at the end of each bullet and the last bullet has a full stop instead of a semi-colon. e.g. the red text below is the action I want the macro to do for each bullet.



Sample Text 1 - this needs a ";"
Sample Text 2; - do nothing
Sample Text 3 - this needs a "."


Then some additional paragraphs.



Sample Text 4; - do nothing
Sample Text 5 - this needs a ";"
Sample Text 6. - do nothing


Ive tried a few things but with no success. Any suggestion? Thanks.

macropod
10-17-2017, 04:04 PM
You really should define a suitable Style for your Bullet paragraphs instead of overriding Word's Style definitions for the underlying paragraphs. You then wouldn't need a macro for the formatting; you'd do it all via the Style definition. That is how Word is supposed to be used.

Control of the last character in each list could then be done via a Find/Replace macro that looks for instances of that Style. For example:

Sub Demo()
Application.ScreenUpdating = False
Const StrSty As String = "Bullet"
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^p"
.Style = StrSty
.Replacement.Text = ""
.Forward = True
.Format = True
.Wrap = wdFindStop
.MatchWildcards = False
.Execute
End With
Do While .Find.Found
If .End = ActiveDocument.Range.End Then
If .Characters.First.Previous Like "[,.;:]" Then
.Characters.First.Previous = "."
Else
.InsertBefore "."
End If
Exit Do
End If
If .Paragraphs.First.Next.Style = StrSty Then
If .Characters.First.Previous Like "[,.;:]" Then
.Characters.First.Previous = ";"
Else
.InsertBefore ";"
End If
Else
If .Characters.First.Previous Like "[,.;:]" Then
.Characters.First.Previous = "."
Else
.InsertBefore "."
End If
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
where 'Bullet' is your Bullet Style's name.

noclass1980
10-17-2017, 11:43 PM
Thanks, I was hoping for a more generic approach so others can use it with the default bullets rather than having to use a bespoke style.

Is it possible to look at a paragraph to determine if its "WdListType.wdListBullet" and the next paragraph to determine if its also a "WdListType.wdListBullet". If this is the case use RIGHT(bullettext,1) to determine if it is a ";" and, if not, use bullettext= bullettext & ";" If the next paragraph is not a "WdListType.wdListBullet", use the same approach to add a "." if the last character is not a "." Hope that makes sense!

Thanks

macropod
10-18-2017, 12:05 AM
I was hoping for a more generic approach so others can use it with the default bullets rather than having to use a bespoke style.
As I said before, you should learn to use Styles properly; they are the foundation of any properly-structured Word document. Without that, the best you can do is look for the 'List Paragraph' Style (which is what you get if you apply bullets without applying a predefined Style) and add whatever additional code is needed for the macro I supplied to differentiate between such paragraphs that use bullets and those that don't. I'm not inclined to write code to compensate for poor document design, thereby encouraging its continuance. Your refusal to use Styles properly already has you unnecessarily and inefficiently formatting bullets via a macro...