通过MailEnvelope发送电子邮件(VBA,Excel)
问题描述:
我想创建一个将特定范围复制到Outlook(带图像)的宏。但它不工作就像我想要它...通过MailEnvelope发送电子邮件(VBA,Excel)
Sub Send_Range_Or_Whole_Worksheet_with_MailEnvelope()
'Working in Excel 2002-2016
Dim AWorksheet As Worksheet
Dim Sendrng As Range
Dim rng As Range
On Error GoTo StopMacro
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Fill in the Worksheet/range you want to mail
'Note: if you use one cell it will send the whole worksheet
Set Sendrng = Worksheets("EMAIL").Range("B2:W41")
'Remember the activesheet
Set AWorksheet = ActiveSheet
With Sendrng
' Select the worksheet with the range you want to send
.Parent.Select
'Remember the ActiveCell on that worksheet
Set rng = ActiveCell
'Select the range you want to mail
.Select
' Create the mail and send it
ActiveWorkbook.EnvelopeVisible = True
With .Parent.MailEnvelope
' Set the optional introduction field thats adds
' some header text to the email body.
.Introduction = "This is test mail 2."
With .Item
.To = ThisWorkbook.Sheets("EMAIL").Range("Z1").Value
.CC = ThisWorkbook.Sheets("EMAIL").Range("Z1").Value
.BCC = ""
.Subject = ThisWorkbook.Sheets("EMAIL").Range("D1").Value
.Display
End With
End With
'select the original ActiveCell
rng.Select
End With
'Activate the sheet that was active before you run the macro
AWorksheet.Select
StopMacro:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
ActiveWorkbook.EnvelopeVisible = False
End Sub
有人可以告诉我为什么MailEnvelope只显示1秒,事后没有任何反应?似乎它不适合我。
我用.display替换了.send,但仍然没有任何变化。另外我尝试使用RNGtoHTML代码,但是这段代码不会复制图像(我在表单“EMAIL”中有动态链接图片)。
答
你的问题是,你使用
。显示
,但你不等待发送,然后关闭信封,而不发送: ActiveWorkbook.EnvelopeVisible =假
- 只是评论它和对话框将留在那里供您发送。
如果你需要使用。发送,或。显示不能在所有的工作:
- 在管理员模式下运行Outlook解决它
- 改变微软的Outlook设置程序化进入*“ (使用时。发送显示烦人的对话框)如果警告我。”解决它那种对我来说
-
把MSGBOX Err.description在结束调试你的错误
MSGBOX Err.descriptio n End Sub
仍试图找出实际的解决方案。
邮件发送宏为我工作,但之后停止。不知道为什么。我甚至试图添加一些注册表项(https://social.technet.microsoft.com/Forums/ie/en-US/e2c89fec-beb3-4224-a6cb-112704406907/cannot-change-programmatic-access-security?forum=outlook)。但那并不奏效。
*什么是最搞笑的是,如果你禁用编程方式访问微软的Outlook警告(https://www.slipstick.com/developer/change-programmatic-access-options/) - 在管理启动MS O和去那个设置 - 它停止显示warnig也停止请求和功能。这几天通常的微软行为......