Solved

Word VBA can't get shape to hide

Posted on 2011-03-17
6
1,841 Views
Last Modified: 2012-05-11
Hi experts

I have to hide some images in a document. I confirm that the shape is correctly named. Tested by selecting the shape and running ?selection.shaperange.name

Image is named corrected
Image is in the first page footer

But the code below will not hide it. The images in the header are working, just not this one.

Any ideas what could be going wrong?
Set ThisDoc = ActiveDocument
    
    On Error Resume Next
    
    For Each sec In ThisDoc.Sections
    
        sec.Headers(wdHeaderFooterFirstPage).Shapes("Logo1").Visible = msoFalse
        sec.Headers(wdHeaderFooterFirstPage).Shapes("PRCLogo1").Visible = msoFalse
        sec.Headers(wdHeaderFooterFirstPage).Shapes("Line").Visible = msoFalse
        sec.Footers(wdHeaderFooterFirstPage).Shapes("StraplineFirstPage").Visible = msoFalse

    Next sec

Open in new window

0
Comment
Question by:Fi69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35163102
It might help to remove the On Error line.


Does this code tell you anything?
Dim ThisDoc As Document
    Dim sec As Section
    Dim sh As Shape
    Set ThisDoc = ActiveDocument
    
    'On Error Resume Next
    
    For Each sec In ThisDoc.Sections
        For Each sh In sec.Headers(wdHeaderFooterFirstPage).Shapes
            Debug.Print sh.Name
        Next sh
    Next sec

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35163113
Sorry.
That should have been looking in the Footers
Dim ThisDoc As Document
    Dim sec As Section
    Dim sh As Shape
    Set ThisDoc = ActiveDocument
    
    'On Error Resume Next
    
    For Each sec In ThisDoc.Sections
        For Each sh In sec.Footers(wdHeaderFooterFirstPage).Shapes
            Debug.Print sh.Name
        Next sh
    Next sec

Open in new window

0
 

Author Comment

by:Fi69
ID: 35163270
That's really bizarre. That code you supplied returns the name of all the shapes in the document (not isolated to the footer).

What it did reveal is that the shape in the primary footer and first page footer are named the same. I've changed the name and now it is hiding as expected. Must be the double up of the name. That's strange too, because, I would think because I've specified where the shape to be found, there wouldn't be any confusion.

Thoughts?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 35164424
Yes, Fi, that information has provoked some thoughts.

I hadn't picked upon it before, but there is only one is only one Shapes collection for all headers and footers in a document. The two routines that I gave you before would produce the same result.

If you need to address the Shapes in a particular header or footer, you should use the ShapeRange collection.



Dim ThisDoc As Document
    Dim sec As Section
    Dim sh As Shape
    Set ThisDoc = ActiveDocument
    
    'On Error Resume Next
    
    For Each sec In ThisDoc.Sections
        For Each sh In sec.Footers(wdHeaderFooterFirstPage).Range.ShapeRange
            Debug.Print sh.Name
        Next sh
    Next sec

Open in new window

0
 

Author Comment

by:Fi69
ID: 35164479
Thanks Graham, perhaps that's the same reason it doesn't allow for doubling up of the shape name using the code that I had.

Are you able to guide me with using shaperange dynamically in the header footer to loop through as per above? Perhaps then I could have my shapes named the same thing (as it would be better)?

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 35164813
It seems that if you have more than one shape in the collection with the same name, the first one of that name in the collection is the one that is returned.

Unfortunately, the shaperange collection doesn't find the object by name, so you will have to step through the collection and find it.
Dim shr As ShapeRange
Set ThisDoc = ActiveDocument
    
    'On Error Resume Next
    
    For Each sec In ThisDoc.Sections
       Set shr = sec.Headers(wdHeaderFooterFirstPage).Range.ShapeRange
        GetShapeByName("Logo1", shr).Visible = msoFalse
        GetShapeByName("PRCLogo1", shr).Visible = msoFalse
        GetShapeByName("Line", shr).Visible = msoFalse
        GetShapeByName("StraplineFirstPage", shr).Visible = msoFalse

    Next sec

Function GetShapeByName(strName As String, shr As ShapeRange) As Shape
    Dim sh As Shape
    
    For Each sh In shr
        If StrComp(strName, sh.Name, vbTextCompare) = 0 Then
            Set GetShapeByName = sh
            Exit Function
        End If
    Next sh
End Function

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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