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.
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.