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