Help needed: Search in Header

U

Udo Fröhn

Hello,
I've got a problem with a Word macro: Instead of searching in the
header of the current section, it will jump to the header of the
previous section on "Selection.Find.Execute".
I can't seem to find the error. Any help's appreciated!
Thanks, Udo

Here's the code:

Sub NewHeader()
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveUp Unit:=wdLine, Count:=2
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or
ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter.
_
LinkToPrevious
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = ")"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="()"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub
 
G

Greg Maxey

Well without going into a great detail, since you don't have anything
"physically selected" the Selection search range is interpreted as the
entire document.

i don't know exactly what your end desire is, but using ranges like
this or a variation thereof:

Sub NewHeader2()
Dim i As Long
Dim oDoc As Word.Document
Dim myRng As Word.Range
Set oDoc = ActiveDocument
Selection.InsertBreak Type:=wdSectionBreakNextPage
'Get the index number of the added section
i = oDoc.Range(0, Selection.Sections(1).Range.End).Sections.Count
With oDoc.Sections(i)
.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
End With
'Set search range to the new section header
Set myRng = oDoc.Sections(i).Headers(wdHeaderFooterPrimary).Range
With myRng.Find
.Text = ")"
.Replacement.Text = "()"
.Execute Replace:=wdReplaceAll
End With
End Sub
 
U

Udo Fröhn

Greg said:
Well without going into a great detail, since you don't have anything
"physically selected" the Selection search range is interpreted as the
entire document.
Code:
Hello Greg,
back in the office - that worked, thanks!
Udo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top