getting correct text box height

K

Keith G Hicks

When I run the following code in word, the last line gives me a result of
0.2777778 inches. This is not correct actually. The text box is resizing and
is really 1.927083 inches tall. If I run this: ?Selection.ShapeRange.Height
/ 72 in the immediate window, AFTER the code below is run it shows the
correct height. Any ideas on how to get the code to really see the right
height? -Keith


Dim shpCanvas As Shape
Set shpCanvas =
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 20, 20, 138,
20)
shpCanvas.Name = "tbFNTNotice"

ActiveDocument.Shapes("tbFNTNotice").Select

'Selection.WholeStory
Selection.Font.Name = "Helvetica"
Selection.Font.Size = 7.5
Selection.TypeText Text:= _
"Peter Parker (Tobey Maguire) is so poor he is living in a tenement
room with a " _
& " mattress and a bare light bulb. He works as a pizza deliveryman
while taking " _
& " science classes at Columbia University. His sweet Aunt Mary
(Rosemary Harris) " _
& " is so broke she is losing her tiny house. My advice to Parker?
Take a mailroom " _
& " job at your friend's Harry Osborn (James Franco) monolithic
company. Or, better " _
& " yet, do an interview as Spider-Man with a tabloid for $100,000
in cash! Let " _
& " Aunt Mary live her last days in financial peace. Don't you owe
her something (for " _
& " being responsible for your uncle's death that left Aunt Mary
lonely and destitute)? "

With Selection.ShapeRange.TextFrame
.AutoSize = True
.WordWrap = True
.MarginTop = 0
.MarginRight = 0
.MarginBottom = 0
.MarginLeft = 0
End With

Debug.Print Selection.ShapeRange.Height / 72 '72 points = 1 inch
 
P

Peter Hewett

Hi Keith G Hicks

Your code works ok for me. The things I'd do differently is to create and use objects of
the appropriate type, rather than the selection object which makes the code very difficult
to read and maintain. Also, there's a whole bunch of built in methods to convert points to
other measurement systems, so I'd use:

Debug.Print Application.PointsToInches(Selection.ShapeRange.Height)

The actual size returned will vary depending on whether you are in Print Layout View or
Web View.

Cheers - Peter

When I run the following code in word, the last line gives me a result of
0.2777778 inches. This is not correct actually. The text box is resizing and
is really 1.927083 inches tall. If I run this: ?Selection.ShapeRange.Height
/ 72 in the immediate window, AFTER the code below is run it shows the
correct height. Any ideas on how to get the code to really see the right
height? -Keith


Dim shpCanvas As Shape
Set shpCanvas =
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 20, 20, 138,
20)
shpCanvas.Name = "tbFNTNotice"

ActiveDocument.Shapes("tbFNTNotice").Select

'Selection.WholeStory
Selection.Font.Name = "Helvetica"
Selection.Font.Size = 7.5
Selection.TypeText Text:= _
"Peter Parker (Tobey Maguire) is so poor he is living in a tenement
room with a " _
& " mattress and a bare light bulb. He works as a pizza deliveryman
while taking " _
& " science classes at Columbia University. His sweet Aunt Mary
(Rosemary Harris) " _
& " is so broke she is losing her tiny house. My advice to Parker?
Take a mailroom " _
& " job at your friend's Harry Osborn (James Franco) monolithic
company. Or, better " _
& " yet, do an interview as Spider-Man with a tabloid for $100,000
in cash! Let " _
& " Aunt Mary live her last days in financial peace. Don't you owe
her something (for " _
& " being responsible for your uncle's death that left Aunt Mary
lonely and destitute)? "

With Selection.ShapeRange.TextFrame
.AutoSize = True
.WordWrap = True
.MarginTop = 0
.MarginRight = 0
.MarginBottom = 0
.MarginLeft = 0
End With

Debug.Print Selection.ShapeRange.Height / 72 '72 points = 1 inch

HTH + Cheers - Peter
 
K

Keith G Hicks

Well, the funny numbers that I was getting are ok now. But there's a new
problem. The following code works fine in a word vba module but when I try
to run it inside of ms access, it crashes (error: "oject variable or with
block varialbe not set") on Selection.Font.Name = "Arial"

'word is opened, a new doc is added and activated prior to all of
this - it all works just fine.

'set up the text box in the word doc
Dim shpTextBox As Shape
Set shpTextBox =
docWord.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, 25, 138, 20)

shpTextBox.Select

With Selection
.Font.Name = "Arial"
.Font.Size = 7.5
.TypeText Text:=Me!FullNoticeText
End With


Keith
 
P

Peter Hewett

Hi Keith G Hicks

Your code works ok for me. The things I'd do differently is to create and use objects of
the appropriate type, rather than the selection object which makes the code very difficult
to read and maintain. Also, there's a whole bunch of built in methods to convert points to
other measurement systems, so I'd use:

Debug.Print Application.PointsToInches(Selection.ShapeRange.Height)

The actual size returned will vary depending on whether you are in Print Layout View or
Web View.

Cheers - Peter

When I run the following code in word, the last line gives me a result of
0.2777778 inches. This is not correct actually. The text box is resizing and
is really 1.927083 inches tall. If I run this: ?Selection.ShapeRange.Height
/ 72 in the immediate window, AFTER the code below is run it shows the
correct height. Any ideas on how to get the code to really see the right
height? -Keith


Dim shpCanvas As Shape
Set shpCanvas =
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 20, 20, 138,
20)
shpCanvas.Name = "tbFNTNotice"

ActiveDocument.Shapes("tbFNTNotice").Select

'Selection.WholeStory
Selection.Font.Name = "Helvetica"
Selection.Font.Size = 7.5
Selection.TypeText Text:= _
"Peter Parker (Tobey Maguire) is so poor he is living in a tenement
room with a " _
& " mattress and a bare light bulb. He works as a pizza deliveryman
while taking " _
& " science classes at Columbia University. His sweet Aunt Mary
(Rosemary Harris) " _
& " is so broke she is losing her tiny house. My advice to Parker?
Take a mailroom " _
& " job at your friend's Harry Osborn (James Franco) monolithic
company. Or, better " _
& " yet, do an interview as Spider-Man with a tabloid for $100,000
in cash! Let " _
& " Aunt Mary live her last days in financial peace. Don't you owe
her something (for " _
& " being responsible for your uncle's death that left Aunt Mary
lonely and destitute)? "

With Selection.ShapeRange.TextFrame
.AutoSize = True
.WordWrap = True
.MarginTop = 0
.MarginRight = 0
.MarginBottom = 0
.MarginLeft = 0
End With

Debug.Print Selection.ShapeRange.Height / 72 '72 points = 1 inch

HTH + Cheers - Peter
 
P

Peter Hewett

Hi Keith G Hicks

One of the reasons I suggested using explicit objects rather than the Selection object is
that when you declare and instantiate an explicit object as opposed to using the selection
object you can use VBA's intellisense feature. This make it obvious what properties and
methods a class/object exposes. See the rewrite:

Dim shpTextBox As Shape
Set shpTextBox = _
ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, 25, 138, 20)

With shpTextBox.TextFrame.TextRange
.Font.Name = "Arial"
.Font.Size = 7.5
.Text = "Dummy text for testing"
End With

Because the With block uses a final object (TextRange) of type Range VBA would pick up the
error of incorrect method/property references within the With block as soon as you try to
run the code rather than at run time. This is overall a much better approach.

HTH + Cheers - Peter


Well, the funny numbers that I was getting are ok now. But there's a new
problem. The following code works fine in a word vba module but when I try
to run it inside of ms access, it crashes (error: "oject variable or with
block varialbe not set") on Selection.Font.Name = "Arial"

'word is opened, a new doc is added and activated prior to all of
this - it all works just fine.

'set up the text box in the word doc
Dim shpTextBox As Shape
Set shpTextBox =
docWord.Shapes.AddTextbox(msoTextOrientationHorizontal, 25, 25, 138, 20)

shpTextBox.Select

With Selection
.Font.Name = "Arial"
.Font.Size = 7.5
.TypeText Text:=Me!FullNoticeText
End With


Keith

HTH + Cheers - Peter
 
K

Keith G Hicks

I noticed one small problem today. If I make word NOT visible when I run all
of this, then I don't get an accurate figure. I have to say objWord.Visible
= True prior to running all of it in order to come out correct. Is there
another way to force accurate measurement without showing word?

Thanks,

Keith
 
P

Peter Hewett

Hi Keith G Hicks

I haven't looked into this in depth, but it would not surprise me that the incorrect
information is returned as the objects size obviously varies depending on a large number
of setting, E.g.: Font, paragraph formatting, shape margins, etc. It's seems quite
possible that the object has to be rendered before the correct determination can occur.

Maybe a kludge is to keep Word hidden and then display it, grab the measurement and then
hide it again. This will probably cause a flicker (it depends what's on screen at the
time the code executed) but may be more acceptable than having Word always visible.

Sorry, that's about the best I can come up with at the moment. If I have any other ideas
I'll add to this thread.

HTH + Cheers - Peter
 
Top