Checking if header in a Word document has focus or not. RRS feed

  • Question

  • Hello,

    I am working on a document which has header and footer. Now the header can be modified by double clicking. I need a VBA code which can check if some one double clicks on the header and footer.

    Thanks in advance.



    Sunday, May 4, 2014 9:26 PM


All replies

  • For that you would need to implement Word's WindowSelectionChange event. See: http://word.mvps.org/FAQs/MacrosVBA/AppClassEvents.htm

    Since there are three headers & footers per Section, you would need code like the following to test the range:

    Private Sub wdApp_WindowSelectionChange(ByVal Sel As Selection)
    Select Case Sel.StoryType
      Case wdEvenPagesFooterStory: MsgBox "Selection is in an Even Pages Footer"
      Case wdEvenPagesHeaderStory: MsgBox "Selection is in an Even Pages Header"
      Case wdFirstPageFooterStory: MsgBox "Selection is in an First Page Footer"
      Case wdFirstPageHeaderStory: MsgBox "Selection is in an First Page Header"
      Case wdPrimaryFooterStory: MsgBox "Selection is in a Primary Footer"
      Case wdPrimaryHeaderStory: MsgBox "Selection is in a Primary Header"
    End Select
    End Sub

    Do note that you do not need to select a header or footer to update it; that can be done by addressing the relevant header/footer range directly.

    Paul Edstein
    [MS MVP - Word]

    Sunday, May 4, 2014 11:05 PM
  • Hi,

    For VBA code problems, I suggest you post the question in MSDN forum:


    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Thanks for Paul's advice :)


    Melon Chen
    TechNet Community Support

    Monday, May 5, 2014 1:55 AM
  • Thanks Melon for the info. I shall post my queries there from next time :)



    Monday, May 5, 2014 12:25 PM
  • Hi Paul,

    I just want to check if the header has the focus or not (Any part of the header). If the header has the focus then it should display a message. An independent function that can be called anywhere.



    Monday, May 5, 2014 5:25 PM
  • You previously said "need a VBA code which can check if some one double clicks on the header and footer". The code I posted does exactly that; now you're referring to only whether the header (not the footer) has focus, irrespective of the user's double-clicking. That is an entirely different proposition. It would be helpful if you could state clearly what you want so people don't waste time providing solutions for what you say when what you want is different.

    For a generalised function to test whether the selection is in a header you could use code like:

    Function InHeader(Rng As Range) As Boolean
    Select Case Sel.StoryType
      Case wdEvenPagesHeaderStory: InHeader = True
      Case wdFirstPageHeaderStory: InHeader = True
      Case wdPrimaryHeaderStory: InHeader = True
      Case Else: InHeader = False
    End Select
    End Function

    Which you could call with code like:

    Sub Test()
    MsgBox InHeader(Selection.Range)
    End Sub

    Paul Edstein
    [MS MVP - Word]

    Monday, May 5, 2014 10:32 PM
  • Sorry for the confusion Paul. Let me explain the situation properly.

    I work for an organisation which has a proper header and footer for all its documents. I have a code which is used for updating the header and footer of these documents.

    But there is a flaw in the code, when someone removes a part of the header by double clicking, then selecting the text and then pressing backspace and then run the update header code, it returns a run time error '4605' (This method or property is not available because some or all the object does not refer to a table).

    So I thought to call a small code which can check if you have double clicked on a header and highlighted it now. Or maybe is there any way in which I can block the user from modifying the header and footer, as in locking them permanently and reveal them on password entry only.

    I hope I have made myself clear. Let me know if you need any other information. I would appreciate if you can please give the procedure to call also.



    Tuesday, May 6, 2014 12:44 PM
  • It seems to me the better approach would be to prevent manual updates to all or part of the header. You can do this by applying read-only editing restrictions to the document and marking whatever portions users are allowed to edit as exceptions.

    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 7, 2014 12:03 AM