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

--
Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
"Life is nothing if you're not obsessed." --John Waters

Re: All textboxes on the current page by Doug

Doug
Sat Aug 09 07:13:10 CDT 2003

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
"Mark Tangard" <mark@tangard.com> wrote in message
news:3F34AF63.BA49C5C9@tangard.com...
>
> 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
>
> --
> Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> "Life is nothing if you're not obsessed." --John Waters



Re: All textboxes on the current page by Mark

Mark
Sat Aug 09 16:51:11 CDT 2003

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

--
Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
"Life is nothing if you're not obsessed." --John Waters


Doug Robbins - Word MVP wrote:
>
> 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
> "Mark Tangard" <mark@tangard.com> wrote in message
> news:3F34AF63.BA49C5C9@tangard.com...
> >
> > 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
> >
> > --
> > Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> > "Life is nothing if you're not obsessed." --John Waters

Re: All textboxes on the current page by martinique

martinique
Sun Aug 10 18:05:47 CDT 2003

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.






"Mark Tangard" <mark@tangard.com> wrote in message
news:3F34AF63.BA49C5C9@tangard.com...
>
> 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
>
> --
> Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> "Life is nothing if you're not obsessed." --John Waters



Re: All textboxes on the current page by Mark

Mark
Sun Aug 10 22:27:54 CDT 2003

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?

--
Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
"Life is nothing if you're not obsessed." --John Waters


martinique wrote:
>
> 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.
>
> "Mark Tangard" <mark@tangard.com> wrote in message
> news:3F34AF63.BA49C5C9@tangard.com...
> >
> > 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
> >
> > --
> > Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> > "Life is nothing if you're not obsessed." --John Waters

Re: All textboxes on the current page by martinique

martinique
Mon Aug 11 01:20:49 CDT 2003

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)







"Mark Tangard" <Mark@Tangard.com> wrote in message
news:3F370D3A.8C6FCCBB@Tangard.com...
> 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?
>
> --
> Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> "Life is nothing if you're not obsessed." --John Waters
>
>
> martinique wrote:
> >
> > 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.
> >
> > "Mark Tangard" <mark@tangard.com> wrote in message
> > news:3F34AF63.BA49C5C9@tangard.com...
> > >
> > > 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
> > >
> > > --
> > > Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> > > "Life is nothing if you're not obsessed." --John Waters



Re: All textboxes on the current page by Mark

Mark
Mon Aug 11 07:17:51 CDT 2003

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.

--
Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
"Life is nothing if you're not obsessed." --John Waters


martinique wrote:
>
> 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)
>
> "Mark Tangard" <Mark@Tangard.com> wrote in message
> news:3F370D3A.8C6FCCBB@Tangard.com...
> > 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?
> >
> > --
> > Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> > "Life is nothing if you're not obsessed." --John Waters
> >
> >
> > martinique wrote:
> > >
> > > 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.
> > >
> > > "Mark Tangard" <mark@tangard.com> wrote in message
> > > news:3F34AF63.BA49C5C9@tangard.com...
> > > >
> > > > 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
> > > >
> > > > --
> > > > Mark Tangard <Mark@Tangard.com>, Microsoft Word MVP
> > > > "Life is nothing if you're not obsessed." --John Waters