PDA

View Full Version : Help with updating an image in the footer



Tecnik
04-19-2006, 07:38 AM
Hi there,

Please can someone help me with this.

I've created quite a few individual Word docs that all contain a common image in the footer. Because of my lack of experience with Word (more of a Quark man) I have inserted the image in each document. I now need to update the image, in every footer, in every document (that means approximately 250-300 updates)! Yep, I'll bet there are better ways of doing this I know and I'm kicking myself I didn't use one of them.

I was hoping to do this update using vba to run through all the files in a folder and apply this change to the footers. I already have some code for looping through files in a folder (thanks Gerry!) however it's the updating/ insertion of the new image I'm trying to solve.

So far I'm playing with this trying to get it to work:-


Sub Insert_Pic()

' declare an InlineShape object variable

Dim MyShape As Shape

' store the pointer to the new shape

Set MyShape = ActiveDocument.Shapes.AddPicture(FileName:=_
"C:\Documents and Settings\pathToFooter\jcfooter_test.tif", _
LinkToFile:=False, SaveWithDocument:=True, Anchor:=Selection.Range)

' set properties of the Shape object

With MyShape
.Width = 492.75
.Height = 32.25
.ZOrder msoSendBehindText
.WrapFormat.Type = wdWrapNone
.RelativeHorizontalPosition = wdRelativeHorizontalPositionCharacter
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.Left = 0
.Top = InchesToPoints(0.02)
End With
End Sub

I've found the code somewhere and have managed to get as far as the insertion of an image, however, one problem I've encountered is that when I try apply the new settings nothing happens.

Secondly, I'm also wanting to put this in some loop that deletes the current footer image.

Any help would be appreciated with this, thanks.

Nick

Edited by GeekGirlau 21-Apr-06. Reason: put line breaks in code

fumei
04-19-2006, 11:05 PM
Word is very very weird processing VBA and graphics. Very weird.

1. Set the Shape object after you insert it. Use the Count of the collection.Set mShape = ActiveDocument.Shapes(ActiveDocument.Shapes.Count)This sets the object as the last one in the collection. The one you just put in.

2. I don't know what your code is for getting at the footer. However, essentially you can delete the whole footer using .Delete.

Tecnik
04-20-2006, 05:56 AM
Thanks for the help Gerry, I used the code to check I was getting what I wanted.

In the end I came up with this routine for replacing the footer:-
Sub ReplaceFooter()
With ActiveDocument.PageSetup
.DifferentFirstPageHeaderFooter = False
End With

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.SelectAll
Selection.Delete

Dim MyShape

Set MyShape = ActiveDocument.Shapes.AddPicture _
(FileName:="C:\Documents and Settings\pathToDocument\jcfooter_test.tif", _
LinkToFile:=False, SaveWithDocument:=True, Anchor:=Selection.Range, _
Left:=-5.5, Top:=-3, Width:=493, Height:=32.25)

MyShape.ZOrder msoSendBehindText

Set MyShape = Nothing

Selection.WholeStory
Selection.Font.Color = wdColorWhite

ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

The routine deletes the existing image, in the footer, and inserts the new one.
It then tweaks the position of the image and sends the image behind the text in the footer. It then re-colors the text in the footer.

This method works but so far is limited in that it toggles 'DifferentFirstPageHeaderFooter = False',
so if you've a different header and footer on the first page you're stuck. Hopefully this will be easy enough to amend.

I put this routine into one you posted for looping through documents in a given folder and it works fine.

Any help on how this could be improved would be appreciated.
Also, can anyone recommend any good books for VBA newbies, I'd like to read more on the subject but am not sure where to start.

Thanks,

Nick

fumei
04-24-2006, 08:26 PM
I'm sorry, but you are going to have to explain what the ehck you are doing with the footers.

Yes, it DOES set DifferentFirstPage to False. Hmmmmm. That is because there is a line that says to do so. If you do not want the code to do that, then take out the line.

It is usually better to action headers/footers directly, if you can. By that I mean, rather than using the View thing (which makes actually changes in the screen views) you can act on explicit headers and footers.

What your code is doing is:
1 making DifferentFirstPage = False. This does NOT remove any content that is in DifferentFirstPage. If you have content in there, it is STILL in there.

2. Deletes everything in HeaderFooterPrimary. Again, this does NOT remove anything from DifferentFirstPage

So tell me what exatcly you want. Describe the header/footer situation.

Tecnik
04-25-2006, 01:26 AM
Hi Gerry,

Perhaps I'd not explained myself properly enough. The code I have posted does exactly what I want and seems to work ok (although it may not do it in the most efficient manner, I don't know, that's why I'd asked for comments). I'd posted the code to, hopefully, help others.

I'd run through what I wanted by hand before trying to create the code to do the same thing.
The way the documents had been set up there WAS a different first page and that's why I set DifferentFirstPage = False. I'd found that if I do that and then select the image in the footer and delete it, this would carry through to all pages. Whereas if i'd left DifferentFirstPage = True then the first page wouldn't be touched. That's why I'd made the comment


This method works but so far is limited in that it toggles 'DifferentFirstPageHeaderFooter = False',
so if you've a different header and footer on the first page you're stuck. Hopefully this will be easy enough to amend.

As I've said before, I'd not actually asked for any help because the code works, I'd asked for comments on how I could improve what I have.

Thanks for the suggestion regarding actioning headers/footers directly, I think I may have read another post regarding this. I'll try and incorporate that into the code I have.

Sorry for any confusion and thanks again for your help.

Regards

Nick

fumei
04-25-2006, 11:20 AM
Sorry, I did not mean to sound critical.
I'd found that if I do that and then select the image in the footer and delete it, this would carry through to all pages. Whereas if i'd left DifferentFirstPage = True then the first page wouldn't be touched. That's why I'd made the commentHere is some code that will action all headers in a section, including DifferentFirstpageSub EachHeader()
Dim oHeader As Word.HeaderFooter
Dim MyShape
For Each oHeader In ActiveDocument.Sections(1).Headers
oHeader.Range.Delete

Set MyShape = ActiveDocument.Shapes.AddPicture _
(FileName:="C:\Documents and Settings\pathToDocument\jcfooter_test.tif", _
LinkToFile:=False, SaveWithDocument:=True, Anchor:=Selection.Range, _
Left:=-5.5, Top:=-3, Width:=493, Height:=32.25)

MyShape.ZOrder msoSendBehindText

Set MyShape = Nothing

oHeader.Range.Font.Color = wdColorWhite
Next
End SubNo need to make DifferentFirstPage = False.

Tecnik
04-26-2006, 02:16 AM
Hi Gerry,

Thanks for your reply and help with the code.

Your routine looks great, very clean and concise.
The new code has already shown me how I need to be thinking,
in the future, when I next start code something.

Once again, thank you for your help and for forwarding my knowledge
and understanding of VBA.

Regards,

Nick