Log in

View Full Version : Solved: Adding Text Box to Second Page



Belch
12-13-2005, 08:27 AM
Sorry to ask for more info but got another problem I could use some help with.

I am trying to add a text box (with text hardcoded in it) to the second page of a document. I recorded a macro to do this (when it comes to autoshapes I try to stay away from manual coding!) but when I run the macro it adds the text box in the right position - but on the wrong page.

Below is the code:
Public Sub PutTextBoxOnPage()
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 171.3, _
410, 245.7, 31.5).Select
Selection.ShapeRange.TextFrame.TextRange.Select
Selection.Collapse
Selection.ShapeRange.Select
Selection.Font.Name = "Arial"
Selection.Font.Name = "Arial"
Selection.Font.Size = 10
Selection.Font.Bold = wdToggle
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.TypeText Text:="THE TEXT GOES HERE"
Selection.ShapeRange.Select
Selection.ShapeRange.ScaleHeight 0.62, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.IncrementTop -149.8
Selection.ShapeRange.Fill.Visible = msoFalse
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoFalse
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 19.45
Selection.ShapeRange.Width = 245.5
Selection.ShapeRange.TextFrame.MarginLeft = 7.2
Selection.ShapeRange.TextFrame.MarginRight = 7.2
Selection.ShapeRange.TextFrame.MarginTop = 3.6
Selection.ShapeRange.TextFrame.MarginBottom = 3.6
Selection.ShapeRange.RelativeHorizontalPosition = _
wdRelativeHorizontalPositionColumn
Selection.ShapeRange.RelativeVerticalPosition = _
wdRelativeVerticalPositionParagraph
Selection.ShapeRange.LockAnchor = False
Selection.ShapeRange.WrapFormat.AllowOverlap = True
Selection.ShapeRange.WrapFormat.Side = wdWrapBoth
Selection.ShapeRange.WrapFormat.DistanceTop = InchesToPoints(0)
Selection.ShapeRange.WrapFormat.DistanceBottom = InchesToPoints(0)
Selection.ShapeRange.WrapFormat.DistanceLeft = InchesToPoints(0.13)
Selection.ShapeRange.WrapFormat.DistanceRight = InchesToPoints(0.13)
Selection.ShapeRange.WrapFormat.Type = 3
Selection.ShapeRange.ZOrder 5
End Sub

All I need to do is get the box to appear on the second page (in the same position on the page). I'm guessing it's the AddTextBox method that needs changing or perhaps one of the Relative...Position methods. I just can't see any way of telling it to put the textbox on page two not page one.

Any hints welcome, cheers.

fumei
12-13-2005, 09:24 PM
1, It is much better coding to use With statements. Your code:
Selection.Collapse
Selection.ShapeRange.Select
Selection.Font.Name = "Arial"
Selection.Font.Name = "Arial"
Selection.Font.Size = 10
Selection.Font.Bold = wdToggle
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.TypeText Text:="THE TEXT GOES HERE"
Selection.ShapeRange.Select
Selection.ShapeRange.ScaleHeight 0.62, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.IncrementTop -149.8
Selection.ShapeRange.Fill.Visible = msoFalse
Selection.ShapeRange.Fill.Transparency = 0#
Selection.ShapeRange.Line.Weight = 0.75
Selection.ShapeRange.Line.DashStyle = msoLineSolid
Selection.ShapeRange.Line.Style = msoLineSingle
Selection.ShapeRange.Line.Transparency = 0#
Selection.ShapeRange.Line.Visible = msoFalse
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 19.45
Selection.ShapeRange.Width = 245.5
Selection.ShapeRange.TextFrame.MarginLeft = 7.2
Selection.ShapeRange.TextFrame.MarginRight = 7.2

Using With statements:
With Selection
.Collapse
.ShapeRange.Select
With .Font
.Name = "Arial"
.Size = 10
.Bold = wdToggle
End With
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeText Text:="THE TEXT GOES HERE"
With .ShapeRange
.Select
.ScaleHeight 0.62, msoFalse, msoScaleFromTopLeft
.IncrementTop -149.8
.Fill.Visible = msoFalse
.Fill.Transparency = 0#
With .Line
.Weight = 0.75
.DashStyle = msoLineSolid
.Style = msoLineSingle
.Transparency = 0#
.Visible = msoFalse
End With
.LockAspectRatio = msoFalse
.Height = 19.45
.Width = 245.5
.TextFrame.MarginLeft = 7.2
.TextFrame.MarginRight = 7.2
End With
End With

2. You are using a Shape. It has an anchor. It always defaults to the beginning of the page. However, your code does correctly position things. Could you explain it better? I ran your code with the Selection on the second page, and the box is placed correctly - on the second page.

Belch
12-14-2005, 08:29 AM
I edited my code to use With...End With statements, but the textbox was still appearing on the first page.
I had a look into the anchors you mentioned and amended the AddTextbox command to this:

ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 155, _
350, 245, 31, ActiveDocument.Tables(2).Range).Select

Each page has a table on it, so Tables(2) selects the second page's table.

I also had to tweak the 'Left' and 'Top' arguments of the AddTextbox method a bit.

This was the quickest way I could see to set the textbox anchor to something on the second page. Whenever I omit the anchor it appears on page one again. It seems to work fine, but I am open to any suggestions as to a more efficient way.
Thanks for the help Gerry.

wnazzaro
12-14-2005, 08:54 AM
Might something like this work:

With ActiveWindow.Selection
.GoTo What:=wdGoToPage, Which:=wdGoToFirst
.GoTo What:=wdGoToPage, Which:=wdGoToNext
End With

That way you start on page two everytime.

Belch
12-14-2005, 09:38 AM
That seems to work fine as well, and seems more sensible than selecting the whole table on a page. Thanks!

fumei
12-14-2005, 03:47 PM
Why bother going to the first page, and then going to the next page. You can just go to a defined page.
Selection.GoTo What:=wdGoToPage, _
Which:=wdGoToNext, Name:="2"

The point being is that you have to have the Selection on the specified page. You do not need to actually select anything.

wnazzaro
12-14-2005, 04:02 PM
Doesn't this make more logical sense?
Selection.GoTo What:=wdGoToPage, Name:=2
I didn't realize GoTo had a Name argument, but that is better.