2

Hi MS Publisher VBA programmers :-)

What seems so simple is actually not documented. All I need to do is be able to insert text programmatically into a text box then have the text resize vertically expanding itself as I add more text.

This works fine if I do it manually by inserting TextBox and choosing the "Grow Text Box To Fit" option in the Text Fit drop-down in the Format tab of the TextBox.

This code doesn't work:

Sub myGrowToFit()
        Set myTextBox = ActiveDocument.Pages(1).Shapes.AddTextbox _
             (Orientation:=pbTextOrientationHorizontal, _
             Left:=0, Top:=0, _
             Width:=100, Height:=40)
        myTextBox.TextFrame.TextRange.text = "Line1" & vbLf & "Line2" & vbLf & "Line3" & vbLf & "Line4"
        myTextBox.TextFrame.AutoFitText = pbTextAutoFitGrowToFit
        MsgBox ("Press OK to Continue After You View The Result")
        myTextBox.Delete
End Sub

Any ideas? Thanks.

Mickey D
  • 347
  • 2
  • 12

1 Answers1

0

2021 and I wasn't able to get this to work for Office 365 ProPlus Publisher.

However, easy workaround for me was to use the .Overflowing property (doc). I just shrink the text until the Overflow property is equal to 0 which indicates it's not overflowing.

 Dim titleTextbox As Shape
 Set titleTextbox = ActiveDocument.Pages.Item(1).Shapes("My Textbox Name")
 
 Dim initialTextSize As Integer
 initialTextSize = titleTextbox.textFrame.TextRange.Font.Size

 While (titleTextbox.textFrame.Overflowing < 0)
    initialTextSize = initialTextSize - 1
    titleTextbox.textFrame.TextRange.Font.Size = initialTextSize
 Wend

Conversely, if you want to grow the text then increase the size in the while loop instead of decreasing it. Then once it overflows, exit loop and decrease the size by one.

Also, I wasn't able to find documentation for pbTextAutoFitGrowToFit, so if you have it please share if you wouldn't mind.

This resource I found, https://learn.microsoft.com/en-us/office/vba/api/publisher.pbtextautofittype, only shows three, pbTextAutoFitBestFit, pbTextAutoFitNone, pbTextAutoFitShrinkOnOverflow.

Hopefully someone figures out how to set the AutoFitText property one day.

sushi
  • 274
  • 1
  • 4
  • 13