Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the units for form widths and heights in VBA?

I'm programming VBA for Word 2007.

I've created a UserForm which I need to resize with script.

I noticed that its not pixels

 Me.Width = pixelW   // form appears about 20% larger than the pixel width

And its not twips either

 Me.Width = (((1 / TwipsPerPixelX()) * pixelW)) / 1)  // form appears very small

So how are form widths and heights measured in Office 2007 VBA?

like image 646
Robin Rodricks Avatar asked Feb 28 '10 13:02

Robin Rodricks


People also ask

How do I change column width in macro?

You can set column width in a macro in Excel by using the ColumnWidth property of the Range object. The following example sets the width of column A to 12 characters. If you want to set the width of multiple columns, you can use a loop.


2 Answers

When manipulating controls on forms through code the sizes are in twips by default (I believe if you change the form's ScaleMode property you can choose to use another unit).

Your conversion formula is wrong (it's easy to get wrong). Try this, wrapped in a function to avoid code duplication with potential for typos in every duplicate

Function nTwipsFromPixelsX(ByVal nPixels As Long) As Long
  nTwipsFromPixels = TwipsPerPixelX() * nPixels
End Function
like image 75
MarkJ Avatar answered Sep 27 '22 20:09

MarkJ


When using VBA for Access, you usually express sizes in twips and/or centimeters, depending if you manage them from the user interface (centimeters) or from code (twips). As an example, you could set the column size for a combobox control in centimeters (by entering the corresponding values in the control's properties when the form is in design mode) or in twips (by updating these values from code while the form is open).

I guess this will also apply to VBA for Word. I'd advise you to write down both cmToTwips and TwipsToCm functions to make your measure conversions

like image 31
Philippe Grondier Avatar answered Sep 27 '22 20:09

Philippe Grondier