【问题标题】:VBA macro to get Outlook attachments only works when Outlook is closed获取 Outlook 附件的 VBA 宏仅在 Outlook 关闭时有效
【发布时间】:2015-10-30 16:29:35
【问题描述】:

Excel 2010 和 SSIS 2012 - 当 Outlook 打开并运行宏时,我收到运行时错误 429 - ActiveX 无法创建对象...当 Outlook 关闭时,宏按预期执行 - 它会下载所有以 Mini Report 开头并以 xlsx 结尾的附件。

SSIS 包使用宏打开 Excel 文件,但随后返回 ActiveX 错误。同样,如果 Outlook 已关闭,SSIS 包会打开 Excel,运行宏(下载文件)并将它们保存在我们的共享驱动器目录中。

我编写了哪些代码需要关闭 Outlook?

VBA代码如下:

Sub GetAttachments()

Dim olapp As Object
Dim olmapi As Object
Dim olmail As Object
Dim olitem As Object
Dim lrow As Integer
Dim olattach As Object
Dim FileName As String

Const num As Integer = 6
Const path As String = "Y:\Wireline Forecast\MiniReport - Production\Mini Report Region Automation\Load Files\"
Const strFileType As String = "xlsx"

Set olapp = CreateObject("outlook.application")
Set olmapi = getnamespace("MAPI")
Set olmail = olmapi.getdefaultfolder(num)

If olmail.Items.restrict("[UNREAD]=True").Count = 0 Then

    MsgBox ("No Unread mails")

    Else
        For Each olitem In olmail.Items.restrict("[UNREAD]=True")
            If olitem.attachments.Count <> 0 Then
                For Each olattach In olitem.attachments
                    If Left(olattach.FileName, 11) = "Mini Report" And Right(olattach.FileName, 4) = "xlsx" Then
                    FileName = "Y:\Wireline Forecast\MiniReport - Production\Mini Report Region Automation\Load Files\" & olattach.FileName
                    olattach.SaveAsFile FileName
                    End If
                Next olattach
            End If
        Next olitem
End If

End Sub

【问题讨论】:

    标签: vba excel ssis outlook


    【解决方案1】:

    如果 Outlook 和 Excel 在不同的安全上下文中运行,就会发生这种情况。确保这两个或两个应用都没有以提升的权限运行(以管理员身份运行)。

    【讨论】:

    • IT 已将其锁定,因此无法以管理员身份运行,因此两个程序应以相同的安全级别运行。
    • Excel 和 Outlook 是如何启动的?
    【解决方案2】:

    实际上发现 Outlook 在打开时被“锁定”,这解释了 ActiveX 错误,所以我构建了一个解决方法....我刚刚添加了一个关闭 Outlook 的脚本任务,以便下一个脚本任务可以获取附件然后是另一个重新打开 Outlook 的脚本任务。不是最漂亮或最干净的,但现在可以使用。

    【讨论】:

      猜你喜欢
      • 2016-09-01
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2023-02-02
      相关资源
      最近更新 更多