每当我运行下面的vba代码时,它都会显示无效使用新的关键字。

问题描述 投票:0回答:1

使用这段代码,我试图通过Outlook发送excel作为附件。

   Sub SendOutlook()
       'Declaring Variables
       Dim OutlookApp  As Outlook.Application
       Dim OutlookEmail  As Outlook.MailItem

       'Assigning variables to create outlook application and mailitem
       Set OutlookApp = New Outlook.Application
       Set OutlookEmail = New Outlook.MailItem

       With OutlookEmail
          'Format of the mail
          .BodyFormat = olFormatPlain
          'Body of the mail
          .Body = "Dear Someone" & vbNewLine & "How are you?"
          'To whom you want to send mail
          .To = "[email protected]"
          'Subject of mail
          .Subject = "Write Subject Here"
          'TO add an attachment
          .Attachments.Add ActiveWorkbook.FullName
          'sends the mail
          .Send
        End With

    End Sub
excel vba excel-2010
1个回答
1
投票

你不能创建一个 MailItem 通过 New. 必须使用 CreateItem 的Outlook应用对象。

   Set OutlookApp = New Outlook.Application
   Set OutlookEmail = OutlookApp.CreateItem(olMailItem)

0
投票

据我调查了解到的情况是,程序化访问发送邮件是有安全风险的,所以不允许通过VBA。

© www.soinside.com 2019 - 2024. All rights reserved.