Hi Trond Arve,
I'm sorry, I read your original question too quickly, seeing "header" where
you wrote "heading".
The answer is yes, the approach can be adapted to prevent users from putting
the cursor in any paragraph having one of a set of styles. The event handler
can be rewritten to something like this:
Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
'quit if active doc isn't attached to this template
If ActiveDocument.AttachedTemplate <> ThisDocument Then Exit Sub
'get out of the heading if we're in it
Select Case Sel.Style
Case "Heading 1", "Heading 2"
With Sel
.Paragraphs(1).Range.Select
.Collapse wdCollapseStart
If .Paragraphs(1).Range.End = ActiveDocument.Range.End Then
.MoveUp Unit:=wdParagraph, Count:=1
Else
.MoveDown Unit:=wdParagraph, Count:=1
End If
End With
Exit Sub
Case Else
End Select
End Sub
There will be a problem if two or more paragraphs at the end of the document
have styles that are in the Case list; the second-to-last paragraph can then
be modified. Design the template to avoid that situation.
I'm not sure how long you can make the list of styles without having a
noticeable impact on Word's performance.
If you object to the flash that may appear as the macro selects the full
paragraph and then collapses the selection, write back and I'll show you how
to use a Range object instead.

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
> Hi Jay,
>
[quoted text clipped - 32 lines]
>>>
>>> Regards, Trond Arve
Trond Arve Wasskog - 23 Nov 2004 17:49 GMT
Hi Jay,
Excellent proposal. Thank you very much. This is almost what we're looking
for. However, there are a couple of problems:
1) Users can still add new Headings.
-> This may be acceptable, the most important thing is to prevent users from
changing/deleting the headings in the original template
2) When I select more than just a Heading (for example the heading and the
body text) in the document, a script error is displayed:
-> "Run time error '91': Object variable or With block variable not set"
-> I guess this is because more than one style is selected simultaneously(?)
Oh, and I changed the comparison part slightly to cover all headings:
Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
'quit if active doc isn't attached to this template
If ActiveDocument.AttachedTemplate <> ThisDocument Then Exit Sub
'get out of the heading if we're in it
If 0 < InStr(Sel.Style, "Heading ") Then
With Sel
.Paragraphs(1).Range.Select
.Collapse wdCollapseStart
If .Paragraphs(1).Range.End = ActiveDocument.Range.End Then
.MoveUp Unit:=wdParagraph, Count:=1
Else
.MoveDown Unit:=wdParagraph, Count:=1
End If
End With
Exit Sub
End If
End Sub
Regards,
Trond Arve
> There will be a problem if two or more paragraphs at the end of the document
> have styles that are in the Case list; the second-to-last paragraph can then
[quoted text clipped - 3 lines]
> paragraph and then collapses the selection, write back and I'll show you how
> to use a Range object instead.
Jay Freedman - 23 Nov 2004 18:55 GMT
Hi Trond Arve,
I think this version behaves better with respect to problem 2. I don't
immediately see anything that can be done about problem 1.
Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
'quit if active doc isn't attached to this template
If ActiveDocument.AttachedTemplate <> ThisDocument Then Exit Sub
' protect against selection of more than one style
On Error GoTo BadSel
'get out of the heading if we're in it
If 0 < InStr(Sel.Style, "Heading ") Then
MoveSel Sel
End If
Exit Sub
BadSel:
MoveSel Sel
End Sub
Private Sub MoveSel(Sel As Selection)
With Sel
.Paragraphs(1).Range.Select
.Collapse wdCollapseStart
Do While (InStr(.Style, "Heading ") > 0)
If .Paragraphs(1).Range.End = ActiveDocument.Range.End Then
.MoveUp Unit:=wdParagraph, Count:=1
Else
.MoveDown Unit:=wdParagraph, Count:=1
End If
Loop
End With
End Sub

Signature
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
> Hi Jay,
>
[quoted text clipped - 48 lines]
>> Jay Freedman
>> Microsoft Word MVP FAQ: http://word.mvps.org
Trond Arve Wasskog - 23 Nov 2004 20:53 GMT
Jay, what can I say... you're the man! I'll buy you a beer if you come to
Norway sometime... and contact med if you ever run into a Java problem ;-)
Regards,
Trond Arve
> Hi Trond Arve,
>
> I think this version behaves better with respect to problem 2. I don't
> immediately see anything that can be done about problem 1.