PDA

View Full Version : How can I return from a page header to the document in VBA ?



wdg1
01-23-2019, 10:34 AM
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

wdg1
01-23-2019, 10:36 AM
Oeps...
easy:
Set oRng = Nothing
Set oHeader = Nothing
Ward

gmayor
01-23-2019, 10:11 PM
Oeps...
easy:
Set oRng = Nothing
Set oHeader = Nothing
WardNot 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 wdStorythough the code as posted should not in any case hang in the header range.