Changing Fields in Headers

J

JodieM

Hi, I have a piece of code that works really well for changing a
field
in one header, but when I have a document that has many sections with
different headers in it, it doesnt work. I have tried many options
using the Section.headers and the storyTypes macros and even just a
simple find and replace but I just can't seem to work it out. I would
be greatful if some of the experts on this forum can help with this.

What I need to do is replace the field INCLUDEPICTURE
locationofpicture with INCLUDEPICTURE locationofnewpicture, to change
a logo in the headers of the document.


This is an edited version of the code that works for 1 header. At the
time the macro runs, the field is already selected as the
includepicture is wrapped in a macrobutton field to be able to call
the macro again to change the logo again at a future date.


Private Sub LogoChang()
Dim LogoPath As String


LogoPath = mynewlogopath


ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader


ActiveWindow.View.ShowFieldCodes = True


'add an empty field
Set myfield = ActiveDocument.Fields.Add _
(Range:=Selection.Range, Type:=wdFieldEmpty)


With ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Fields(3)
'the field with the logo is the third field in the header
.Code.Text = ""
.Code.Select
Selection.InsertAfter "Macrobutton LogoUpdate "
Selection.Collapse Direction:=wdCollapseEnd
Set myField1 = .Code.Fields.Add _
(Range:=Selection.Range, Type:=wdFieldEmpty)


With myField1
.Code.Text = ""
.Code.Select
Selection.InsertAfter "INCLUDEPICTURE " & logopath
End With
End With


ActiveWindow.View.ShowFieldCodes = False


ActiveDocument.StoryRanges(wdPrimaryHeaderStory).Fields.Update


End Sub


There is probably a lot of extra code in this macro as I've cobbled
it
together from a number of sources, but the bit that adds the field is
where it does all the work.


So my steps from here would be to either.


1. Repeat the code above so it updates the other header storys
2. Find a way to loop through all the storys.
3. Rather than deleting the field and adding the field again find a
way to do a find and replace on the field


For 1. This is not really elegant code
For 2. I've tried something along the lines of this
http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm
but without the find, and just trying to createt the new field, but I
can't work out how to be on my existing field to then start creating
the new field.
For 3. Again, I've tried the looping through and findin as per the
MVPS code above but it just wont find ^d INCLUDEPICTURE, and I'm not
sure of the exact code to get it to replace the exact path of my
logo.

I've also tried something along the lines of this structure to move
through headers http://support.microsoft.com/kb/269565 but I can't get
it to move to and select the next header before it does the update, it
just keeps updating the first header.

Any help you can give would be appreciated.
 
D

Doug Robbins - Word MVP

This probably does what you want:

Dim i As Long
With ActiveDocument
For i = 1 To .Sections.Count
With .Sections(i).Headers(wdHeaderFooterPrimary).Range.Fields(3)
.Code = "INCLUDEPICTURE " & logopath
.Update
End With
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

JodieM

Sorry about the delay in replying but I did post a reply and it
somehow did not show up.

Thank you very much for your help Doug. When I tried your code it
showed me that I had an error in my code and I was always entering the
new field in the current selection, rather than in the specific header
the code was working on.

So then I went back to one of the other 5 or so versions of this code
I had done and workd out how to do this elegantly so that it will
update only the correct field in the header regardless of what type of
header or what section the header was in.

So for the benefit of other people who may want to do a macro that
creates a field in any header of the document, then here is my very
elegant code solution.

Private Sub LogoChange()
Dim LogoPath As String
Dim p As Shape
LogoPath = MyLogoPath

ActiveWindow.View.ShowFieldCodes = True

'Loop through all the sections
For Each osection In ActiveDocument.Sections
'loop through each header in the section
For Each oheader In osection.Headers
If oheader.Exists Then
oheader.Range.Select
'only do this macro if there is a macrobutton field in
the header
For Each fld In oheader.Range.Fields
If fld.Type = wdFieldMacroButton Then
fld.Select
With fld
'Add the new field
Set myfield =
ActiveDocument.Fields.Add(Selection.Range, wdFieldEmpty)
With myfield
.Code.Text = ""
.Code.Select
Selection.InsertAfter "Macrobutton
LogoUpdate "
Selection.Collapse
Direction:=wdCollapseEnd
Set myField1 = .Code.Fields.Add _
(Range:=Selection.Range,
Type:=wdFieldEmpty)


With myField1
.Code.Text = ""
.Code.Select
Selection.InsertAfter
"INCLUDEPICTURE " & Chr(34) & Replace(LogoPath, "\", "\\") & Chr(34)
End With
End With
End With
Exit For
End If
Next fld
End If
oheader.Range.Fields.Update
Next oheader
Next osection

ActiveWindow.View.ShowFieldCodes = False

'Close the header and footer panes
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
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

End Sub
 

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