VBA-错误438对象不支持...命令按钮的CenterAlign标题

时间:2019-10-02 05:42:37

标签: excel vba

下面的屏幕快照显示错误438“对象不支持属性...”。我的代码如下:目的是使Userform1上所有命令按钮的标题/文本居中对齐。为什么我收到此错误?如何解决?

Option Explicit

         '<<<<<<<<<<< Adapted from http://www.freevbcode.com/ShowCode.asp?ID=330

#If VBA7 Then
      Private Declare PtrSafe Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
      Private Declare PtrSafe Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
#Else
      Private Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
      Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
#End If

'--------------------------------------------

Public Enum BUTTON_STYLE
    BS_CENTER = &H300&
    BS_LEFT = &H100&
    BS_RIGHT = &H200&
    BS_TOP = &H400&
End Enum

'--------------------------------------------

Private Const GWL_STYLE& = (-16)

Public Sub AlignCommandButtonText(Button As Object, Style As BUTTON_STYLE)
      Dim lHwnd As Long
      On Error Resume Next
      lHwnd = Button.hwnd
      Dim lWnd As Long
      Dim lRet As Long
      If lHwnd = 0 Then Exit Sub
      lWnd = GetWindowLong(lHwnd, GWL_STYLE)
      lRet = SetWindowLong(Command1.hwnd, GWL_STYLE, Style Or lWnd)
      Button.Refresh
End Sub


'--------------------------------------------

Public Sub CenterAlignButtons()
    For Each control_Object In UserForm1.Controls
       If TypeName(control_Object) = "CommandButton" Then
         Set Button = control_Object
         Button.Refresh        '<<<<<<< This method of calling is wrong, causing the error...?
         DoEvents
       End If
    Next
End Sub

'--------------------------------------------

Error 438...

Before running language code

After running language code

0 个答案:

没有答案