Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Word VBA can't get shape to hide

Posted on 2011-03-17
6
Medium Priority
?
1,970 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
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.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses

610 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