Adding a shape or textbox to a specified page

J

John M

I've tried to write some code that doesn't work quite as I would like. Can
anyone help?

Various things are done correctly in the earlier part of the code, which I
don't need to bother you with. However I then want to add a new page to the
end and place a text box on there (left hand corner) with given text. This
is given below and works intermittently.

--------------

doc.Range(1).InsertBreak wdPageBreak
Selection.EndKey Unit:=wdStory

Set s = doc.Shapes.AddTextbox(msoTextOrientationHorizontal,
CentimetersToPoints(dcPageMarginLeft + dcPageWd / 2), _
CentimetersToPoints(dcPageMarginTop), dMaxWidth, dMaxHeight)

--------------

The VBA code exists in one document and applies to a new document DOC. It
seems to work the first time it is run but when the code is run a second
time it fails on the second line. The text selection doesn't move to the
last page, which causes the textbox to be incorrectly added to the "current"
page rather than the last.

I presume that the fact that the first DOC is still open means the SELECTION
applies to it rather than the second DOC.

Now I'm no expert in Word VBA having learned VBA with Excel (where I was
instructed not to use "Select" unless unavoidable). So it occurs to me that
there ought to be a way to Add a page and then put a text box on it without
using select.

Any suggestions?
 
J

John M

I have found something that confirms my selection suspicion but I still
would prefer some direction from someone who knows what they are doing.

-----------------------
doc.Range(1).InsertBreak wdPageBreak
doc.Select
Selection.EndKey Unit:=wdStory

Set s = doc.Shapes.AddTextbox(msoTextOrientationHorizontal, _
CentimetersToPoints(dcPageMarginLeft + dcPageWd / 2), _
CentimetersToPoints(dcPageMarginTop), dMaxWidth, dMaxHeight, )
------------------------

This works now but I still would like to know the answer without using the
Select method.


John


Note: I have tried using the following as the anchor in the AddTextbox:
-----------------------
doc.Range(1).InsertBreak wdPageBreak

Set s = doc.Shapes.AddTextbox(msoTextOrientationHorizontal, _
CentimetersToPoints(dcPageMarginLeft + dcPageWd / 2), _
CentimetersToPoints(dcPageMarginTop), dMaxWidth, dMaxHeight,
doc.Paragraphs(doc.Paragraphs.Count).Range)
 
A

Astrid

Hi John,

Try something like this:

--------------------------------------------------------------------------------------------
Dim oRange As Range
Dim oTextBox As Shape

Set oRange = ActiveDocument.Range
oRange.Collapse direction:=wdCollapseEnd
oRange.InsertBreak Type:=wdPageBreak

Set oRange = ActiveDocument.Range
oRange.Collapse direction:=wdCollapseEnd
Set oTextBox = ActiveDocument.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=0, _
Top:=0, _
Width:=CentimetersToPoints(5), _
Height:=CentimetersToPoints(5), _
Anchor:=oRange)

Set oTextBox = Nothing
Set oRange = Nothin
--------------------------------------------------------------------------------------------
 

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