Ending outline numbered lists when linked to paragraph style

R

Rich007

Does anyone know a way to control what style is set when the user hits enter
twice in an outline numbered list?

Alternatively, (and I'm only going here in desperation) is there a way to
assign a macro to a "double-enter" on the keyboard which would then check the
current style, then check whether there are any numbers or bullets present
and if not (so the user is ending the list) apply the Body Text style?

Here's the background:

I've created my list styles, following all the advice out there on the MVP
sites, using this basic method:
'1) Create a new paragraph style:
Dim StyleName1 As String
StyleName1 = "something"
ActiveDocument.Styles.Add Name:=StyleName1, _
Type:=wdStyleTypeParagraph

'2) Define Font and Paragraph settings
With ActiveDocument.Styles(StyleName1)
.AutomaticallyUpdate = False
.BaseStyle = "" 'No style
.NextParagraphStyle = StyleName1
With .Font
.Name = "Arial"
.Size = 11
.Bold = True
End With
With .ParagraphFormat
.LeftIndent = CentimetersToPoints(-1.5)
End With
End With

'3) Created new paragraph styles for the other levels in the list
With ActiveDocument.Styles(StyleName2)
.AutomaticallyUpdate = False
.BaseStyle = StyleName1
.NextParagraphStyle = StyleName2
End With
'etc...

'4) Create a new ListTemplate (with a name)
ActiveDocument.ListTemplates.Add OutlineNumbered:=True, _
Name:="MyListTemplate"

'5) Define the settings for each ListLevel:
'DEFINE OUTLINE NUMBERED LIST SETTINGS
Set ltTemp = ActiveDocument.ListTemplates("MyListTemplate")
'Note the above works despite the Help for ListTemplates.Add
saying:
'"You cannot use this name to index the list template in the
collection."
'It only works if you use the above syntax,
'OR if you asign the name to a variant (NOT a string!), e.g.:
'Dim MyLTName As Variant
'MyLTName = "MyListTemplate"
'Set ltTemp = ActiveDocument.ListTemplates(MyLTName)

'NUMBER FORMAT
ltTemp.ListLevels(1).NumberFormat = "%1" '1, 2, 3...
ltTemp.ListLevels(2).NumberFormat = "%1%2" '1A, 1B, 1C, 2A, 2B, 2C...

'NUMBER STYLE
ltTemp.ListLevels(1).NumberStyle = wdListNumberStyleArabic '1,
2, 3...
ltTemp.ListLevels(2).NumberStyle = wdListNumberStyleUppercaseLetter 'A,
B...

'RESET ON HIGHER
For j = 1 To 2
ltTemp.ListLevels(j).ResetOnHigher = j - 1
Next j

'etc... for all the other settings

'6) Link each ListLevel to the correct paragraph style:
'LINKED STYLE
ltTemp.ListLevels(1).LinkedStyle = StyleName1
ltTemp.ListLevels(2).LinkedStyle = StyleName2

This all works very well with one major issue. If you use the automatic
numbered lists in Word, when you hit enter twice (see note below), the list
ends and the resulting two new paragraphs have the Normal style.

But, assuming you set up the list styles to be followed by themselves:
ActiveDocument.Styles(StyleName1).NextParagraphStyle = StyleName1
when you hit enter twice the style stays as my paragraph style, but the
numbering (or bullets) disappear on the two new lines! The user now has
lines of text that look like Body Text, but are still actually the StyleName1
style minus the numbers.

So, does anyone know a way to control what style is set when the user ends
an outline numbered list?

Alternatively, (and I'm only going here in desperation) is there a way to
assign a macro to a "double-enter" on the keyboard which would then check the
current style, then check whether there are any numbers or bullets present,
and if not (...then the user is ending the list...) apply the Body Text style?

(Note: technically, it isn't "enter twice", it's when the user hits enter
when there is no other text in the numbered paragraph - that should really be
the trigger for any macro that sets the style for the subsequent paragraph).

Many thanks (especially for reading all the way to here!).

Cheers
Rich
 
C

Cindy M.

Hi Rich007,
Does anyone know a way to control what style is set when the user hits enter
twice in an outline numbered list?

Which version of Word is involved? What Word does, and what can be set, very
definitely depends on that...

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
R

Rich007

Hi Cindy,
I'm using 2003.

I have reluctantly found a work around in the form of intercepting the
Return and Backspace keys, but this is very much NOT my preferred solution,
since I am very nervous about rolling this out to my entire company knowing
every time they hit Enter or Backspace my code will run!

Please let me know if there is a way to control the way Word2003 handles the
end of a list....

In the mean time, I'll share here the code for handling the end of lists...
(enjoy)

Sub AssignReturnKey()
'Run during design time to assign a macro to intercept the Return key
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyReturn), KeyCategory:= _
wdKeyCategoryMacro, Command:="OnReturn"
End Sub

Sub UnAssignReturnKey()
'Clears any assignment to the Return key
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyReturn)).Clear
End Sub

Sub OnReturn()
'Checks the current paragraph to see if it one of the list styles.
On Error GoTo ErrorHandler
Dim CurrentStyle As String
CurrentStyle = Selection.Paragraphs.Style
If Left(CurrentStyle, 9) = "MyBullets" Then
'e.g. MyBulletsL1, MyBulletsL2, etc.
HandleLists ("Body Text")
ElseIf Left(CurrentStyle, 10) = "MyNumbered" Then
'e.g. MyNumberedL1, MyNumberedL2, etc.
HandleLists ("Body Text")
'ElseIf... etc. for each list style...

ElseIf CurrentStyle = "Normal" Then 'To handle the built-in bullets or
numbers.
HandleLists ("Normal")
Else
Selection.TypeParagraph
End If
Exit Sub
ErrorHandler:
Selection.TypeParagraph
'Just in case it goes wrong, do a new paragraph so the key isn't rendered
useless.
End Sub

Sub HandleLists(NewStyle As String)
'If the current paragraph is empty, its style is changed to NewStyle
'If the current paragraph is not empty, a new paragraph is created.
If Selection.Start = Selection.End Then
'The selection does not contain 1 or more characters
Dim MyRange As Range
Set MyRange = Selection.Paragraphs(1).Range
If MyRange.Characters.Count = 1 Then
'The paragraph is empty (only contains the paragraph mark)
Selection.Paragraphs.Style = NewStyle
If NewStyle = "Normal" Then Selection.TypeParagraph
'To handle the built-in bullets or numbers.
Else 'The paragraph is not empty
Selection.TypeParagraph
End If
Else 'The selection contains 1 or more
characters
Selection.TypeParagraph
End If
End Sub

Sub AssignBackspaceKey()
'Run during design time to assign a macro to intercept the Backspace key
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyBackspace), KeyCategory:= _
wdKeyCategoryMacro, Command:="OnBackSpace"
End Sub

Sub UnAssignBackspaceKey()
'Clears any assignment to the Backspace key
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyBackspace)).Clear
End Sub

Sub OnBackSpace()
' When Backspace is pressed, checks to see if there the user has
' deleted a bullet or number in a list.
On Error GoTo ErrorHandler
Dim CurrentStyle As String
CurrentStyle = Selection.Paragraphs.Style
If Selection.Start = Selection.End Then
'The selection does not contain 1 or more characters
If Selection.Start = Selection.Paragraphs(1).Range.Start Then
'At start of paragraph
If Left(CurrentStyle, 9) = "MyBullets" Then
'e.g. MyBulletsL1, MyBulletsL2, etc.
Selection.Paragraphs.Style = "Body Text" 'Set to Body Text
ElseIf Left(CurrentStyle, 10) = "MyNumbered" Then
'e.g. MyNumberedL1, MyNumberedL2, etc.
Selection.Paragraphs.Style = "Body Text"
'ElseIf... etc. for each list style...

ElseIf CurrentStyle = "Normal" Then
'To handle the built-in bullets or numbers.
Dim MyListType
MyListType =
Selection.Range.Paragraphs(1).Range.ListFormat.ListType
If MyListType > 0 Then 'Is list, so reset format to
Normal
Selection.Paragraphs.Style = "Normal"
ElseIf MyListType = 0 Then 'No list, so do backspace
Selection.TypeBackspace
End If
Else
'If not one of the list styles (or Normal), perform backspace
Selection.TypeBackspace
End If
Else
'If not at the start of a paragraph, perform backspace
Selection.TypeBackspace
End If
Else
'If the selection does contain 1 or more characters, perform backspace
Selection.TypeBackspace
End If
Exit Sub
ErrorHandler:
'Just in case it goes wrong, do a backspace so the key isn't rendered
useless.
Selection.TypeBackspace
End Sub
 

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