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


Hi Karl, what are you doing over here... slumming? <g

I only do Excel (now), so I can't speak for the other Office products, but
you can find an Excel UserForm's hWnd using the FindWindow API. Here is a
working line from the UserForm's Initialize event from one of my previous
postings ...

hWnd = FindWindow("ThunderDFrame", Me.Caption)

I have no idea whether ThunderDFrame is usable elsewhere.

--
Rick (MVP - Excel)


"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