首页 文章

获取Outlook附件的VBA宏仅在Outlook关闭时有效

提问于
浏览
0

Excel 2010和SSIS 2012 - 当Outlook打开并运行宏时,我收到运行时错误429 - ActiveX无法创建对象...当Outlook关闭时,宏按预期执行 - 它会加载所有开始的附件使用迷你报告并以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

2 回答

  • 0

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

  • 0

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

相关问题