vba 如何用VBA打开Outlook

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33328314/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:03:19  来源:igfitidea点击:

How to open outlook with VBA

vbaoutlook

提问by vicky vent

I like to open Outlook with VBA. It should check if outlook is open and if not then it should open it. I have code but its to big and some times dont work with other macros with Call function. What should be the simple and short code to do this and work with all versions?

我喜欢用 VBA 打开 Outlook。它应该检查 Outlook 是否打开,如果没有,则应该打开它。我有代码,但它很大,有时不能与具有 Call 函数的其他宏一起使用。执行此操作并适用于所有版本的简单而简短的代码应该是什么?

#Const LateBind = True

Const olMinimized As Long = 1
Const olMaximized As Long = 2
Const olFolderInbox As Long = 6

#If LateBind Then

Public Function OutlookApp( _
    Optional WindowState As Long = olMinimized, _
    Optional ReleaseIt As Boolean = False _
    ) As Object
    Static o As Object
#Else
Public Function OutlookApp( _
    Optional WindowState As outlook.OlWindowState = olMinimized, _
    Optional ReleaseIt As Boolean _
) As outlook.Application
    Static o As outlook.Application
#End If
On Error GoTo ErrHandler

    Select Case True
        Case o Is Nothing, Len(o.Name) = 0
            Set o = GetObject(, "Outlook.Application")
            If o.Explorers.Count = 0 Then
InitOutlook:
                'Open inbox to prevent errors with security prompts
                o.session.GetDefaultFolder(olFolderInbox).Display
                o.ActiveExplorer.WindowState = WindowState
            End If
        Case ReleaseIt
            Set o = Nothing
    End Select
    Set OutlookApp = o

ExitProc:
    Exit Function
ErrHandler:
    Select Case Err.Number
        Case -2147352567
            'User cancelled setup, silently exit
            Set o = Nothing
        Case 429, 462
            Set o = GetOutlookApp()
            If o Is Nothing Then
                Err.Raise 429, "OutlookApp", "Outlook Application does not appear to be installed."
            Else
                Resume InitOutlook
            End If
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Unexpected error"
    End Select
    Resume ExitProc
    Resume
End Function

#If LateBind Then
Private Function GetOutlookApp() As Object
#Else
Private Function GetOutlookApp() As outlook.Application
#End If
On Error GoTo ErrHandler

    Set GetOutlookApp = CreateObject("Outlook.Application")

ExitProc:
    Exit Function
ErrHandler:
    Select Case Err.Number
        Case Else
            'Do not raise any errors
            Set GetOutlookApp = Nothing
    End Select
    Resume ExitProc
    Resume
End Function

Sub open_outlook()
    Dim OutApp  As Object
    Set OutApp = OutlookApp()
    'Automate OutApp as desired
End Sub

回答by Atul Vij

I think you can try below code.Its shortest code i tried to open in my all VBA coding.

我想你可以试试下面的代码。它是我尝试在所有 VBA 编码中打开的最短代码。

Sub Open_Outlook()

Shell ("OUTLOOK")

End Sub

回答by Eugene Astafiev

See How to automate Outlook from another programfor the sample code. You can use the GetObjectmethod for getting the running instance of Outlook instead of creating a new one:

有关示例代码,请参阅如何从另一个程序自动化 Outlook。您可以使用GetObject获取 Outlook 运行实例的方法,而不是创建一个新实例:

Set objOutlook = GetObject(, "Outlook.Application")

However, Outlook is a singleton. Each time you call the CreateObjectmethod you will get the same instance. You can't run two instances of Outlook at the same time. See GetObject in Word VBA script to find Outlook instance fails with 429 error unless both apps running as administratorfor more info.

但是,Outlook 是单例。每次调用该CreateObject方法时,您都会获得相同的实例。您不能同时运行两个 Outlook 实例。请参阅Word VBA 脚本中的 GetObject 以查找 Outlook 实例失败并显示 429 错误,除非两个应用程序都以管理员身份运行以获取更多信息。

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

请注意,Microsoft 目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)自动化 Microsoft Office 应用程序,因为 Office 可能表现出不稳定在此环境中运行 Office 时的行为和/或死锁。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Officearticle.

如果您正在构建一个在服务器端上下文中运行的解决方案,您应该尝试使用已为无人值守执行安全的组件。或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺乏许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。在Office 服务器端自动化注意事项一文中阅读有关此内容的更多信息。

回答by Moreno

You could use something simplier:

你可以使用更简单的东西:

Sub EmailMe()

dim mail as object
dim msg as object

set mail= createobject("Outlook.Application")
set msg=mail.createitem(0)

 with msg
  .to="[email protected];...."
  .subject="What are you sending this for"  
  .body"Whatever you want to say"
  .attachment.add Activeworkbook.fullname
  .send
 end with

end sub

回答by Orin Moyer

Dim oOutlook As Object

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOutlook Is Nothing Then
    Shell ("OUTLOOK")
Else
    'already open
End If