Consulting

Results 1 to 6 of 6

Thread: Accessing the hWnd Property of a dialog/form in VBA...?

  1. #1
    VBAX Newbie
    Joined
    Jun 2008
    Location
    Manchester UK
    Posts
    3
    Location

    Question Accessing the hWnd Property of a dialog/form in VBA...?

    I have written a macro/project in Word VBA which uses a Directory
    Search Dialog box based on the excellent article here:

    (http//www(.)developer(.)com/net/vb/article(.)php/1541831

    remove brackets and paste into browser to view article

    Everything works great apart from the fact that I can't get the program to access the Me.hWnd property... I realise that the article program is designed for full VB rather than VBA, but if I use 0 (zero) instead of Me.hWnd then everything works, but I realise this is not the best way of doing things....

    Me.hWnd is representing the form within the project that calls the Directory
    Browser Dialog

    If there isn't a solution within VBA... what is the worst that can happen by
    leaving the value at 0...?

    Thanks in advance

    Ian

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is this a userform? If so, you get the windows handle like so

    [vba]

    With frm
    If Val(Application.Version) >= 9 Then
    hwnd = FindWindow("ThunderDFrame", .Caption)
    Else
    hwnd = FindWindow("ThunderXFrame", .Caption)
    End If
    [/vba]

    But doesn't Word support FileDialog, I stopped using that technique years ago.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Jun 2008
    Location
    Manchester UK
    Posts
    3
    Location

    Question

    Hi Thanks for the quick response... I have tried a couple of the options that you mentioned, however I have also taken a slight diversion. I am moving the project from Word to Excel as I think it is going to be better to publish the results in a spreadsheet rather than a word document. For Background info on what I am doing see below:

    • I am not sure what you mean by a user form, but it is a Form built in the VBA Editor that looks the attached jpg
    • I can't seem to get FindWindow() to work I just get Sub or Function not defined... do I need to add an additional reference to my project...?
    • I previously had my code split into 1 Form and 2 Modules (The Form, Directory Identification Module & Tree Navigation Module). I couldn't reference the Main Form from the module so i have pasted all of the code back into the Form Code area and will break it down later as necessary so I am now able to use "With Me" in place of you "With frm" to reference the Main Dialog window.
    Any further help would be much appreciated...

    Thanks in advance

    Ian

    Background Info:
    I am trying to write a macro within a spreadsheed that will start at the top of a project tree and scan through the subdirectories locating CAD drawing files (Microstation *.dgn files), opening them and validating some internal configuration settings (working units) and publishing them in a report so that we can identify how far some problems that we are finding are spread. That is why I need the Directory Browse dialog box rather than FileDialog approach.

    It has been about 5 years since I did any VB programming and is my first serious project in VBA ever so combined with a failing memory I am also trying to understand the differences between the two. I have no means of writing a full VB application at work due to restricted privilages, however that is the long term aim.

    Thanks for the patience and help...

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That looks like a Userform.

    As to the error, that's because I didn't give you the FindWindow API declaration, I left you something to do (joke!).

    Add this at the head of the declaratives section, after any Option statemen ts

    [vba]

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    As I said though, if you use FileDialog, you don't need that complex Brwose routine, and no hWnd.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Newbie
    Joined
    Jun 2008
    Location
    Manchester UK
    Posts
    3
    Location

    Talking Massive Thanks


    Woo Hooo... Thanks... it's working perfectly...

    Sorry for being so lazy... as you may be able to tell... this isn't my main day job... I am trying to prove a point and win some brownie points so I am squeezing bits in at lunchtime and after work... a slow painful process...

    Thanks for the help Xld (awesome...)

Posting Permissions

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