Text Boxes


ActiveDocument.Shapes.AddTextbox(Type:=msoTextOrientation.msoTextOrientationUpward , _ 
                               Left:=10, Top:=10, Width:=10, Height:=10, Anchor:=False)


For adding pictures and graphics, refer to Illustrations > VBA Code > Shapes


Reading Writing Text

When you read text from a textbox additional characters will be included in your text string.

Dim stext As String 
   stext = ActiveDocument.Shapes(1).TextFrame.TextRange.Text
   Debug.Print stext
   For ichar = 1 to Len(stext) Step 1
      Debug.Print Asc(Mid(stext, ichar,1)) & " - " & Mid(stext,ichar,1)
   Next ichar

Any spaces are represented by Chr(32)
At the end of the text there will also be a Chr(13)
To remove the carriage return at the end of the string you need to include the following:

stext = Repace(stext, Chr(13),"") 

Text boxes cannot have the same name if you are renaming using VBA
However testboxes with the same name can be inserted by inserting the same building block multiple times


The following line of code does not include any shapes in the headers or footers

ActiveDocument.Shapes 

Public Sub Page_GetTextboxPosition() 
Dim oShape As Word.Shape
   On Error GoTo ErrorHandler
   Set oShape = ActiveDocument.Shapes(1)
   Debug.Print "Left - " & oShape.Left
   Debug.Print "Top - " & oShape.Top
   Debug.Print "Width - " & oShape.Width
   Debug.Print "Height - " & oShape.Height
End Sub


Adds a text box to a drawing canvas.

Dim shpCanvas As Shape 
'Create a new document and add a drawing canvas
    Set shpCanvas = ActiveDocument.Shapes.AddCanvas _
    (Left:=10, Top:=10, Width:=150, Height:=200)
    shpCanvas.CanvasItems.AddTextbox _
         Orientation:=msoTextOrientationHorizontal, _
         Left:=40.45, Top:=129.3, Width:=129.5, Height:=205.2

Add the Title text box

    Set oShape = ActiveDocument.Shapes.AddTextbox _ 
         (Orientation:=msoTextOrientationHorizontal, _
         Left:=105, _
         Top:=213, _
         Width:=Application.CentimetersToPoints(13.55), _
         Height:=Application.CentimetersToPoints(1.74))

   oShape.Line.Weight = 1 
   With oShape.TextFrame.TextRange
      .Text = "Document Title"
      .ParagraphFormat.Alignment = wdAlignParagraphCenter
      .ParagraphFormat.Style = "Title"
   End With



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext