确定现有 Outlook 实例是否打开 [英] Determining whether an existing Outlook instance is open
问题描述
在阅读如何使用自动化发送消息后,我不清楚如果我已经打开了一个新的 Outlook 实例,是否可以避免打开一个新实例.如果是这样,我不确定如何搜索确定现有 Outlook 实例是否打开的示例.
After reading how to use automation to send a message, I'm unclear of whether it's possible to avoid opening a new instance of Outlook if I already have one opened. If so, I'm unsure of how to search for examples determining whether an existing Outlook instance is open.
-----包括建议--------
-----Including the suggestion--------
我有以下代码段,但我发现我无法正确创建实例.我基本上遵循 这个例子.我要么收到 此屏幕截图,要么出现以下错误未定义用户定义的类型."有什么建议吗?
I have the following snippet, but I found that I can't create the instance properly. I'm basically following this example. I'm either getting this screenshot, or the error of "User-defined type not defined." Any suggestions?
Sub Example()
'Dim w As Outlook.Application
Const ERR_APP_NOTRUNNING As Long = 429
On Error Resume Next
' Handle Microsoft outlook
Set w = GetObject(, "Outlook.Application")
If Err = ERR_APP_NOTRUNNING Then
'Set w = New Outlook.Application
Set w = CreateObject("Outlook.Application")
End If
End Sub
推荐答案
我在你的问题中看到你注释掉了
I see in your question that you commented out
'Dim w As Outlook.Application
大概是因为这会给您未定义用户定义的类型"错误.
presumably because this gives you the "User-defined type not defined" error.
这可能是因为您尚未在 Excel-VBA 项目中设置对 Outlook 库的引用.这按如下方式完成:工具 > 参考 > 选中Microsoft Outlook xx.x 对象库".那你可以写这个
This is likely because you have not set a reference to the Outlook library in your Excel-VBA project. This is done as follows: Tools > References > check "Microsoft Outlook xx.x Object Library". Then you can write this
Dim w As Outlook.Application
Set w = New Outlook.Application
' or,
'Set w = CreateObject("Outlook.Application")
顺便说一下,这会导致编译时(或早期")绑定.并为您提供 Outlook
对象智能感知.
which, by the way, results in compile-time (or "early") binding. And gives you the Outlook
object intellisense.
或者,您可以省略设置引用并将 w
声明为通用对象并让它在运行时绑定
Alternatively, you can omit setting the reference and declare w
as a generic object and let it bind at run-time
Dim w As Object
Set w = CreateObject("Outlook.Application")
但运行时(或后期")绑定效率较低.
做任何感觉最好的事情——我会继续冒险,很有可能,你不会注意到效率上的差异.我最近才改用早期绑定的东西,真的只是因为智能感知.
Do whatever feels best -- I'm going to go ahead and venture that chances are, you won't notice the difference in efficency. I'm a recent convert to the early-binding thing, really just because of the intellisense.
编辑 所以您已经创建了一个新的 Outlook 应用程序,但您看不到它.如果您查看 Windows 任务管理器,您会看到该进程在那里,正在运行——但它只是没有显示在屏幕上.不幸的是,Microsoft 的一些杰出工程师认为 Outlook 不应该像 Word 或 Excel 那样具有 Visible
属性,因此我们不得不使用一种笨拙的解决方法.打开一个特殊文件夹,例如收件箱如下:
EDIT So you've created a new Outlook application, but you can't see it. If you look in the Windows task manager, you'll see that the process is there, running -- but it's just not showing on the screen. Unfortunately, some brilliant engineer at Microsoft decided that Outlook shouldn't have a Visible
property like Word or Excel do, so we have to use an awkward workaround. Open one of the special folders e.g. the Inbox like this:
Dim w As Outlook.Application
Dim wInbox As Outlook.MAPIFolder
Set w = New Outlook.Application
Set wInbox = w.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
wInbox.Display 'This makes Outlook visible
这篇关于确定现有 Outlook 实例是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!