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
671 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

628 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