Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word VBA Row/Cell .Height property - ?!?!?!?

Posted on 2003-12-05
10
Medium Priority
?
4,767 Views
Last Modified: 2007-12-19
Dear everyone,

Am I like the last loneliest idiot in the world or something? It appears that the Word (2000) VBA Row (or Cell) property .Height doesn't actually return the height of the Row (or Cell).

For example:

(assume I've created a single cell table)

?Word.ActiveDocument.Tables(1).Rows(1).Height

will NOT give me the HEIGHT of the FIRST ROW of the FIRST TABLE in the ACTIVE WORD DOCUMENT.

I dunno, I would have said I was pretty specific there...
Instead, the output is dependant on the value of a certain .HeightRule property. There are three possible values:
wdRowHeightAtLeast - Now the row has to be 'At least' .Height tall.
wdRowHeightAuto - The row height is determined by the amount of text therein.
wdRowHeightExactly - The row is exactly .Height tall (.Height is in points)

Here's the really good fun part. When .HeightRule is set to wdRowHeightExactly then .Height is, well, the height of the row. No probs. except we don't know how tall we want it do we, because that rather depends on whetehr the text will fit or not. Ok, next one. When .HeightRule is wdRowHeightAtLeast well now .Height returns the minimum height for the Row. How tall is the row actually? don't ask word, it hasn't got a clue. And finally, wdRowHeightAuto. The row height is determined automatically by the amount of text in it. and what does .Height return? 99999999 - or more specifically wdUndefined. GREAT. THANKYOU MICROSOFT.

So despite the fact that Word MUST know how big that damn Row is - it has to draw and print the damn thing doesn't it? - it WILL NOT tell you how big it is.

OK, I said, it's a little problem, but nothing a good workaround won't solve:

(MyRow as Row, PleaseWork as Long)

MyRow.HeightRule = wdRowHeightAuto
MyRow.Cells(1).Range.Text = "Horses"
MyRow.HeightRule = wdRowHeightExactly
PleaseWork = MyRow.Height

No such luck. Instead, the bit about wdRowHeightExactly actually RESIZED my bloody Row! So that whilst .Height is now accurate, the bottom of the text is chopped off in my Table.

So exactly what the hell is the point in having a .Height property that is readable if you can't actually learn anything from its use? Why not just have a .Height method? hmm? Why not finish the job and have it pop up a little window that says "Screw you programmer! Ha ha! - The boys at MS"

Or maybe it's me. I really hope so. Any ideas anyone? (BTW I'm not just ranting, I really need to sort this out, so beg, grovel etc etc)

=malc=

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 37

Assisted Solution

by:Joanne M. Orzech
Joanne M. Orzech earned 400 total points
ID: 9882733
Not sure if this will help but worth a shot:

http://www.experts-exchange.com/Applications/MS_Office/Q_10301103.html

0
 
LVL 9

Accepted Solution

by:
gilbar earned 400 total points
ID: 9883179
It's the same deal as when finding fontsize for a selection with deferent fontsizes, you get 99999999 returned. so here's my plan (which involves lots of loops)

gothrough each table
   gothrough each row
       gothrough each cell
          Select text, copy&paste to scratch document
          gothrough each character, selecting one at a time
          if Siz < Selection.font.size then Siz = Selection.font.size
        loop
    loop
    Siz should now be the Biggest fontsize in that row, which determined how high row is
    fancy calculation to figure how high a font size is
 loop

this is the best idea i can come up with, IF you go through with that I can help you flesh out the loops (if you need help, I'm guessing you don't).  Don't know if this is anyuse but you sounded desperate

0
 
LVL 9

Expert Comment

by:gilbar
ID: 9883195
I paste it into a scratch doc to help looping, when done with that cell, you'd delete the scratch doc& goto next cell
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
LVL 2
ID: 9883467
Thanks for your input guys, I eventually used a workaround in my prog, but I've just come up with this, which covers the general idea to some degree:

Sub test()

  Dim MyRange As Range
  Dim MyHeight As Long
  Dim RandomString As String
 
  'Some random text...
  Randomize Timer
  Do While (Int(Rnd * 30) > 0)
    RandomString = RandomString + "horses "
  Loop
   
  'The table...
  ThisDocument.Paragraphs(1).Range.Select
  ThisDocument.Tables.Add Selection.Range, 1, 1
  ThisDocument.Tables(1).Rows(1).Cells(1).Range.Text = RandomString
 
  'Take a sounding...
  ThisDocument.Tables(1).Select
  MyHeight = Selection.Range.Information(wdVerticalPositionRelativeToPage)
 
  'Then another...
  ThisDocument.Paragraphs(ThisDocument.Paragraphs.Count).Range.Select
 
  'Advanced algebraics...
  MyHeight = Selection.Range.Information(wdVerticalPositionRelativeToPage) - MyHeight
 
  'Display results...
  MsgBox (Str(MyHeight))
 
End Sub

I think one could probably use this principle to make it happen.

I'll split the points, since I'm not sure who's input led me here :)
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 9883482
You can give to gilbar - but thanks anyway :)
0
 
LVL 9

Expert Comment

by:gilbar
ID: 9883525
thanxs www (and thank you too JO)
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 9883538
Should I give you a "points for" question gilbar??  I'm not sure - can I give you my points?
0
 
LVL 9

Expert Comment

by:gilbar
ID: 9883595
nope, i'm good, you keep 'em (i'll never be able to catch you and dreamboat any way ;) , your link was as least as good as my simple idea
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 9883612
Thanks gilbar :)
0
 
LVL 2
ID: 9886487
hehe, gosh you guys are altruistic. kinda makes me feel all warm and fuzzy inside!

I had some problems dishing out the points this afternoon. EE seemed to be having some kinda probs...

I'll have another crack now...

=me=
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question