View Full Version : Accessing the hWnd Property of a dialog/form in VBA...?

06-24-2008, 09:23 AM
I have written a macro/project in Word VBA which uses a Directory
Search Dialog box based on the excellent article here:


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


Bob Phillips
06-24-2008, 09:46 AM
Is this a userform? If so, you get the windows handle like so

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

But doesn't Word support FileDialog, I stopped using that technique years ago.

06-25-2008, 05:38 AM
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


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...

Bob Phillips
06-25-2008, 09:51 AM
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

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Bob Phillips
06-25-2008, 09:54 AM
As I said though, if you use FileDialog, you don't need that complex Brwose routine, and no hWnd.

06-26-2008, 05:36 AM
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...)