PDA

View Full Version : Create a macro to insert a text box in Word



PJY
11-07-2007, 02:53 PM
I?m using Word 2003 XP. What I would like to do is create a macro that would allow me to place a header & footer on a landscape page in the same position as it is on a portrait page. I have worked around this in the past, but we are doing more and more documents that have both portrait and landscape that I would like a macro to help me out. My thought was to create a macro that would insert a text box for the footer and a text box for the header. However, I keep getting errors and cannot figure out what I am doing wrong.
Here is exactly what I would like to do:
Insert text box on the top left side of my document, sized 0.84 h x 0.48 w, Layout = behind text, absolute position at horizontal -0.5 to the right of column and vertical 1.22 below page, with the text direction changed so that it reads vertically from top to bottom, font Times New Roman, 8 pt., italic and no line. The second text box needs to be sized at 0.42 x 0.42, Layout = behind text, absolute position at horizontal 8.68 to the right of column, and 7.26 below page, this will also have the text reading vertically from top to bottom, Times New Roman, 12 pt, and no line.
Here?s what I currently have:
Selection.CreateTextbox
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
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 = msoTrue
Selection.ShapeRange.Line.ForeColor.RGB = RGB(0, 0, 0)
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Left = 72#
Selection.ShapeRange.Top = 90#
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.Left = InchesToPoints(0)
Selection.ShapeRange.Top = InchesToPoints(0)
Selection.ShapeRange.LockAnchor = False
Selection.ShapeRange.LayoutInCell = True
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 = wdWrapSquare
Selection.ShapeRange.TextFrame.AutoSize = True
Selection.ShapeRange.TextFrame.WordWrap = False
Selection.ShapeRange.ScaleHeight 2.48, msoFalse, msoScaleFromTopLeft
Selection.Font.Size = 8
Selection.Font.Italic = wdToggle
Selection.Orientation = wdTextOrientationDownward
Selection.ShapeRange.ScaleHeight 4.44, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
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 = msoTrue
Selection.ShapeRange.Line.ForeColor.RGB = RGB(0, 0, 0)
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 60.5
Selection.ShapeRange.Width = 34.55
Selection.ShapeRange.Left = 72#
Selection.ShapeRange.Top = 90#
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.Left = InchesToPoints(0)
Selection.ShapeRange.Top = InchesToPoints(0)
Selection.ShapeRange.LockAnchor = False
Selection.ShapeRange.LayoutInCell = True
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 = wdWrapSquare
Selection.ShapeRange.TextFrame.AutoSize = False
Selection.ShapeRange.TextFrame.WordWrap = True
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
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 = msoTrue
Selection.ShapeRange.Line.ForeColor.RGB = RGB(0, 0, 0)
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 60.5
Selection.ShapeRange.Width = 34.55
Selection.ShapeRange.Left = 72#
Selection.ShapeRange.Top = 90#
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 = _
wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = InchesToPoints(-0.5)
Selection.ShapeRange.Top = InchesToPoints(1.22)
Selection.ShapeRange.LockAnchor = False
Selection.ShapeRange.LayoutInCell = True
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
Selection.ShapeRange.TextFrame.AutoSize = False
Selection.ShapeRange.TextFrame.WordWrap = True
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 108#, _
90#, 54#, 45#).Select
Selection.ShapeRange.TextFrame.TextRange.Select
Selection.Collapse
Selection.ShapeRange.Select
Selection.ShapeRange.ScaleWidth 0.67, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 0.6, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.Fill.Visible = msoTrue
Selection.ShapeRange.Fill.Solid
Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)
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 = msoTrue
Selection.ShapeRange.Line.ForeColor.RGB = RGB(0, 0, 0)
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
Selection.ShapeRange.LockAspectRatio = msoFalse
Selection.ShapeRange.Height = 30.25
Selection.ShapeRange.Width = 30.25
Selection.ShapeRange.Left = 108#
Selection.ShapeRange.Top = 90#
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 = _
wdRelativeVerticalPositionPage
Selection.ShapeRange.Left = InchesToPoints(8.68)
Selection.ShapeRange.Top = InchesToPoints(7.26)
Selection.ShapeRange.LockAnchor = False
Selection.ShapeRange.LayoutInCell = True
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
Selection.ShapeRange.TextFrame.AutoSize = False
Selection.ShapeRange.TextFrame.WordWrap = True
ActiveWindow.ActivePane.HorizontalPercentScrolled = 26
Selection.Orientation = wdTextOrientationDownward
ActiveWindow.ActivePane.HorizontalPercentScrolled = 31
ActiveWindow.ActivePane.HorizontalPercentScrolled = 0
End Sub

Is this possible? Thanks in advance for even reading this huge question! I?ve attached a sample of what we are doing.

fumei
11-07-2007, 03:25 PM
Good heavens.

1. please use the VBA code tags when you post code.

2. use With statements!

3. "However, I keep getting errors " Please, whenever you mention errors, always, always, state what the error is.

4. If I understand correctly, you are creating textboxes to use as headers and footers. Why not use....headers and footers? Is it because you want to have vertical text? Your attachment does not help at all, as it does not contain anything like what you are describing.

PJY
11-07-2007, 03:46 PM
I guess I am just not explaining this right because I've asked 4 others for help and they all said to use auto text! The end result I'm looking for is for the headers and footers on the landscape pages to be in the same position as the portrait pages. These reports are bound as if all the pages are in portrait, so what I have done in the past is printed the headers and footers that would appear on the landscape page on a portrait page then print my landscape page onto the already printed header & footer page. But we are doing more and more of these and we are starting to send them to the client in .pdf format so I can't really "fudge" it any longer. The error I rec'd was run-time error '424' object required.

OTWarrior
11-08-2007, 01:25 AM
do you mean have the header and footer always at the narrow ends of the page (as in portrait) when you change to landscape? or to have the header and footer the same size and in the middle of the top of the page in landscape?

if the header and footer will not change, why not take a screengrab and save it as a gif or png file (whichever works out smaller but still looks good) and paste it onto the document.

you can set the text to wrap around it with the format settings for the former idea, and for the later you could just position it where you wnated it.

hope that helps.

as for your error, put in some breakpoints in the code, then when it runs you can narrow down the line of code that is causing the error

TonyJollans
11-08-2007, 01:47 AM
I have to agree that the attachment is no help at all - it doesn't show anything relevant on the landscape page.

Your 'requirement' is no such thing; it is a proposed solution.

If I understand correctly, your requirement is to have normal footers (containing whatever) on portrait pages and to have no proper footers on landscape pages but to have something up the left hand side that will look sort of like a footer when the page is turned round. Can you confirm this?

PJY
11-08-2007, 07:02 AM
Hi all once again,
Yes Tony, you seem to understand. But I do see what you mean about my attachment. I am attaching a revised version. What I am trying to do is position the header & footer on the landscape page in the same spot it appears in the portrait page. In the attachment, the header & footer on page 2 in black font is where the text from the header & footer appears - I want it to appear where it is shown in purple. I hope it is a little clearer this time.
To OTWarrior - this is what I mean - have the header and footer always at the narrow ends of the page (as in portrait) when you change to landscape. I tried the gif, but the problem for me with that is these are reports and I use cross-reference and marked text for my TOC, so that wasn't a viable solution - although it did work to a certain extent.
Thanks so much for even taking the time to look at my problem.
PJY

TonyJollans
11-08-2007, 08:52 AM
Take a look at this Microsoft page (http://support.microsoft.com/kb/211930) to see if it helps.

Or this Word MVPs page (http://word.mvps.org/FAQs/Formatting/LandscapeSection.htm) may be easier to read and understand.

Whatever you do, each landscape part will need to be in its own section.

fumei
11-09-2007, 03:05 PM
And there ya go.

Oh...and AutoText would indeed be a good idea.

TonyJollans
11-09-2007, 04:22 PM
Oh...and AutoText would indeed be a good idea.
Agreed!