PDA

View Full Version : [SOLVED:] Change Image in Table in some Headers



dbowlds
03-09-2018, 06:42 PM
Hello, I have a Word document that has multiple sections. The first section contains a document cover page. The second section contains our corporate cover letter. In that section the header has a single graphic. The remaining sections (random amount) of the document have in the header a two column, single row table. Our corporate logo resides in the first cell of the table. Sometimes we need the logo to be replaced with another logo. I can create a userform that provides the user with a selection of images (logos) from which the user can select (one). I would then want that image to replace the logo in those pages that have an image in the first cell of a table in the header. Is this possible? I would greatly appreciate any help.

macropod
03-09-2018, 07:58 PM
Try something based on:

Sub UpdateHeaderLogos()
Application.ScreenUpdating = False
Dim Rng As Range, Sctn As Section, HdFt As HeaderFooter, StrNm As String
With Application.Dialogs(wdDialogInsertPicture)
If .Show = True Then StrNm = .Name
End With
If StrNm = "" Then Exit Sub
With ActiveDocument
For Each Sctn In .Sections
For Each HdFt In Sctn.Headers
With HdFt
If .Exists = True Then
If (Sctn.Index = 1) Or (.LinkToPrevious = False) Then
If .Range.Tables.Count = 1 Then
With .Range.Tables(1).Range.Cells(1).Range
If .InlineShapes.Count = 1 Then
Set Rng = .InlineShapes(1).Range
.InlineShapes(1).Delete
.InlineShapes.AddPicture FileName:=StrNm, Range:=Rng
End If
End With
End If
End If
End If
End With
Next
Next
End With
Application.ScreenUpdating = True
End Sub

dbowlds
03-10-2018, 11:02 AM
Paul, thank you for this. The code doesn't seem to do what I need. It inserts the picture in the middle of the page versus in the header. It also only inserts a single instance of the picture even though I have multiple sections with the logo in the header. It also inserts the picture on the first page (which doesn't have a table in the header; actually doesn't even have a header) and on the 2nd page (which has no table but has an image in the header). Any ideas?

macropod
03-10-2018, 01:32 PM
I think you'll find the code is updating all the applicable headers, but it's also adding an extra copy to the document body. The simplest fix is to change:
If .Show = True Then StrNm = .Name
to:
If .Show = True Then StrNm = .Name: Undo

If some headers are being missed, that will be because the existing header lacks a picture in the first cell of a table.

dbowlds
03-11-2018, 05:40 AM
Paul, you are right! The picture that existed in the first cell of the table header actually didn't exist in the cell; it was formatted as over text versus inline with text. Once I changed this to inline your code worked.

However the image that is also inserted in the body is still there. When I tried to change the code to the line you listed above (by adding ": Undo" to the end of the line) I receive a Sub or Function not defined error. Does this mean I need to have a subroutine called Undo that somehow tells the program to skip this step?
One other question. I had originally planned on a userform that presented the user with different picture options (pre-sized). However, I much prefer your approach, leveraging the insert picture dialogue. Once the picture is inserted, replacing the previous header picture, is there a way to autofit the picture to fit the cell column width?
I very much appreciate you helping me with this Paul.
Doug

macropod
03-11-2018, 02:07 PM
You shouldn't get an error simply as a result of adding ': Undo' after StrNm = .Name, though you might if a pic isn't selected and there's nothing to undo. Try replacing:
If .Show = True Then StrNm = .Name
with:


If .Show = True Then
StrNm = .Name
Undo
End If
As for the picture size, if you give the table cell a fixed height and width, any picture inserted into it will have its dimensions constrained to fit the available space. To set the fixed:
• row height, use Table Tools>Layout>Properties>Row>Specify Height>Exactly.
• column width, first uncheck the 'automatically resize to fit contents' option under Table Tools>Layout>Properties>Table>Options. Then set the preferred column width under Table Tools>Layout>Properties>Columns.
You might also want to set the cell margins to 0 all round. It's also a good idea to format the table paragraph with 0 before/after spacing.

dbowlds
03-11-2018, 02:55 PM
Hi Paul, thank you again for the response. You were spot on about how to get the new picture to autofit into the first cell - thanks! - the new inserted logo is sized perfectly!

However, the picture in the body is still being added. When I tried your above change, I still get an error saying that the sub or function is not defined and the debugger highlights the Undo line. Any idea?
21801

macropod
03-11-2018, 03:08 PM
Try using:

ActiveDocument.Undo
though that really shouldn't be needed.

If that doesn't fix things, perhaps you have a faulty Office installation, which you might repairing via Windows Control Panel > Programs > Programs & Features > Microsoft Office (version) > Change > Repair.

dbowlds
03-11-2018, 04:24 PM
PAUL!!!! You ROCK!!! That did the trick. I very much appreciate your responsiveness and willingness to help me out! You really made my day. Thank you.
I am new to this - how does one flag a thread as SOLVED?

macropod
03-11-2018, 04:33 PM
I am new to this - how does one flag a thread as SOLVED?
From the 'Thread Tools' dropdown.