Remove Image from Header

Hello,

I currently have a macro that inserts a logo into the header of a Word document.  I am currently working on a macro that removes the image from the header.  However, the header of my Word document has two images, so I only need to remove one of them.  The code below shows that I have so far.

When I run the macro to add the image into the headers, it will name the images in inserts based on the sections and pages.  So if I have three sections, and two pages per section the logo names would be Logo1_1, Logo1_2, Logo2_1, Logo2_2.  How do I get my macro LogoRemove to remove those logos with the name?  I do not want to specify each name in the LogoRemove macro, it should automatically have a counter or find the logo in each section and delete it.  

Let me know if you need any further clarification.

Thanks.
Sub LogoRemove()
    Dim shapes_to_delete As New Collection
    Dim sh As Shape
    Dim hdr As HeaderFooter
    
    RemoveLogoInAHeader ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
    
    If ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter Then
        RemoveLogoInAHeader ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage)
    End If
    
    If ActiveDocument.Sections(1).PageSetup.OddAndEvenPagesHeaderFooter Then
        RemoveLogoInAHeader ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages)
    End If
End Sub

Sub RemoveLogoInAHeader(hdr As HeaderFooter)
    Dim shapes_to_delete As New Collection
    Dim sh As Shape

    For Each sh In hdr.Shapes
         Select Case sh.Name
             Case "Picture 4", "Picture 2", "Logo1_1", "Logo2_1"
                 shapes_to_delete.Add sh
         End Select
     Next sh
     For Each sh In shapes_to_delete
         sh.Delete
     Next sh

End Sub

Open in new window

LVL 2
navid86Asked:
Who is Participating?
 
GrahamSkanRetiredCommented:
You could do something like this
Sub RemoveLogoInAHeader(hdr As HeaderFooter)
    Dim shapes_to_delete As New Collection
    Dim sh As Shape

    For Each sh In hdr.Shapes
         Select Case sh.Name
             Case "Picture 4", "Picture 2"
                 shapes_to_delete.Add sh
         Case Else
            If sh.Name Like "Logo#_#" Then
                  shapes_to_delete.Add sh
            End If
         End Select
     Next sh
     
     For Each sh In shapes_to_delete
         sh.Delete
     Next sh

End Sub

Open in new window

0
 
navid86Author Commented:
Perfect Solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.