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
649 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
[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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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 walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.

751 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