Auto clicking on a message box using VBA

落爺英雄遲暮 提交于 2020-07-10 06:01:27

问题


I am automating testcases for my company's addin.

In one of the testcases, after saving my workbook on my local machine when I try closing the workbook, I am required to click on 'Yes'- saves edited data in our database, 'No'- discards the edits or 'cancel'- simply closes the message box.

I have tried using sendKeys but that didn't work for me.

Eg: For clicking 'No' through my VBA code, I tried to send tab and enter keys like this:

Application.Wait (Now() + TimeValue("00:00:10"))
Call SendKeys("{TAB}", True)
Call SendKeys("{ENTER}", True)

I have tried with various time values still didn't get any success. I would be glad if someone can help me with this.

Thanks in advance! :)

UPDATE: As of now, I have been doing this:

'Some tasks...
ActiveWorkbook.Save
Call Application.Run("workbook.xlsm!Discard")
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=False
Application.DisplayAlerts = True

But is there any way to get the window object reference for the message box? Or anyway to stick to the workflow: edit workbook -> save -> try closing -> opt to discard/commit changes to database?

NOTE: By discarding/committing changes to database, what I mean is a server where the data gets updated. Excel is just an interface for the user. I don't want just the excel to be saved but the operation of calling discard/save changes on server as well...


回答1:


if you have already saved the workbook, you only need to close the workbook. Please try the below code

Activeworkbook.close false

This will close your workbook and you wont be promted for selecting any options.



来源:https://stackoverflow.com/questions/35549410/auto-clicking-on-a-message-box-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!