Headers, footers and macros

J

Jorge

Hi

Do you know how can I place the insertion point in a header/footer from my
macro?

Best

Jorge
 
H

Helmut Weber

Hi Jorge,

you don't need an insertion point at all.

This one will replace the 4th character
in an ordinary footer with "vvv".

Dim r As Range
Set r = ActiveDocument.StoryRanges(wdPrimaryFooterStory)
With r
.Characters(4).Text = "vvv"
End With

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jorge

Well, I think I do need the insertion point in the header because I want to
do a search inside the header. At the moment I'm using these two instructions
at the beginning and the end of my procedure:

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
...
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
 
H

Helmut Weber

No.

Have a look at this:

Dim r As Range
Set r = ActiveDocument.StoryRanges(wdPrimaryHeaderStory)
With r.Find
..Text = "dk"
..Replacement.Text = "xx"
' clear other options in addition
..Execute Replace:=wdReplaceAll
End With

Greetings from Bavaria

Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
D

Debra Ann

I needed to replace all instances of "NP Inc" in all footers throughout a
document and came across this helpful note. However, when I try to run it,
it doesn't do anything. Below is my code. Have I done something wrong?

Dim r As Range

Set r = ActiveDocument.StoryRanges(wdPrimaryFooterStory)

With r.Find
.Text = "NP Inc."
.Replacement.Text = "NP Canada Ltd."
.Execute Replace:=wdReplaceAll
End With
 
D

Debra Ann

I need to replace text that is in the footers throughout a document and I
came across this helpful suggestion. However, when I try to run the code it
doesn't do anything. Have I done something wrong? Here is my code:

Dim r As Range

Set r = ActiveDocument.StoryRanges(wdPrimaryFooterStory)

With r.Find
.Text = "NP Inc."
.Replacement.Text = "NP Canada Ltd."
.Execute Replace:=wdReplaceAll
End With
 
G

Greg Maxey

Works here if the footer is a primaryfooter

To process each footer storytype, try:

Sub ScratchMacro()
Dim i As Long
Dim rngStory As Word.Range
i = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
Do
Select Case rngStory.StoryType
Case 8, 9, 11 'Corresponds to First, Primary and Even page footer
story
With rngStory.Find
.Text = "NP Inc."
.Replacement.Text = "NP Canada Ltd."
.Execute Replace:=wdReplaceAll
End With
Case Else
'Do Nothing
End Select
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub
 
D

Debra Ann

Greg,

This worked beautifully. I'm sorry I am just getting back to you on this
"wonderful" code. I somehow missed the notification that the quesiton was
answered and got involved in something else the last couple of weeks.

But ... thank you, thank you, thank you ;-)
 
D

Debra Ann

Oops, just realized one thing it didn't change.

On my Table of Contents, List of Tables, and List of Figures sections, I
have a first page of section footer and the rest of the section footer.
Until the engineers add information into the template, each one of these
sections only contain one page each. Therefore, the section footers for the
rest of the section that are hidden because they have not been used yet, do
not change with this code. It is only obvious once the Table of Contents
goes to two pages and the footer still contains the wrong company. Is there
any way to fix this?
 

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