确定现有的Outlook实例是否打开 [英] Determining whether an existing Outlook instance is open

查看:170
本文介绍了确定现有的Outlook实例是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读如何使用自动化发送邮件后,我不清楚是否有可能避免打开一个新的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 对象Intellisense。

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任务管理器中查看,您将看到该进程正在运行,但它只是不显示在屏幕上。不幸的是,微软的一些精湛的工程师决定,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屋!

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