All textboxes on the current page

M

Mark Tangard

OK, so Word has no page object, but I need one anyway. ;)

Is this the most efficient way to operate on all of the textboxes
whose anchors are on the current page?

Dim sh As Shape, r As Range
Set r = Selection.Range
Set r = r.GoTo(What:=wdGoToBookmark, Name:="\page")
r.Select
If r.ShapeRange.Count = 0 Then MsgBox "No shapes.": Exit Sub
For Each sh In ActiveDocument.Shapes
If sh.Type = msoTextBox Then
If sh.Anchor.InRange(r) Then _
sh.TextFrame.TextRange.InsertBefore "Z"
End If
Next

TIA
 
D

Doug Robbins - Word MVP

Hi Mark,

I don't know if this is really any better

Dim sh As Shape, i As Integer
i = 0
Selection.Bookmarks("\page").Range.Select
For Each sh In ActiveDocument.Shapes
If sh.Anchor.InRange(Selection.Range) And sh.Type = msoTextBox Then
sh.TextFrame.TextRange.InsertBefore "Z"
i = i + 1
End If
Next sh
If i = 0 Then MsgBox "No Shapes"

But I don't like using things like GoTo(What:=wdGoToBookmark, Name:="\page")

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
M

Mark Tangard

Well, surprisingly, they both run OK. I was concerned that
iterating through all the shapes in the doc (testing on a
3-pager, destined for something far larger) would cause one
of those meltdown loops....
 
M

martinique

Why not

Set r = ActiveDocument.GoTo(wdGoToPage,
Selection.Range.Information(wdActiveEndPageNumber))

For Each sh In r.ShapeRange
If sh.Type = msoTextBox Then
sh.TextFrame.TextRange.InsertBefore "Z"
end if
Next

Doug's selection suggestion (Selection.Bookmarks("\page").Range.Select)
fails if the selection is inside a textbox -- which I guess is not unlikely
under the circumstances.
 
M

Mark Tangard

Hi martinique,

I actually started with almost that exact approach, but got a
bizarre 'Out of Memory' error (#7) at the loop's 'For' line.
I also found that

r.ShapeRange.Count

gives *zero* no matter what's on the page. Do you get no error
if you run this on a page with some textboxes on it?
 
M

martinique

Very interesting. I tested my code snippet in separate bits. There's
definitely a bug in Word here, but your work-around doesn't avoid it either:
try this on a document with no textboxes:

Set r = activedocument.range
for each sh in r.shaperange

also gives an error, but a different one ('object not available', which I
guess makes sense, although one would prefer it simply to iterate zero
times, like any well-behaved collection).

---
ha! found it. The problem is the selection.goto statement. This doesn't
select the page. It selects only the first position on the page, which might
not be in the paragraph to which the textboxes are anchored. This works

Set pPageRange = ActiveDocument.Bookmarks("\page").Range
If pPageRange.ShapeRange.Count > 0 Then
For Each sh In pPageRange.ShapeRange
Debug.Print sh.Name
Next
End If

but you'll have to add code to deal with the error on the first line, if the
selection is in any storyrange other than the mainstory. (the > 0 test
shouldn't be necessary, but there you go)
 
M

Mark Tangard

Aaaagh. Scary. This particular doc will in fact have all shapes
anchored to each page's first paragraph. (Each page has only 2
paragraphs.) But you just know someone'll hit the Enter key at
some point and all hell will break loose. Back to the lathe.
Thanks for this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top