Consulting

Results 1 to 3 of 3

Thread: How can I return from a page header to the document in VBA ?

  1. #1
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location

    How can I return from a page header to the document in VBA ?

    How do I leave a page-header in VBA, after I copied 7 lines of text from a Word document, and copy this into the header of page 2, 3, 4,...

    First off, I did a search to find the (unique word) "ongeval", that means accident.
    Then, this line, and the 7 following lines have to be pasted into the pageheader (not on page 1).
    This is what I made:
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "Ongeval"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine
    Selection.MoveDown Unit:=wdLine, Count:=7, Extend:=wdExtend
    Selection.Copy
    Selection.EndKey Unit:=wdStory
    Selection.InsertBreak Type:=wdPageBreak
    Selection.TypeText Text:="."
    Selection.TypeParagraph
    The VBA code from my previous thread, does work here.
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.Paste
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    But now, the cursor "hangs" in the page-header.
    How do I return to the document in VBA?

    Thanks

    Ward

  2. #2
    VBAX Regular
    Joined
    Oct 2018
    Location
    Antwerp
    Posts
    41
    Location
    Oeps...
    easy:
    Set oRng = Nothing
    Set oHeader = Nothing
    Ward

  3. #3
    Quote Originally Posted by wdg1 View Post
    Oeps...
    easy:
    Set oRng = Nothing
    Set oHeader = Nothing
    Ward
    Not so easy if you use the selection object rather than ranges (as I suggested in your earlier post). When you use ranges, the cursor never enters the header so cannot hang there.

    Your posted code does not define oRng or oHeader and almost certainly the module does not include Option Explicit at the top which would force Word to display that error, so the codes don't do anything. The code you need is
    Selection.EndKey wdStory
    though the code as posted should not in any case hang in the header range.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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