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

时间:2014-01-14 20:16:32

标签: excel vba outlook email-attachments

在发送邮件之前,有没有办法可以打开附加到电子邮件模板的工作簿,编辑并保存?我使用Set Mesg = OutlookAp.CreateItemFromTemplate("C:\Template.oft")创建了mailitem对象,我可以看到附件,但到目前为止我看不到打开它的方法。如果有人有建议,或者知道这根本无法做到,我全都听见了。

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

1 个答案:

答案 0 :(得分:0)

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

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