Adding a text box script

O

Oz Springs

I have created a template that includes the insertion of a text box. I am
using Word 2004 (Mac) and the template is to be used on PCs.

The problems is that the text box draw does not work on a PC using Word 2002
and Windows XP Professional. The offending line is:

________________________

ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 42.55, _
667.65, 340#, 69#).Select
_______________________
And the error message is:

Run-time error Œ-2147024891 (80070005)¹
This member cannot be accessed in this view.
______________________

Can anyone tell me what is wrong and, better still, how to correct it?

Thanks for any help




Oz

PS I am wondering, as ³in this view² is something I think I can understand
in the cryptic error message, whether the client has the form open in Normal
rather than Page Layout view, or maybe in header or footer. How can I stop
him from trying to run this macro in anything other than Page Layout view?
 
J

Jay Freedman

Hi Oz,

I can reproduce the error message if the selection is in a header/footer,
and a similar one if the selection is in a footnote/endnote. It doesn't
matter whether the view is Normal, as the selection of the textbox will
force the view to Print Layout automatically.

Add this at the beginning of the macro to get to the right view:

If Selection.Information(wdInHeaderFooter) Or _
Selection.Information(wdInCommentPane) Or _
Selection.Information(wdInFootnoteEndnotePane) Then
With ActiveDocument.ActiveWindow.View
.Type = wdPrintView ' not strictly necessary...
.SeekView = wdSeekMainDocument
End With
End If
 
J

Jay Freedman

I meant to add this bit before sending my response:

It's probably a better idea to avoid selecting things and working with the
Selection object altogether, if that's possible. There are only a few things
you can do with the Selection that can't be done with Range objects and
other objects.

You'd start by declaring a Shape object

Dim myTextBox as Shape

and assign the result of the .AddTextbox method to it; use the Anchor
parameter to specify the anchor location from which the Left and Top are
measured:

Set myTextBox = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=42.55, Top:=667.65, _
Width:=340#, Height:=69#, _
Anchor:=ActiveDocument.Paragraphs(1).Range)

Then you can manipulate the myTextBox object in any way you need to -- add
text, format the TextFrame, etc. -- all without touching the Selection. It
won't matter what's in the active window, view, or pane.
 
O

Oz Springs

Hi Jay

Thank you very for your help. I haven¹t checked it out yet, but can you
confirm that you have set the textbox anchor to the first paragraph so that
it won¹t move around? If so, this is a bonus because I was going to set it
to stay put and you have saved me the bother of sorting that out as well.

Kind regards



Oz
 
J

Jay Freedman

Hi Oz,

Actually, I chose to anchor the textbox in the first paragraph because
that's a simple thing to do in an example. You can choose any spot in the
document that suits your needs.

The relationship between an object's position and its anchor is more than a
little strange. The only hard-and-fast rule is that the object must be on
the same page as the anchor. If text is added before the anchor range that
moves the anchor to the next page, the object will also jump to the next
page.

Within that restriction, there's no foolproof way to lock to location of the
object on that page. If you "lock the anchor" either programmatically or
through the Format Object dialog, you prevent only the movement of the
anchor from one paragraph to another; the object itself can still be dragged
to a new position on the page.

If you want the object to stay on the first page (or the first page of any
particular section), you can anchor it in the First Page Header instead of
the regular text (the object's position can still be anywhere on the page).
That will put it mostly out of harm's way -- the user will have to go to
some trouble just to select it. As a further protection, you can write
macros to (mostly) prevent user access to the header (see
http://word.mvps.org/FAQs/Customization/ProtectWord2000PlusHeader.htm)
unless their security setting disables macros.

That may be more than you really wanted to know, but some day you'll need
it. ;-)
 
O

Oz Springs

Thank you for this information. It should be safer to put the anchor on the
top paragraph mark in order to stop it moving up and down if text above it
changes, though this is not guaranteed.

It would be good if Microsoft could make text boxes less fragile, especially
for people creating templates, such as this one, to be used by many people
who don¹t know Word and its idiosyncrasies.

Text box text can¹t be seen in galley view and it is so easy to just delete
it without knowing you have done so (oh, these blank paragraph marks look so
messy...). Also, we should be able to ³lock² a text box in a position and on
a particular page without relying on those moveable paragraph marks. This
template will be used on a pre-printed, single page form and the text box
has to rest upon a stripe of colour. Otherwise I¹d avoid text boxes like the
plague in templates.

Wouldn¹t it be nice if Microsoft found a way of fixing this problem?

Many thanks



Oz
 

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