API Screen Locking

  • Thread starter Thread starter Paul W Smith
  • Start date Start date
P

Paul W Smith

I have seen code in the past which uses an windows API to lock the screen

I found the code below which apparently does this.

However it does not work or more accurately the LockWindowUpdate
ThisApplication.MainFrameHWND line gives a "Object does not support this
property or method" error.

How do I correct this?


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Paste the following declaration at the top of your module:

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As
Long) As Long

And then use these lines of code in your macro to lock and unlock screen
updates:

' Locks window updates
LockWindowUpdate ThisApplication.MainFrameHWND

' Unlocks window updates
LockWindowUpdate 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
simply
pplication.hWnd in xl2002 or later,

in earlier and/or all versions

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

sub test()
dim nHwnd as long
nHwnd = FindWindow("XLMAIN", Application.Caption)
end sub

In passing, unless you really know what you are doing and have cast iron
error handling I strongly recommend you don't use LockWindowUpdate,
particularly if distributing.

Regards,
Peter T
 
nHwnd = FindWindow("XLMAIN", Application.Caption)

Since XL 2000, the Application object exposes the HWnd of the XLMAIN
window, so FindWindow is not necessary.

Dim L As Long
L = Application.HWnd

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com




simply
pplication.hWnd in xl2002 or later,

in earlier and/or all versions

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

sub test()
dim nHwnd as long
nHwnd = FindWindow("XLMAIN", Application.Caption)
end sub

In passing, unless you really know what you are doing and have cast iron
error handling I strongly recommend you don't use LockWindowUpdate,
particularly if distributing.

Regards,
Peter T
Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
 
I believe Excel's HWnd property first became directly exposed in XL2002, and
in both XL97 & 2000 need to use FindWindow.

Regards,
Peter T
 
Back
Top