GetOpenFileName called from modal userform.. enables workbook windows. BUG?

K

keepITcool

Anyone knows a fix for this?
or should I use API's to disable the workbook windows?

New workbook, insert a userform with following code

Private Sub UserForm_Click()
Dim v
v = Application.GetOpenFilename
If v <> False Then Workbooks.Open v
End Sub

run the form. (MODAL)
click the form and open a workbook.

Excel menu;s are still disabled, but all workbook windows in the excel
desktop are now ENABLED!

Context menu's ARE available. try copy and paste somewhere in the newly
opened workbook. and excel will crash. (XP/ 2003)

(minimizing a workbook is ok. but if you close the
newly opened workbook via the "X button it crashed your form.
with an "automation error: invoked a disconnect" error.
 
P

Peter T

Hi KeepITcool,

You know very considerably more about API's than I, but I think I would
disable the application windows, rather than the workbook windows (btw how
?). In other words restore to a normal modal form state.

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

Private Declare Function EnableWindow Lib "user32" ( _
ByVal hwnd As Long, ByVal bEnable As Long) As Long

Private Sub UserForm_Click()
Dim v
Dim nHWind As Long

v = Application.GetOpenFilename
If v <> False Then Workbooks.Open v

AppActivate Me.Caption
nHWind = FindWindowA("XLMAIN", Application.Caption)
EnableWindow nHWind, 0&

End Sub

Regards,
Peter T
 
K

keepITcool

Peter,

thanx. so simple.

enablewindow for the application does the trick.
to refocus the form after the dialog i added the bringtotop.

in some cases it's preferable over hiding/showing the userform.
as the userform_activate event isnt triggered.


Option Explicit

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

Private Sub CommandButton1_Click()
Dim v, h&
v = Application.GetOpenFilename
If v <> False Then Workbooks.Open v
h = FindWindow(vbNullString, Application.Caption)
EnableWindow h, 0&
h = FindWindow(vbNullString, Me.Caption)
BringWindowToTop h
End Sub

Private Sub UserForm_Activate()
MsgBox "activated!"
End Sub


Private Sub CommandButton2_Click()
Dim v, h&
Me.Hide
v = Application.GetOpenFilename
If v <> False Then Workbooks.Open v
Me.Show
End Sub






--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Peter T wrote :
 
P

Peter T

Glad it worked.

Slightly off topic I notice you changed one, but not both of the API's to
Alias. Could you clarify which is preferable, or are they directly the
interchangeable in all situations.

FindWindowA Lib "user32"
vs
FindWindow Lib "user32.dll" Alias "FindWindowA"

EnableWindow Lib "user32"
vs
EnableWindow Lib "user32" Alias "EnableWindow"

and similar with other API's

TIA,
Peter T
 
K

keepITcool

just copied them from "ApiViewer 2004"

not too sure about specifics..

the name BEFORE the alias is used in code
the name AFTER alias is the actual dll.function called.

--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Peter T wrote :
 

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