The instructions say to create a class with the following code:
Option Explicit
' FormResize Class module
' Change this constant to True if you want to
' see error messages.
#Const DEBUGGING = False
' ==================================
' Windows API declarations.
' ==================================
Private Declare Function GetDesktopWindow _
Lib "user32" () As Long
Private Declare Function GetDeviceCaps _
Lib "gdi32" _
(ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function GetDC _
Lib "user32" _
(ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC _
Lib "user32" _
(ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Const HORZRES = 8 ' Horizontal width in pixels
Private Const VERTRES = 10 ' Vertical width in pixels
Private Const LOGPIXELSX = 88 ' Logical pixels/inch in X
Private Const LOGPIXELSY = 90 ' Logical pixels/inch in Y
Private Const TWIPSPERINCH = 1440
Private Const TWIPSPERPOINT = 20
Private Type POINTAPI
x As Long
y As Long
End Type
Private mptScreen As POINTAPI
Private mptScreenInPoints As POINTAPI
Public Sub RescaleForm( _
YourForm As Object, _
OriginalX As Long, _
OriginalY As Long, _
Optional CenterForm As Boolean = True)
' Called from the Initialize event of forms.
' Attempts to scale the form appropriately
' for the given screen size, as compared
' to the size screen on which it was designed.
Dim decScale As Variant
On Error GoTo HandleErrors
decScale = GetScreenScale(OriginalX, OriginalY)
' If you don't want forms to expand (they were created on a
' lower-resolution device than the current device), but only
' shrink (they were created on a higher-resolution device
' than the current device), then use the next line instead
' of the current If...Then line.
'If (decScale < 1) Then
If (decScale <> 1) Then
' Set Width, Height, and Zoom properties.
YourForm.Width = YourForm.Width * decScale
YourForm.Height = YourForm.Height * decScale
YourForm.Zoom = decScale * 100
' If you don't want to center the
' form (if you're about to move it
' somewhere else, for example) you
' can skip this code by setting the
' CenterForm parameter to be False.
If CenterForm Then
YourForm.Move _
(mptScreenInPoints.x - YourForm.Width) / 2, _
(mptScreenInPoints.y - YourForm.Height) / 2
End If
End If
ExitHere:
Exit Sub
HandleErrors:
Select Case Err.Number
Case Else
Call HandleError("FormResize.RescaleForm", _
Err.Number, Err.Description)
End Select
Resume ExitHere
End Sub
Private Function GetScreenScale( _
OriginalX As Long, OriginalY As Long) As Variant
Dim decFactorX As Variant
Dim decFactorY As Variant
On Error GoTo HandleErrors
' Fill in the mptScreen info.
Call GetScreenInfo
' Get the ratio of the current screen
' size to the design-time screen size.
decFactorX = CDec(mptScreen.x / OriginalX)
decFactorY = CDec(mptScreen.y / OriginalY)
' You only get one scaling factor on these
' forms. You could use the Max, or the
' average, or the min. I went with the
' Max value of the two.
GetScreenScale = Max(decFactorX, decFactorY)
ExitHere:
Exit Function
HandleErrors:
Select Case Err.Number
Case Else
Call HandleError("FormResize.GetScreenScale", _
Err.Number, Err.Description)
Resume ExitHere
End Select
End Function
Private Sub GetScreenInfo()
' This procedure fills in the module variables:
' mptScreen, mptScreenInPoints
On Error GoTo HandleErrors
Dim ptCurrentDPI As POINTAPI
Dim ptTwipsPerPixel As POINTAPI
Dim lngDC As Long
Dim hWnd As Long
On Error GoTo HandleErrors
hWnd = GetDesktopWindow()
lngDC = GetDC(hWnd)
' If the call to GetDC didn't fail (and it had
' better not, or things are really busted),
' then get the info.
If lngDC <> 0 Then
' How many pixels per logical inch?
With ptCurrentDPI
.x = GetDeviceCaps(lngDC, LOGPIXELSX)
.y = GetDeviceCaps(lngDC, LOGPIXELSY)
End With
' How many twips per pixel?
With ptTwipsPerPixel
.x = TWIPSPERINCH / ptCurrentDPI.x
.y = TWIPSPERINCH / ptCurrentDPI.y
End With
' What's the current screen resolution?
With mptScreen
.x = GetDeviceCaps(lngDC, HORZRES)
.y = GetDeviceCaps(lngDC, VERTRES)
End With
' What's the screen resolution in points? (For
' use when centering the form.)
With mptScreenInPoints
.x = mptScreen.x * ptTwipsPerPixel.x / TWIPSPERPOINT
.y = mptScreen.y * ptTwipsPerPixel.y / TWIPSPERPOINT
End With
' Release the information context.
Call ReleaseDC(hWnd, lngDC)
End If
ExitHere:
Exit Sub
HandleErrors:
Select Case Err.Number
Case Else
Call HandleError("FormResize.GetScreenInfo", _
Err.Number, Err.Description)
End Select
Resume ExitHere
End Sub
Private Function Max( _
varValue1 As Variant, varValue2 As Variant) As Variant
If varValue1 > varValue2 Then
Max = varValue1
Else
Max = varValue2
End If
End Function
Private Sub HandleError(strName As String, _
lngNumber As Long, strDescription As String)
#If DEBUGGING Then
MsgBox "Error: " & strDescription & _
" (" & lngNumber & "

", vbExclamation, strName
' Trigger a breakpoint. Remove this
' if you don't want a breakpoint here.
Debug.Assert False
#End If
End Sub
Then you are suppose to BEGIN QUOTE--Call the class' RescaleForm method from your form's Initialize event. The RescaleForm method takes four parameters:
· YourForm: A reference to your form. You cannot use the generic UserForm type here (which means you can't pass the built-in Me keyword); you must use the specific type for your form. This is unfortunate, but it's the way VBA forms work. You'll find a discussion of why you must pass the exact form reference later in this article.
· OriginalX: A long integer containing the original width of your screen when you designed the form. Generally, 640, 800, 1024, or 1280.
· OriginalY: A long integer containing the original height of your screen when you designed the form. Generally, 480, 600, 768, or 1024.
· CenterForm (optional, default True): A Boolean value indicating whether you want to have the code center the form for you. Normally, VBA forms open centered, and once you modify the size, the code must center the form manually. If you're going to position the form to some specific location, in the form's Activate event, you might set this to False to save a little time. No point centering the form and then moving it somewhere else.
For example, if you have a form named UserForm1 that you designed on a 1024x768 screen, add code like this to the form's Initialize event procedure:
With New FormResize
.RescaleForm UserForm1, 1024, 768
End With
END OF QUOTE
The following is from one of my forms:
Option Explicit
Dim frmCoupons As Form_frmCoupons
Private Sub Form_Initialize()
With New FormResize
.RescaleForm frmCoupons, 1024, 768
End With
End Sub
I'm not getting any errors, it just does not do what it is supposed to do which is change the size of the forms in relation to a change in resolution.
Sorry for the long post, but you asked for it.