通过 Excel VBA 在 Outlook 中打开新邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22747284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:02:43  来源:igfitidea点击:

Open new message in Outlook by Excel VBA

excelvbaexcel-vbaoutlook

提问by Petrik

everyday I am handling daily reporting. Which was quite time consuming. Basically I need to send email containing brief comparison of sales yesterday with sales last week and month. That is working pretty well. Once this is done the message is pasted to new sheet and then I have to copy it and paste into new email in Outlook.

每天我都在处理每日报告。这是相当耗时的。基本上我需要发送电子邮件,其中包含昨天与上周和上个月的销售额的简要比较。这工作得很好。完成此操作后,消息将粘贴到新工作表中,然后我必须将其复制并粘贴到 Outlook 中的新电子邮件中。

Is there a possibility to create macro that will open new message in Outlook?So I'll be able to insert my text. I am able to write macro that will send it directly from Excel but this is not something I really want to as some part of the reporting must by done by looking at numbers manually.

是否有可能创建将在 Outlook 中打开新邮件的这样我就可以插入我的文本了。我能够编写直接从 Excel 发送的宏,但这并不是我真正想要的,因为报告的某些部分必须通过手动查看数字来完成。

Many thanks in advance!

提前谢谢了!

回答by brettdj

To add the ActiveWorbookas an attachment:

要将其添加ActiveWorbook为附件:

  1. Save it to a specifc location
  2. Use Attachments.Addto add the file from the location from 1
  1. 将其保存到特定位置
  2. Use Attachments.Add从 1 的位置添加文件

code

代码

Sub CustomMailMessage()
Dim strFile As String
Dim OutApp As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Recipient
Dim Recipients As Recipients

  Set OutApp = CreateObject("Outlook.Application")
  Set objOutlookMsg = OutApp.CreateItem(olMailItem)

  strFile = "C:\temp\myfile.xlsx"
  ActiveWorkbook.SaveAs strFile

  Set Recipients = objOutlookMsg.Recipients
  Set objOutlookRecip = Recipients.Add("[email protected]")
  objOutlookRecip.Type = 1

  With objOutlookMsg
    .SentOnBehalfOfName = "[email protected]"
    .Subject = "Testing this macro"
    .HTMLBody = "Testing this macro" & vbCrLf & vbCrLf
    'Resolve each Recipient's name.
    For Each objOutlookRecip In objOutlookMsg.Recipients
      objOutlookRecip.Resolve
    Next
    .Attachments.Add strFile
    .display
  End With

  'objOutlookMsg.Send
  Set OutApp = Nothing
End Sub

回答by Petrik

I've found this one and it is working perfectly!!!!

我找到了这个,它运行良好!!!!

Just maybe one extra thing - is there a possibility to attach opened document as a attachment?

也许只是一件事 - 是否有可能将打开的文档作为附件附加?

Sub CustomMailMessage()
Dim OutApp As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Recipient
Dim Recipients As Recipients

  Set OutApp = CreateObject("Outlook.Application")
  Set objOutlookMsg = OutApp.CreateItem(olMailItem)

  Set Recipients = objOutlookMsg.Recipients
  Set objOutlookRecip = Recipients.Add("[email protected]")
  objOutlookRecip.Type = 1

  objOutlookMsg.SentOnBehalfOfName = "[email protected]"
  objOutlookMsg.Subject = "Testing this macro"
  objOutlookMsg.HTMLBody = "Testing this macro" & vbCrLf & vbCrLf
  'Resolve each Recipient's name.
  For Each objOutlookRecip In objOutlookMsg.Recipients
    objOutlookRecip.Resolve
  Next
  'objOutlookMsg.Send
  objOutlookMsg.Display

  Set OutApp = Nothing  
End Sub

回答by avb

I cannot test it now, but it would go like this:

我现在无法测试它,但它会是这样的:

set o = createObject("Outlook.Application")
set m = o.CreateItem(olMailItem) ' replace it with 0 if you get error here
o.show ' or .Display - not sure

You can set o.To, o.Subject etc. before displaying it. Sorry it is not tested but I do not have Outlook on my home computer, I use it only at work. I will check it tomorrow if I remembered correctly.

您可以在显示之前设置 o.To、o.Subject 等。对不起,它没有经过测试,但我的家用电脑上没有 Outlook,我只在工作中使用它。如果我没记错的话,明天我会检查一下。