Solved

how do I get the index of a particular shape in a collection of activedocument.shapes MS Word VBA

Posted on 2013-01-29
4
592 Views
Last Modified: 2013-01-29
how do I get the index of a particular shape in a collection of activedocument.shapes MS Word VBA
0
Comment
Question by:Dov_B
  • 2
  • 2
4 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
How do you define the particular shape, i.e. what information about the shape are you starting with?
0
 

Author Comment

by:Dov_B
Comment Utility
well in  the solution you gave me an hour ago
you wrote the following code
Sub AddSmallTransparentCaption()
    Dim sh As Shape
    Dim tb As Shape
    Dim s As Integer

    s = 1
    
    Set sh = ActiveDocument.Shapes(s)
    sh.Select
    Selection.InsertCaption "Figure", , , wdCaptionPositionBelow
    
    Set tb = ActiveDocument.Shapes(s + 1)
    With tb
        .TextFrame.TextRange.Font.Size = 6
        .Fill.Transparency = 1
        .TextFrame.AutoSize = True 'doesn't seem to work
    End With
End Sub

Open in new window

so I wanted to know how I could add captions to pre existing shapes in my old document by selecting them and running a macro but I won't Be able to reference the caption because I won't know its index in the collection
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
I used the Index, because I didn't know about your situation.

If you were wanting to incorporate the code into a larger macro that, say, loops through several shapes, then the index would be useful. In fact, the InsertCaption method only works with the Selection, so it is necessary to ensure that the Shape is selected.

If you are starting with the shape already selected,  omit lines 8 & 9. When the caption is created, it is automatically selected, so you just need to use the first ShapeRange Object.
Sub AddSmallTransparentCaption()
    Dim tb As Shape
    
    Selection.InsertCaption "Figure", , , wdCaptionPositionBelow
    
    Set tb = Selection.ShapeRange(1)
    With tb
        .TextFrame.TextRange.Font.Size = 6
        .Fill.Transparency = 1
        .TextFrame.AutoSize = True 'doesn't seem to work
    End With
    tb.Width = 30
    tb.Height = 10
End Sub

Open in new window

0
 

Author Closing Comment

by:Dov_B
Comment Utility
Dear GrahamSkan I am a teacher and an amateur self taught programmer I am in the middle of making a library of free material for elementary students with learning disabilities. It is only possible because of the many answers you have given me! May you be blessed with everything GOOD!
0

Featured Post

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.

Join & Write a Comment

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

728 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

10 Experts available now in Live!

Get 1:1 Help Now