?
Solved

Remove Image from Header

Posted on 2011-10-12
2
Medium Priority
?
415 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:navid86
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 36957687
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
 
LVL 2

Author Closing Comment

by:navid86
ID: 36958020
Perfect Solution!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question