When I lock the Word app window with the code below, the lock does not hold
unless I put a breakpoint on, say, the With appWord statement following the
commented out Sleep statement.
Since it worked using a breakpoint, I ASSuMEd that I could use Sleep to
achieve the same effect. Well, I tried Sleep 20000 and that did no good.
What do I have to do to make sure the Lock works?
The code will evetually be part of a .exe or .dll, so cannot rely on manual
breakpoint.
I am running the code on VB 6, but I expect that the locking issue would occur no matter the host app.
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long Private Declare Function GetWindowText Lib "user32" _ Alias "GetWindowTextA" _ (ByVal hWnd As Long, _ ByVal lpString As String, _ ByVal cch As Long) As Long Private Declare Function GetActiveWindow Lib "user32" () As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub btnByeBye_Click() Unload Me End Sub Private Sub btnNoTemplate_Click() Dim appWord As Word.Application Dim docWord As Word.Document Dim lngHWnd As Long Dim lngReturn As Long Dim strCaption As String Set appWord = New Word.Application lngHWnd = FindWindow(vbNullString, "Microsoft Word") If lngHWnd = 0 Then Debug.Print "Not found" appWord.Quit Set appWord = Nothing Unload Me Exit Sub Else Debug.Print "Handle is" & Str(lngHWnd) Debug.Print "Caption is: " & ActiveWindowCaption(lngHWnd) lngReturn = LockWindowUpdate(lngHWnd) If lngReturn = 0 Then Debug.Print "Lock failed" appWord.Quit Set appWord = Nothing Unload Me Exit Sub Else Debug.Print "Lock success:" & Str(lngReturn) & Str(lngHWnd) End If End If ' Sleep 1000 With appWord .ScreenUpdating = False .Visible = False .WindowState = wdWindowStateMinimize ' Blob appears Set docWord = .Documents.Add(Visible:=False) With docWord ' Makes blob disappear .ActiveWindow.Visible = False ' Do stuff here .Close End With ' Blob appears, then goes away .Quit End With Set docWord = Nothing Set appWord = Nothing End Sub Private Function ActiveWindowCaption(Optional hWnd = -1) As String ' Return caption for active window. Dim strCaption As String Dim lngLen As Long If hWnd = -1 Then hWnd = GetActiveWindow() End If strCaption = String$(255, vbNullChar) lngLen = Len(strCaption) If (GetWindowText(hWnd, strCaption, lngLen) > 0) Then ActiveWindowCaption = strCaption Else ActiveWindowCaption = "" End If End Function



Reply With Quote