vba - 如何用VBA打开outlook

标签 vba outlook

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

#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

最佳答案

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

Sub Open_Outlook()

Shell ("OUTLOOK")

End Sub

关于vba - 如何用VBA打开outlook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328314/

相关文章:

asp.net - 在 ASP.NET 中使用 MAPI 读取 oulook 时出错

vba - Outlook - 检查电子邮件地址类型

c# - 使用 EWS/以编程方式使用 Outlook 日记条目

vba - 在同一 Outlook 对话下使用 VBA 发送电子邮件

excel - VBA 字符串 - 添加换行符\n 等效项不起作用

excel - VBA递归 "For loops"排列?

excel - 搜索一系列单元格中的所有单词对

excel - VBA 通过 FOR EACH 循环对象

html - 用于 outlook 的 HTML 代码分析器,如 firebug

vba - 如何在 Excel 中使用 VBA 从 SQLite 数据库检索数据?