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
632 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
ID: 38830503
How do you define the particular shape, i.e. what information about the shape are you starting with?
0
 

Author Comment

by:Dov_B
ID: 38830524
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
ID: 38830568
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
ID: 38830587
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
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) …
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…

680 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