+ Reply to Thread
Results 1 to 2 of 2

VBA & API - Strange problem using

  1. #1
    Philip
    Guest

    VBA & API - Strange problem using

    Hi all,

    I have a 'Please wait' form that shows a progress bar, and messages to the
    user. I am using the below code to hide the title-bar on window Initialise:

    >>> CODE>>>>

    Private Sub UserForm_Initialize()

    lFrmHdl = FindWindowA(vbNullString, Me.Caption)
    ShowTitleBar False

    Me.Show vbModeless

    End Sub

    Public Function ShowTitleBar(ByVal bState As Boolean)
    Dim lStyle As Long
    Dim tR As RECT

    '// Get the window's position:
    GetWindowRect lFrmHdl, tR

    '// Modify whether title bar will be visible:
    lStyle = GetWindowLong(lFrmHdl, GWL_STYLE)
    '
    If Not bState Then
    lStyle = lStyle And Not WS_SYSMENU
    lStyle = lStyle And Not WS_MAXIMIZEBOX
    lStyle = lStyle And Not WS_MINIMIZEBOX
    lStyle = lStyle And Not WS_CAPTION
    blnTitleVisible = True
    Else
    lStyle = lStyle Or WS_SYSMENU
    lStyle = lStyle Or WS_MAXIMIZEBOX
    lStyle = lStyle Or WS_MINIMIZEBOX
    lStyle = lStyle Or WS_CAPTION
    blnTitleVisible = False
    End If

    SetWindowLong lFrmHdl, GWL_STYLE, lStyle

    '// Ensure the style takes and make the window the
    '// same size, regardless that the title bar
    '// is now a different size:
    SetWindowPos lFrmHdl, 0, tR.Left, tR.Top, tR.Right - tR.Left, tR.Bottom -
    tR.Top, _
    SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED

    End Function

    <<< END CODE <<<<

    My problem is, somewhere there must be a memory leak or something because I
    get an 'The object invoked has disconnected from it's clients' error followed
    by a crash of Excel...

    The strange thing is, this all works fine with my splash form, it's just
    with the wait form that runs during the macro that there is a problem.

    Is there anyway to ignore that error?

    thanks

    Philip

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Phillip,

    You have violated one of the most basic API rules - Never Store A Window Handle in a Variable!!!

    The reason is window handles are recycled as threads and processes are created and destroyed. To use the API sucessfully requires dilligence to detail. This is a very powerful platform and not VB or VBA which is designed to protect you from yourself. The API has virtually no safeguards, which can lead to major system corruption when it fails.

    The VBA in Office 2000 and later provide you with the window handle property for the UserForm, no API call needed.

    To Get the Window Handle of a Form:
    UserForm1.Hwnd or Me.Hwnd

    The best advice about using the API is, If you don't know what it does and what it effects DON"T USE IT.

    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1