How to resize a TextFrame

767 Views Asked by At

I have a 1000 page document in a MS Publisher..

I have to resize every TextFrame. There are approximately four per page.

My Macro looks like this:

Sub Resize_Textbox()
Dim pubPage As Page
Dim pubShape As Shape
For Each pubPage In ActiveDocument.Pages
    For Each pubShape In pubPage.Shapes
        If pubShape.Type = pbTextFrame Then
            pubShape.TextFrame.Height = "21.5 cm"
            If pubShape.TextFrame.Width = "18 cm" Then
                pubShape.TextFrame.Width = "12.6 cm"
            End If
            If pubShape.TextFrame.Width = "8.75 cm" Then
                pubShape.TextFrame.Width = "6.3 cm"
            End If
        End If
    Next pubShape
Next pubPage
End Sub

I checked Macro Security and have enabled all Macros. I saved everything.

It gives me an error in line where I want to change the height of the textframe.

2

There are 2 best solutions below

2
Gary Evans On

In VBA, the height and width properties are done in points not literal cm, you can use CentimetersToPoints to convert it to the required value.

pubShape.TextFrame.Height = CentimetersToPoints(21.5)

Do not include the " cm" suffix.

EDIT\ADDITIONAL:

I run a test and did not get the error your getting, the difference being that I was working with declared variables. Your error message would usually imply we need set = before it or that we are referencing something that isn't there.

Try the below in your method and see if it works (Note this sample only does page one): -

Public Sub test()
Dim pg As Page
Dim shp As Shape

Set pg = ThisDocument.Pages(1)
    For Each shp In pg.Shapes
        If shp.Type = pbTextFrame Then
            shp.Height = CentimetersToPoints(5)
        End If
    Next
Set pg = Nothing

End Sub
0
Toma On

I realized what I did wrong. I just had to do it like this:

pubShape.Height = ...

Without TextFrame in the middle.