从 Excel 调用 Outlook VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5096353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call outlook VBA from Excel
提问by loveforvdubs
I have a function in the outlook VBA that I want to call when a certain excel workbook is closed. Is there a way to do this with the BeforeClose event of excel? I know how to write functions for this event, but I am not sure how to link them to the current outlook session to get to the function.
我在 Outlook VBA 中有一个函数,我想在某个 excel 工作簿关闭时调用它。有没有办法用excel的BeforeClose事件来做到这一点?我知道如何为此事件编写函数,但我不确定如何将它们链接到当前的 Outlook 会话以访问该函数。
回答by i_saw_drones
If you wish to get hold of a reference to an instanceof Outlook that is already running, you will need to use:
如果您希望获得对已在运行的 Outlook实例的引用,则需要使用:
Set myOutlookApp = GetObject(,"Outlook.Application")
which will give you access to the Outlook application object so you can call your desired VBA function in Outlook:
这将使您能够访问 Outlook 应用程序对象,以便您可以在 Outlook 中调用所需的 VBA 函数:
myOutlookApp.MyFunctionToExecute()
You'll probably need to make the function Public
otherwise Excel's VBA might not be able to see it.
您可能需要创建该函数,Public
否则 Excel 的 VBA 可能无法看到它。
回答by chris neilsen
MS Office applications can interact with each other by this method (this is based on Office 2007, but others will be similar):
MS Office 应用程序可以通过这种方法相互交互(这是基于 Office 2007,但其他的将类似):
Add a reference to the app into Excel
将对该应用程序的引用添加到 Excel 中
In Excel VBA, from the Tools\References menu select Microsoft Outlook 12.0 Object Library
在 Excel VBA 中,从 Tools\References 菜单中选择 Microsoft Outlook 12.0 Object Library
In your BeforeClose Event include
在您的 BeforeClose 事件中包括
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
You can now access Outlook through the olApp object. I don't know much about the Outlook object model, so others may be able help more from here on...
您现在可以通过 olApp 对象访问 Outlook。我不太了解 Outlook 对象模型,所以其他人可能会从这里开始提供更多帮助......