How can I prevent users from editing the headings of a document in Word 2000?

T

Trond Arve Wasskog

We are generating Word2000 document templates where the document
structure is based on headings. It is important that the user does not
change the headings while editing the document template (note that
this is not Word Templates, just plain Word documents with the
specified heading structure). More specifically, we want to prevent
the user from:
o Changing the existing headings
o Removing existing headings
o Adding new headings

Anybody done something like this? I guess we have to apply some VBA
scripting. Any help and tips appreciated.

Regards, Trond Arve
 
T

Trond Arve Wasskog

Hi Jay,

thanks for your response. However, the document below refers to preventing
the header/footer. We need to prevent the headings from being changed.
Unfortunately I'm no Word or VBA expert; do you think a similar approach is
possible to prevent headings from being changed? That is, using the
WindowSelectionChange event to detect heading changes?

Regards,
Trond Arve
 
J

Jay Freedman

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

Trond Arve Wasskog

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
 
J

Jay Freedman

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
 
T

Trond Arve Wasskog

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
 

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