?
Solved

Word VBA can't get shape to hide

Posted on 2011-03-17
6
Medium Priority
?
1,923 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
Independent Software Vendors: 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 2000 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

Industry Leaders: 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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
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.
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

762 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