Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Trond Arve Wasskog - 23 Nov 2004 09:42 GMT
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
Jay Freedman - 23 Nov 2004 14:32 GMT
Hi Trond Arve,

See http://word.mvps.org/FAQs/Customization/ProtectWord2000PlusHeader.htm.

Signature

Regards,
Jay Freedman
Microsoft Word MVP          FAQ: http://word.mvps.org

> We are generating Word2000 document templates where the document
> structure is based on headings. It is important that the user does not
[quoted text clipped - 10 lines]
>
> Regards, Trond Arve
Trond Arve Wasskog - 23 Nov 2004 14:51 GMT
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

> Hi Trond Arve,
>
[quoted text clipped - 14 lines]
> >
> > Regards, Trond Arve
Jay Freedman - 23 Nov 2004 16:31 GMT
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.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.