Results 1 to 15 of 15
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Prevent or protect Header (MS Word 2000)

    I would like to be able to protect/prevent the header from beeing opened. This I have asked many people and got a VBA code from someone, but it did not work in all cases.

    Using protection on section 1 for instance is not acceptable. Then some functions in Word is disabled. There should be possible to detect the event when a header is accessed and then e.g. starting a dialog box in which one shall type the info to be placed in the header.

    The code I got, but which does not work properly is as follows:

    The following code, pasted into the This Document module of a Word 2000 Template, will keep users out of the header and footer of documents based on that template:

    Option Explicit
    'reserve memory for an application variable
    Private WithEvents wdApp As Word.Application

    Private Sub Document_Close()
    'release the memory for the application variable
    Set wdApp = Nothing
    End Sub

    Private Sub Document_New()
    'assign Word to the application variable
    If wdApp Is Nothing Then
    Set wdApp = ThisDocument.Application
    End If
    End Sub

    Private Sub Document_Open()
    'assign Word to the application variable
    If wdApp Is Nothing Then
    Set wdApp = ThisDocument.Application
    End If
    End Sub

    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 header/footer if we're in it
    If Sel.StoryType = wdEvenPagesFooterStory Or _
    Sel.StoryType = wdEvenPagesHeaderStory Or _
    Sel.StoryType = wdFirstPageFooterStory Or _
    Sel.StoryType = wdFirstPageHeaderStory Or _
    Sel.StoryType = wdPrimaryFooterStory Or _
    Sel.StoryType = wdPrimaryHeaderStory Then
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    Exit Sub
    End If
    End Sub


    Hope this is understandable and that somebody can give me some advise.

    Berst regards
    Bj
    Attached Images Attached Images

  2. #2
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Prevent or protect Header (MS Word 2000)

    In what way(s) does the code not work?

    Is the template containing the code open for editing? (or is it a document based on the template?)

    What are you trying to do that doesn't happen?

    What happens that you don't want to have happen?

    Any error messages?
    Charles Kyle Kenyon
    Madison, Wisconsin

  3. #3
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi,
    Thanks for a quick answer.

    The VBA code that I inserted in my mail is placed in a template (e.g. Note-en.dot file) in the module "This Document".

    The template is used to generate a new file based on the template(standard way of generating a new file in Word). The new Document1 or after saving it as e.g. myfile.doc should then prevent users from accessing the header.
    Sometimes it works , but mostly the header will be opened. No messages. After that the header is allways opened when double-clicking in the header area or opened via view/Header/Footer.

    Best regards
    Bj

  4. #4
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    <P ID="edit"><FONT SIZE=-1>Edited by gwhitfield on 20-Jul-01 20:51.</FONT></P>Markup tags added

    Hi Charles,

    The code I pasted into my first message I now have found out that I got it from the following site, an MVP site by Bill Coan:

    <A target="_blank" HREF=http://www.mvps.org/word/FAQs/Customization/ProtectWord2000Header.htm>http://www.mvps.org/word/FAQs/Customizatio...d2000Header.htm</A>

    If this code is inserted as said in the article it will work. But, I have to show a Dialog Box when the template is started and therefore I put a code(frmNotatEn.Show) to start the Dialog Box in the rutine "Private Sub Document_New()". By doing so the protection does not function any more.

    I continue looking for an other way of having the Dialog Box open when I generate a new document based on this template.

    (My template is such designed that the dialog box can be retreaved after the new .doc document is saved. The Dialog Box then contains the text I typed in the text boxes the first time. This gives the user a possibility to change any mistakes or simly using the document in a simular situation)

    If someone could test out the code and find a way of getting the dialog box appear, I would appreciate it very much.

    Thanks

    Best regards
    Bj

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi Bj

  6. #6
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi Gary,

    Thanks for your sugestions. I will try it and think I might use it.

    To my problem with the template:

    The problem I only have on my PC at work. Siiting at home, like I do now, there seem to be no problem. But if I change some code in the template when I am using it creating a document it very often turns out that it will not work.

    I'm not a programmer in VBA, only trying to adjust what others have done. So e.g. I'm not sure what wdApp contains or whether it ony is a variable without any properties. I will contact some people tpmorrow and try to find out.

    I have attached the template file here. You will also see that it contains other code that has the purpose of beeing able to retreave what was typed in the different text boxes in the dialog box. Try it out and see if it is doing any harm to the protection of the header.

    I hope I will be able to find out what causes the problem at work.

    Best regards

    Bj

  7. #7
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Sorry the file is too big. I will amend it and then attach it later.

    regards
    Bj

  8. #8
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi Gary,

    Thanks again for your comments and suggestion. I have tried it out and it works. regarding my problem it seems that I had missed out the procedure for closing the document. It looks as the wdApp must be set to nothing when the document closes.

    Any way now it works at least on the ones I have tried it out on. It remains to update some other templates in order to get them updated with this functionality.

    Regarding my template: It is more than 100kb and I have therefore ziped it and tried to attach it here but I was not allowed. Can't understand why because the ziped file is only 53 kb. If you would like to see the template I may need to mail it directly to you. Can I find your mail address here on the lounge?

    Please feel free to comment.

    Best regards
    Bj

  9. #9
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi again,

    I'm not sure why your zip file won't attach here, but in any case I'd be interested in seeing it - you can get my private e-mail address by clicking on my name in this post - that will take you to the personal details page.

    BTW one of the lines of code in my last post can be improved as follows:

    If LCase(Right(ActiveDocument.FullName, 4)) <> ".dot" Then

    - this provides for those occasions when Windows, for reasons known only to itself, changes the file extension from ".dot" to ".Dot".

    Gary

  10. #10
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Thanks for the code. It works.
    (Whats BTW?)

    By the way i discovered that the following feature is available in VBA, by looking in help and searching for Document and under New Events:

    WindowBeforeDoubleClick Event


    Occurs when the editing area of a document window is double-clicked, before the default double-click action.

    Syntax:
    Private Sub object_WindowBeforeDoubleClick(ByVal Sel As Selection, ByVal Cancel As Boolean)

    object: An object of type Application declared with events in a class module. For more information about using events with the Application object, see Using Events with the Application Object.

    I tried it but it did not work at once. may be someone has tried this before?

    Best regards
    Bj

  11. #11
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hello Bj

  12. #12
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Thanks a lot Gary. Then I will not try the WindowBeforeDoubleClick Event. As you say; how do one know where the cursor is?

    I also discovered that the code from Bill Coan at http://www.mvps.org/word/FAQs/Customizatio...d2000Header.htm actually has changed. The procedure Document_Close has been omitted. May be I understand why, but not completely. Using it caused the wdApp to be Nothing and then the header was accessible again. Think I take it away and try my templates without for a while before I publish them.

    Now back to work again, have a lot of other things to do!! "Templates" is only a hobby.


    Best regards
    Bj

  13. #13
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi again,

    One other quick comment and it's off to do something that's also important: sleeping!

    In your template, the declaration of the Application object variable, and the event procedure, are in the ThisDocument module. They should not be - see the VBA Help for "Using Events with the Application Object" - these should be in a separate class module - the help topic with provide more detail.

    Gary

  14. #14
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi again Gary!

    Thanks a lot. I will look into the matter and do necessary amendments.
    Here time is 11:50 and we still have some hours to go. But for you .... ... sleep well and be fully rested tomorrow!!

    Best regards
    Bj

  15. #15
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: Prevent or protect Header (MS Word 2000)

    Hi Gary,

    Thanks a lot for the help to be able to prevent users to access the Header/Footer. I did put the code inot a couple of templates and after some struggling I got it working. This template also function such that a Dialog Box opens when double - clicking the Header/Footer and you can also open the Dialog Box after the document is saved and opened a later time. Since the size is more than 100kb I cannot attach it to this site. If you'r interested I can mail it to you.

    Again thanks for the help!

    Best regards
    Bj

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •