首页 文章

附加到outlook模板的VBA打开工作簿

提问于
浏览
1

有没有办法可以打开附加到电子邮件模板的工作簿,编辑并在发送邮件之前保存它?我用 Set Mesg = OutlookAp.CreateItemFromTemplate("C:\Template.oft") 创建了mailitem对象,我可以看到附件,但是我可以完成,我全都听见了 .

看起来我可能需要在发送之前保存和编辑文件...仍然可以接受想法,但看起来它似乎无法通过VBA打开附件

1 回答

  • 0

    我假设您从Excel自动化Outlook . 此解决方案可能适合您,但正如您所说,它确实依赖于保存附件并重新附加文件的操作版本 . 假设您可以编写将工作簿附件"edit"的代码,这应该适合您 .

    Sub TestOutlookTemplate()
    
    Dim MyOutlook As Outlook.Application
    Dim MyMail As Outlook.MailItem
    Dim att As Outlook.Attachment
    Dim templatePath As String
    Dim tempFileName As String
    Dim attWorkbook As Workbook
    
    
    templatePath = "C:\users\david_zemens\desktop\Untitled.oft"
    tempFileName = "C:\users\david_zemens\desktop\tempexcelfile.xlsx"
    
    Set MyOutlook = CreateObject("Outlook.Application")
    
    Set MyMail = MyOutlook.CreateItemFromTemplate(templatePath)
        MyMail.Display
    
        For Each att In MyMail.Attachments
            If att.DisplayName Like "*.xls*" Then
                att.SaveAsFile tempFileName
    
                'Now that you have saved the file, delete the attachment
                att.Delete
    
                'Open the file
                Set attWorkbook = Workbooks.Open(tempFileName)
    
                'Perform manipulation on the file
                attWorkbook.Sheets(1).Name = "Sheet ONE"
    
                'Save fhe file
                attWorkbook.Save
    
                'Close the file
                attWorkbook.Close
    
                MyMail.Attachments.Add tempFileName
            End If
        Next
    
    
    'Send your mail (make sure you have added a recipient
    MyMail.Send
    
    Set attWorkbook = Nothing
    Set MyMail = Nothing
    Set MyOutlook = Nothing
    
    
    End Sub
    

相关问题