Attachments.Add 当文件打开时,使用 Office365,使用在旧版本的 Outlook 中工作的 Outlook VBA

Attachments.Add when file is open, with Office365, using Outlook VBA that worked in older version of Outlook

提问人:blimbert 提问时间:11/16/2023 最后编辑:Communityblimbert 更新时间:11/20/2023 访问量:42

问:

我从 Office 2016 迁移到 Office 365。我有多年的代码。

该代码具有附加文件的选项。如果附加的文件未打开,则它可以正常工作。如果文件已打开,则我会收到以下错误:

运行时错误 '-2147024864 (80070020)

enter image description here

Sub Email(Optional vToAddress As String, Optional vSubject As String, Optional vEmailRange As String = "", Optional vCCAddress As String, Optional vBCCAddress As String = "", Optional vAttachmentFullPath As String)

    Dim OutApp As Object
    Dim OutMail As Object
    Dim olInsp As Object
    Dim xlSheet As Worksheet
    Dim wdDoc As Object
    Dim oRng As Object
        
    If vEmailRange <> "" Then
        Application.GoTo Range(vEmailRange)
        Selection.Copy
    End If
    
    On Error Resume Next
    Set OutApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then Set OutApp = CreateObject("Outlook.Application")
    On Error GoTo 0
        
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = vToAddress
        If vCCAddress <> "" Then .CC = vCCAddress
        If vBCCAddress <> "" Then .BCC = vBCCAddress
        .Subject = vSubject
        .BodyFormat = olFormatHTML
        '.HTMLBody = ""
        If vAttachmentFullPath <> "" And Dir(vAttachmentFullPath) <> "" Then
            .Attachments.Add vAttachmentFullPath
        End If
            
        Set olInsp = .GetInspector
        Set wdDoc = olInsp.WordEditor
        Set oRng = wdDoc.Range
            
        If vEmailRange <> "" Then
            oRng.Collapse 1
            oRng.Paste
        End If
           
        .Display
        .Send
    End With
       
    Set OutMail = Nothing
    Set OutApp = Nothing
    Set olInsp = Nothing
    Set wdDoc = Nothing
    Set oRng = Nothing
    Application.CutCopyMode = False

End Sub
VBA Outlook Office365 电子邮件附件

评论

0赞 FaneDuru 11/16/2023
我无法测试它,没有可用的 Excel 版本,但我也不能否认它的行为。也许 Microsoft 代表认为您可以修改相应的文件并冒着 npt 发送最终版本的风险......或不。所以,我认为你应该忍受这一点。我建议您使用使用相同路径的方法,但将“Copy_”作为现有文件的前缀。使用此方法,保存的文件不会打开,因此您可以附加它,然后删除 () 它...ActiveWorkbook.SaveCopyAskill
0赞 Tragamor 11/16/2023
如果问题是打开的文件,我会想到两种解决方案:要么关闭文件(可能不需要),要么创建一个可以附加到电子邮件的临时副本。
1赞 Dmitry Streblechenko 11/16/2023
也许正确的问题是为什么文件是打开的?它从何而来?

答: 暂无答案