Solved

Word VBA can't get shape to hide

Posted on 2011-03-17
6
1,787 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
  • 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
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.
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

810 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