View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba,microsoft.public.word.vba.general
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Best Practice for Obtaining hWnd

Well hello again Karl!

As Rick says it's -
hWnd = FindWindow("ThunderDFrame", Me.Caption)

That's good for Excel 2000+ and AFAIK all Office 2000 or later with VBA6.

If you want to cater for anyone still using Office97 (most don't but I do) -

If Val(Application.Version) = 9 Then
sClassName = "ThunderDFrame"
Else
sClassName = "ThunderXFrame"
End If

instead of checking the app version you could do
#If VBA6 ... #Else If ... #End IF
but will need a bit more for 2010/64bit

If there's any possibility a form with a similar caption could be showing
elsewhere, even in another instance, best to temporarily change the caption
to something unique.

Regards,
Peter T


"Karl E. Peterson" wrote in message
...
Hi Folks --

Is there any sort of agreed upon "best practice" for obtaining the hWnd of
VBA UserForms? Something that'll pretty much work wherever VBA may be
found? I seem to generally use some variation of this:

Option Explicit

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private hWnd As Long

Private Sub UserForm_Activate()
hWnd = GetForegroundWindow()
Debug.Print Hex$(hWnd)
End Sub

But I'm pretty much only working in either Word or Excel, and version 2003
at that. Any reason to think that wouldn't work in PowerPoint, Access,
2007, 2000, elsewhere, ...? Is there a better (more universal) way?

Thanks... Karl
--
.NET: It's About Trust!
http://vfred.mvps.org