API Screen Locking

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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
P

Peter T

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
 
C

Chip Pearson

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
 
P

Peter T

I believe Excel's HWnd property first became directly exposed in XL2002, and
in both XL97 & 2000 need to use FindWindow.

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top