Vba userform control box code amendment needed.

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Code:
Private Sub UserForm_Initialize()
    
    Call CreateMenu
    Call StoreInitialControlMetrics
    
    'PostMessage hwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0
End Sub


Private Sub UserForm_Resize()
    Dim oCtrl As Control
    For Each oCtrl In Me.Controls
        With oCtrl
            If .Tag <> "" Then
                .Width = Split(.Tag, "*")(0) * ((Me.InsideWidth) / dInitWidth)
                .Left = Split(.Tag, "*")(1) * (Me.InsideWidth) / dInitWidth
                .Height = Split(.Tag, "*")(2) * (Me.InsideHeight) / dInitHeight
                .Top = Split(.Tag, "*")(3) * (Me.InsideHeight) / dInitHeight
                If HasFont(oCtrl) Then
                    .Font.Size = Split(.Tag, "*")(4) * (Me.InsideWidth) / dInitWidth
                End If
            End If
        End With
    Next
    Dim arr() As String
    If Len(sIntialColumnWidth) > 0 Then
        arr = Split(Replace(sIntialColumnWidth, "pt", ""), ";")
        For i = LBound(arr) To UBound(arr)
            arr(i) = arr(i) * PxToPt(CLng((Me.InsideWidth) / dInitWidth), False)
        Next i
    End If
    Me.Repaint
End Sub
Private Sub StoreInitialControlMetrics()
    Dim oCtrl As Control, dFontSize As Currency
    dInitWidth = Me.InsideWidth
    dInitHeight = Me.InsideHeight
    For Each oCtrl In Me.Controls
        With oCtrl
            On Error Resume Next
                dFontSize = IIf(HasFont(oCtrl), .Font.Size, 0)
            On Error GoTo 0
            .Tag = .Width & "*" & .Left & "*" & .Height & "*" & .Top & "*" & dFontSize
        End With
    Next
End Sub
 Private Sub CreateMenu()
    Call WindowFromAccessibleObject(Me, hwnd)
    lStyle = GetWindowLong(hwnd, GWL_STYLE)
    lStyle = lStyle Or WS_SYSMENU Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX Or WS_THICKFRAME
    SetWindowLong hwnd, GWL_STYLE, lStyle
    DrawMenuBar hwnd
End Sub
Private Function HasFont(ByVal oCtrl As Control) As Boolean
    Dim oFont As Object
    On Error Resume Next
    Set oFont = CallByName(oCtrl, "Font", VbGet)
    HasFont = Not oFont Is Nothing
End Function
Private Function ScreenDPI(bVert As Boolean) As Long
    Static lDPI(1), lDC
    If lDPI(0) = 0 Then
        lDC = GetDC(0)
        lDPI(0) = GetDeviceCaps(lDC, LOGPIXELSX)
        lDPI(1) = GetDeviceCaps(lDC, LOGPIXELSY)
        lDC = ReleaseDC(0, lDC)
    End If
    ScreenDPI = lDPI(Abs(bVert))
End Function
Private Function PxToPt(Points As Single, bVert As Boolean) As Long
    PxToPt = Points / (ScreenDPI(bVert) / POINTSPERINCH)
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''

Hello wonderful people,

I have this code here which was written for me by @Jaafar Tribak some time back.

At the time, I was on office 2016 so he wrote the code based on that version.

The code is a great one which works great and adjust all controls as well.

The only issue I am facing right now is that when I install office 2016 for the first time, it works good. But after I connect to Internet, the code fails to create the control boxes.

I have also tried it with other office versions and it fails to work.

If there is a way to handle this could someone please help me out?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It's failing perhaps because you're missing parts of the code.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top