vba 通过 MailEnvelope 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43272849/
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
Send Email via MailEnvelope
提问by Bluesector
I want to copy a specific range into mail (with images).
我想将特定范围复制到邮件中(带有图像)。
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
Why does MailEnvelope show for one second and nothing happen afterwards?
为什么 MailEnvelope 显示一秒钟,然后什么也没有发生?
I replaced .Send
with .Display
but nothing changed. Alternatively I tried to use the RNGtoHTML code but this code does not copy images (I have dynamic linked picture in the sheet "EMAIL").
我替换.Send
了.Display
但没有任何改变。或者,我尝试使用 RNGtoHTML 代码,但此代码不复制图像(我在工作表“EMAIL”中有动态链接图片)。
回答by Petr Szturc
Your issue is that you use
你的问题是你使用
.Display
。展示
but you don't wait for send and then close the Envelope without sending: ActiveWorkbook.EnvelopeVisible = False
但您不会等待发送然后关闭信封而不发送:ActiveWorkbook.EnvelopeVisible = False
- just comment it and the dialog will stay there for you to send it.
- 只需评论它,对话框就会留在那里供您发送。
If you need to use .Send, or .Display doesn't work at all:
如果您需要使用 .Send 或 .Display 根本不起作用:
- running outlook in admin mode solves it
- changing MS Outlook setting for Programatic access* to "warn me if.." solved it kind of for me (displaying the annoying dialog when using .Send)
put MsgBox Err.description at the end to debug your error
MsgBox Err.description End Sub
still trying to figure out the actual solution.
- 在管理模式下运行 Outlook 可以解决它
- 将 MS Outlook 的 Programatic access* 设置更改为“如果……则警告我”为我解决了这个问题(使用 .Send 时显示烦人的对话框)
将 MsgBox Err.description 放在最后以调试您的错误
MsgBox Err.description End Sub
仍在试图找出实际的解决方案。
The mail send macro worked for me but stopped after while. Not really sure why. I even tried to add some registry keys (https://social.technet.microsoft.com/Forums/ie/en-US/e2c89fec-beb3-4224-a6cb-112704406907/cannot-change-programmatic-access-security?forum=outlook). But that didn't work.
邮件发送宏对我有用,但过了一会儿就停止了。不太确定为什么。我什至尝试添加一些注册表项(https://social.technet.microsoft.com/Forums/ie/en-US/e2c89fec-beb3-4224-a6cb-112704406907/cannot-change-programmatic-access-security?forum =展望)。但这没有用。
*What is most funny is that if you disable MS outlook warning for programatic access (https://www.slipstick.com/developer/change-programmatic-access-options/) - start MS O in admin and go to that setting - it stops displaying the warnig but also stops the request and the functionality. The usual microsoft behaviour these days....
*最有趣的是,如果您禁用程序访问的 MS Outlook 警告(https://www.slipstick.com/developer/change-programmatic-access-options/) - 在管理员中启动 MS O 并转到该设置 -它停止显示警告,但也会停止请求和功能。这些天微软通常的行为......
回答by linktheory
Trying stepping through it.
试图通过它。
When you run the script, it will display when you get to this line of code,
当您运行脚本时,它会在您到达这行代码时显示,
.display
but then the code below is ran and which will cause it to stop displaying.
但随后运行了下面的代码,这将导致它停止显示。
ActiveWorkbook.EnvelopeVisible = False
If you step through the code, you should be able to see your content until you get to the line of code above.
如果您逐步完成代码,您应该能够看到您的内容,直到您到达上面的代码行。