使用VBA自动单击消息框 [英] Auto clicking on a message box using VBA

查看:186
本文介绍了使用VBA自动单击消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我公司的插件自动化测试用例.

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.

我尝试使用sendKeys,但是对我来说不起作用.

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

例如:为了在我的VBA代码中单击否",我尝试发送标签并输入如下键:

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.

提前谢谢! :)

更新:到目前为止,我一直在这样做:

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?

注意::通过放弃/提交对数据库的更改,我的意思是在其中更新数据的服务器. Excel只是用户的界面.我不希望只保存excel,而是还要在服务器上调用discard/save的操作也会发生变化...

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...

推荐答案

如果您已经保存了工作簿,则只需要关闭工作簿即可.请尝试以下代码

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.

这篇关于使用VBA自动单击消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆