PDA

View Full Version : Changing header without removing embedded picture?



brettdoyle
07-16-2015, 06:24 AM
I have some code that is similar to below:


With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
If .Range.text <> vbCr Then
.Range.text = Replace(.Range.text,"Old text","New text")
End If
End With


It works and replaces the text that I want... but the problem is I lose everything besides the text in the header. For instance, there is a horizontal line and an image in the logo that disappear as soon as the .text property is changed.

Is there a different way to change the text while retaining images?

gmayor
07-16-2015, 07:44 AM
Use the following



Dim oRng As Range
Set oRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
With oRng.Find
Do While .Execute(FindText:="old text", MatchCase:=True)
oRng.Text = "new text"
Loop
End With

brettdoyle
07-16-2015, 08:40 AM
Interest... I was able to make it.

Any idea why you have to create limits of the text you are modifying in order to retain images and other formatting?