Consulting

Results 1 to 10 of 10

Thread: Change Image in Table in some Headers

  1. #1
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location

    Post Change Image in Table in some Headers

    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.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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?
    undo error.jpg

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Regular
    Joined
    Mar 2018
    Location
    Leesburg
    Posts
    68
    Location
    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?

  10. #10
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by dbowlds View Post
    I am new to this - how does one flag a thread as SOLVED?
    From the 'Thread Tools' dropdown.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •