Solved

Word VBA can't get shape to hide

Posted on 2011-03-17
6
1,725 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.

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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now