PDA

View Full Version : Solved: Stop users editing headers and footers



JQ1
11-28-2005, 02:41 PM
I have created a Word template using a user form which inserts text in the header but I want to stop users editing the text in the Word document. Any help would be very much appreciated, I've been struggling with this for 3 days!

fumei
11-28-2005, 07:20 PM
Bottom line? If you have stubborn, or clever users, you can not stop them. Word IS a word processor and is designed to edit text. You can slow 'em down, but if they are determined.....

You can remove Header and Footer from the View menu. Right click the menu bar and choose Customize. Select View, and then drag off the Header and Footer item. Of course that means you have to disable Customize, so they can't put it back.

Why are you inserting text there anyway? By that I don't why IS there text there, but why is there user input to get it there? Obviously your userform gets information from the user, and then puts that in the header. That is fine, but WHY do you need to stop them from editing their OWN input?

JQ1
11-29-2005, 12:22 AM
Thanks for the input, it's a company standard document and the user input is limited to selecting a location from a drop down the rest of the text is automated using VBA, for some reason users insist on changing formatting etc. and the company insist on a standard format! It was originally developed as a user form but protecting it disables mail merge, bullet points etc. I thought there may be some way of selecting the main document immediately if they move into the header?

Marcster
11-29-2005, 02:24 AM
Hi JQ,

Put this is a normal module:


Sub ViewFooter
MsgBox "Disabled." 'You cannot display footer in page layout view.
End Sub

Sub ViewHeader
MsgBox "Disabled." 'You cannot display header in page layout view.
End Sub


HTH,

Marcster.

Marcster
11-29-2005, 02:39 AM
Just tested the above and it doesn't stop someone double-clicking the header to change the contents. :doh: Only over rides the menu commands.

I'll think of something else and get back to you... :thinking:

Marcster.

This will over ride Alt+Shift+A:

Sub ShowAllHeadings()
MsgBox "ShowAllHeadings"
End Sub

Marcster
11-29-2005, 03:38 AM
I've come up with this:


Option Explicit

Private WithEvents objWord As Word.Application

Private Sub Document_Open()
'Set variable objWord to active document in order for the events to be captured.
Set objWord = ThisDocument.Application
End Sub

Private Sub objWord_WindowSelectionChange(ByVal Sel As Selection)
'Do not allow user to be in the header or footer of document.
'Using "ActiveWindow.ActivePane.View.SeekView" to find out where the user is in the document.

Select Case ActiveWindow.ActivePane.View.SeekView
Case wdSeekCurrentPageFooter
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekCurrentPageHeader
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekCurrentPageHeader
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekEvenPagesFooter
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekEvenPagesHeader
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekFirstPageFooter
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekPrimaryFooter
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekFirstPageHeader
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case wdSeekPrimaryHeader
Call displayDisabledMsg
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Case Else
Exit Sub
End Select
End Sub

Sub displayDisabledMsg()
MsgBox "Headers and footers are disabled in this document.", vbCritical, "Action Cancelled"
End Sub


You need to paste the above in the ThisDocument module.

Tested on Word 2000.

HTH,

Marcster.

JQ1
11-29-2005, 05:59 AM
Thank you soooo much, 3 days? I think I'd have been 3 years coming up with that one!

Marcster
11-29-2005, 06:24 AM
Thanks,
Glad to help :yes.

Marcster.

fumei
11-29-2005, 08:31 AM
That certainly does NOT stop anyone from directly accessing headers or footers by VBA. All it does is prevent going into the window. I would also like to point out that it prevents everyone from doing so.

However, this is what I stated. You can slow them down, but you can't stop them. Well, you can...but it takes a bit more code than that.

For example:
With ActiveDocument.Sections(1).Headers(1).Range
With .Font
.Size = "40"
.Bold = True
End With
.Text = "Na-na-na-na"
End With
would make the Primary header of Section 1 40 point, bold with the text "Na-na-na-na". The Marcster's Selection View code would not even run.

I would also like to point out that if there IS a standard format then styles should be used. Otherwise "standard format" is a meaningless term. In which case, if styles are being used (and they should), then you CAN enforce format.

Say you have a corporate style for headers - CorpHeader. If you put the following into the FileSave routine it will ALWAYS make sure the headers have the correct style. The users can make all the format changes they want. Go ahead. But if they SAVE the file....the headers will be changed into the "standard format".

The power of styles.
Dim oFF As Word.HeaderFooter
Dim oSection As Word.Section
For Each oSection In ActiveDocument.Sections()
For Each oFF In oSection.Headers
oFF.Range.Style = "ClearFormatting"
oFF.Range.Style = "CorpHeader"
Next
Next
Note that Clearformatting is not available in Word 97 or (I think) 2000. It is handy to ensure partial formatting is cleared. Otherwise you would have to actually select the header. This way there is no selection made, it just uses the Range.

Finally (almost) I must point out that this is a good example of finding out real requirements. If the requirements are, in fact, to ensure the document is maintained with a standard format....then deal with it at that level. At that level it is irrelevant whether the user changes anything, or not. You can make it revert to the standard format. In other words, preventing access to the header is NOT the issue - the format of the header is the issue, and can be dealt with.

Finally (really), again, would people please use the underscore character in the VBA code window? I would really appreciate it. I am going to continue to mention this. I know I am being a PITA, but there you go. It is simple thing to do, and it makes it easier for the rest of us.

The long comment in Marcster's code window should be something like:
'Using "ActiveWindow.ActivePane.View.SeekView" _
' to find out where the user is in the document.
This shortens the line and allows the text to be displayed without scrolling left/right.

Thanks.