excel vba - VBA - Closing or clicking OK in MsgBox from another workbook, -
hi following code in excel vba,
sub () workbooks.open ("a.xls") activeworkbook.worksheets("1").select activesheet.commandbutton1.value = true end sub
i'm opening workbook (the code inside protected can't modify workbook "b") , clicking run button on worksheet, , returns msgbox ok button.
i want know how can use vba close msgbox or clicking "ok" ...i tried use,
`application.displayalert = false`
before opening workbook "b" not work..
thanks help!
there might approach using sendkeys
-- sendkeys
notoriously temperamental. here more reliable approach:
1) if don't have organized this, have click-event handler of button 1-line sub looks like:
private sub commandbutton1_click() process end sub
where process
sub heavy lifting. in general, think idea have event handlers functioning dispatchers subs.
2) change code process
(or whatever choose name it) in following way:
a) modify first line like
sub process(optional verbose boolean = true)
b) process
has like
msgbox "processed!"
replace by
if verbose msgbox "processed!"
3) in code gave above, replace line
activesheet.commandbutton1.value = true
by line
application.run "a.xls!process", false
this approach bypass button entirely , run code button triggers, run in silent mode.
on edit: use sendkeys
following. put line
application.sendkeys "~"
before line
activesheet.commandbutton1.value = true
~
character shortcut enter
. sendkeys
doesn't send keystroke, instead puts on windows message queue. vba doesn't have direct control when message processed. in case lack of control benefit. vba interpreter moves onto next line, triggers msgbox
. time sendkeys
message processed default okay
button on message box has focus hence receives enter key. can happen before box painted, making seem msgbox
never there -- better think of being destroyed before have time see it.
the reason why necessary have sendkeys
line before line clicks button once message box appears cause vba interpreter wait until closed -- hence calling code suspend execution until after message box closed, hence sendkeys
wouldn't processed until no longer needed.
i don't trust sendkeys
. suspect when run code happen a1
in newly activated sheet receive enter key (shifting selection a1
a2
) before message box appears. i'm not sure if can happen, if workaround might move sendkeys
vbscript program. launch program (with window minimized , not waiting return) before button clicked. vbscript program can have 0.5 second pause before uses sendkeys
. script running in different thread won't blocked message box.
Comments
Post a Comment