使用MsgBox从不同文档上的宏调用函数

时间:2022-03-17 06:28:47

Im calling a macro from another macro and it is working well im using

我从另一个宏调用一个宏,它使用得很好

Application.Run("'name of the file'!function to call")

But the problem is that the function i need to call have a MsgBox and it expect the user to give the OK.

但问题是我需要调用的函数有一个MsgBox,它希望用户给出OK。

How can i send the OK by default? i cant change the original code.

我如何默认发送OK?我无法更改原始代码。

Thanks for all your help

感谢你的帮助

1 个解决方案

#1


1  

Anything is possible... but you probably don't want to go there.

一切皆有可能......但你可能不想去那里。

Rubberduck (a VBE add-in OSS project I manage) uses EasyHook to hook into vbe7.dll and intercept calls to rtcMsgBox, the internal function that gets called when VBA code invokes the MsgBox function.

Rubberduck(我管理的VBE加载项OSS项目)使用EasyHook挂钩到vbe7.dll并拦截对rtcMsgBox的调用,rtcMsgBox是在VBA代码调用MsgBox函数时调用的内部函数。

This allows Rubberduck unit tests to setup a return value for the MsgBox function, without ever displaying an actual message box:

这允许Rubberduck单元测试为MsgBox函数设置返回值,而不会显示实际的消息框:

Fakes.MsgBox.Returns vbOk
DoSomething ' procedure under test, invokes a MsgBox that needs to say "Ok"

The hook is only enabled while a Rubberduck unit test is running though, so you couldn't use this API for what you need - but Rubberduck is open-source, and you could fork it and tweak it (it's written in C#) so that its Fakes API can be used in production code via a modified API that lets the client VBA code toggle the hook on and off.

挂钩仅在Rubberduck单元测试运行时启用,因此您无法使用此API来满足您的需求 - 但是Rubberduck是开源的,您可以将其分叉并进行调整(它是用C#编写的),以便它的Fakes API可以通过修改后的API在生产代码中使用,该API允许客户端VBA代码切换钩子的开关。

Or you could write some fiendish Win32 API code and implement something similar.

或者你可以编写一些恶魔Win32 API代码并实现类似的东西。

Good luck!

祝你好运!

#1


1  

Anything is possible... but you probably don't want to go there.

一切皆有可能......但你可能不想去那里。

Rubberduck (a VBE add-in OSS project I manage) uses EasyHook to hook into vbe7.dll and intercept calls to rtcMsgBox, the internal function that gets called when VBA code invokes the MsgBox function.

Rubberduck(我管理的VBE加载项OSS项目)使用EasyHook挂钩到vbe7.dll并拦截对rtcMsgBox的调用,rtcMsgBox是在VBA代码调用MsgBox函数时调用的内部函数。

This allows Rubberduck unit tests to setup a return value for the MsgBox function, without ever displaying an actual message box:

这允许Rubberduck单元测试为MsgBox函数设置返回值,而不会显示实际的消息框:

Fakes.MsgBox.Returns vbOk
DoSomething ' procedure under test, invokes a MsgBox that needs to say "Ok"

The hook is only enabled while a Rubberduck unit test is running though, so you couldn't use this API for what you need - but Rubberduck is open-source, and you could fork it and tweak it (it's written in C#) so that its Fakes API can be used in production code via a modified API that lets the client VBA code toggle the hook on and off.

挂钩仅在Rubberduck单元测试运行时启用,因此您无法使用此API来满足您的需求 - 但是Rubberduck是开源的,您可以将其分叉并进行调整(它是用C#编写的),以便它的Fakes API可以通过修改后的API在生产代码中使用,该API允许客户端VBA代码切换钩子的开关。

Or you could write some fiendish Win32 API code and implement something similar.

或者你可以编写一些恶魔Win32 API代码并实现类似的东西。

Good luck!

祝你好运!