Results 1 to 9 of 9
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Control handle (Word 97SR2)

    Does anyone know the trick to getting the hWnd handle for a control on a VBA userform?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Control handle (Word 97SR2)

    I imagine the same calls will work for VBA userforms as for Access forms, and you can find the API call and code for getting the HWnd for Access controls at <A target="_blank" HREF=http://www.mvps.org/access/api/api0027.htm>http://www.mvps.org/access/api/api0027.htm</A>. Of course the handle will probably be a lot more useful in Word than in Access. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Control handle (Word 97SR2)

    Does it have one? I guess I thought window handles were for windows (including child windows and dialogs). Are controls windows, or do they have window handles just because it's a useful thing for them to have?

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Control handle (Word 97SR2)

    Apparently when they get the focus they become windows.
    Charlotte

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Control handle (Word 97SR2)

    <P ID="edit" class=small>Edited by jscher2000 on 23-Oct-01 22:19.</P>My usual technique (see attached) doesn't work. I don't know the control's "window title"; it's name doesn't seem to be it.

    <img src=/w3timages/blueline.gif width=33% height=2><img src=/w3timages/blueline.gif width=33% height=2>

    I added a feature to list all the running windows and their parent (if any), and I did not find any windows that were child windows of the UserForm. I've hit a wall.
    Attached Files Attached Files

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Control handle (Word 97SR2)

    Jeff,

    Somewhere I've read that most forms controls are windows (good guess that combo boxes, listboxes, textboxes and command buttons are among them).
    VBA hides all the details of creating and managing these windows, and the fact that they're windows is not reflected in the VBE user environment - so Windows.Count won't catch these.

    Similarly in the Word user interface, there can be a number of ancillary windows existing at a given moment (displayed dialogs, userforms, popup menus) but these won't be reflected in an application windows count 'cause they're managed internally by the application.

    It seems that getting a handle for any of these windows is going to be a lot harder than a handle for an application window.....

    Gary

  7. #7
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Control handle (Word 97SR2)

    This will only work for controls that can accept the focus.

    Put this is your Module
    Public Declare Function GetFocus Lib "user32" Alias "GetFocus"() As Long


    Then.

    Control.SetFocus
    hWnd=GetFocus()

    Although my documentation says this returns handle of the window that has the input focus I have successfully used this on Frames and Pictures.

    Unfortunately if the control won't accept the focus you are SOL.

    Regards,

    Kevin Bell

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Control handle (Word 97SR2)

    Charlotte, Big Kev, et.al:

    If the GetFocus API call works, I'll be most grateful.

    I should chime in on this lively discussion to mention my dilemma and the reason I asked this question.

    Combo boxes on userforms with the StartupPosistion property set to "CenterOwner" will display the contents of the list -- using the "Dropdown" method -- in the upper left corner of the application window NOT directly under the combox field. This problem has plagued my apps for years and I cound never find an answer, except force the user to click the combobox (even when the combo is the only control on a form). I'm hoping that by getting the hWnd of the combobox I can use SendMessage to programmatically open the list properly. Or maybe once the focus is set via the API, maybe .dropdown will work right.

    If there is a no-brainer answer as to why the list portion of the combobox does not drop down correctly, lay it on me.

    I've seen weird window behavior in other ways too. In Word VBA set screenupdating to false in the initialize event for a form. Then display the form. Use your mouse to drag the form around the screen. Do you get "form trails"?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Control handle (Word 97SR2)

    Kevin,

    Please see my response to Charlotte above... and thanks..
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

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