Searching autoshapes

konopca
NewLounger
Posts: 5
Joined: 03 Feb 2011, 14:08

Searching autoshapes

Post by konopca »

Hi all -
I have been a visitor here for a while now but this is my first post here. My company is currently using Word 2003, in a few weeks updating to Word 2010.

HansV provided this code some time ago on another lounge and it has always worked quite well. Lately though I am finding in my longer documents that it is unclear if the code has finished running, it just keeps looping. Is it possible to modify the code so that it searches the shapes in the order that they appear in the document from beginning to end? I have never been able to figure out the order in which the shapes were found and every time I open the document it starts finding at a different place.

Sub FindNextAutoShape()
Dim lngNumShapes As Long
Static lngCurShape As Long
Dim i As Long
lngNumShapes = ActiveDocument.Shapes.Count
If lngCurShape >= lngNumShapes Then
lngCurShape = 0
End If
For i = lngCurShape + 1 To lngNumShapes
Select Case ActiveDocument.Shapes(i).Type
Case msoLine, msoTextBox
lngCurShape = i
ActiveDocument.Shapes(i).Select
ActiveWindow.ScrollIntoView ActiveDocument.Shapes(i)
Exit For
End Select
Next i
End Sub

Thanks in advance for any help.

User avatar
StuartR
Administrator
Posts: 12629
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Searching autoshapes

Post by StuartR »

I don't think there is any way to control the order in which the shapes are found, but you could add a line to update the status bar to show how far through the shapes collection you are, like this:

Code: Select all

Sub FindNextAutoShape()
Dim lngNumShapes As Long
Static lngCurShape As Long
Dim i As Long
    lngNumShapes = ActiveDocument.Shapes.Count
    If lngCurShape >= lngNumShapes Then
        lngCurShape = 0
    End If
    For i = lngCurShape + 1 To lngNumShapes
    Application.StatusBar = "Checking shape " & Str(i) & " of " & Str(lngNumShapes)
        Select Case ActiveDocument.Shapes(i).Type
            Case msoLine, msoTextBox
                lngCurShape = i
                ActiveDocument.Shapes(i).Select
                ActiveWindow.ScrollIntoView ActiveDocument.Shapes(i)
                Exit For
        End Select
    Next i
End Sub
StuartR


konopca
NewLounger
Posts: 5
Joined: 03 Feb 2011, 14:08

Re: Searching autoshapes

Post by konopca »

I think this will work Stuart, thank you.

User avatar
HansV
Administrator
Posts: 78622
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Searching autoshapes

Post by HansV »

I posted a reply a couple of hours ago, but it has gone up in smoke. A belated welcome to Eileen's Lounge!

Stuart has probably already solved your problem, but if you want to loop through shapes in the order in which they are located in the document, you could do the following:

Fill an array with the shapes, and another array with the start position of the anchors of the shapes.
Sort the second array in ascending order, and simultaneously sort the first array.
You can now loop through the first array.

Post back if you want to use this and need help with it.
Best wishes,
Hans

konopca
NewLounger
Posts: 5
Joined: 03 Feb 2011, 14:08

Re: Searching autoshapes

Post by konopca »

Hi HansV - I have no idea how to do this. Anyway the code works well enough as it is for now. It could probably use some tweaking but the changes you suggest sound difficult. What I mean by tweaking is that the line "Checking shape x of x" is always starting at shape 3 of X. It is not finding the first two because they are not lines or text boxes. But that is okay too. Thanks for replying though.
Carol.

User avatar
HansV
Administrator
Posts: 78622
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Searching autoshapes

Post by HansV »

OK, we'll leave it alone then.
Best wishes,
Hans