我想创建一个将特定范围复制到outlook(带图像)的宏。但它并不像我想要的那样......
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”表中有动态链接图片)。
答案 0 :(得分:0)
您的问题是您使用
。显示
但是你不等待发送,然后关闭信封而不发送: ActiveWorkbook.EnvelopeVisible = False
如果您需要使用.Send,或.Display根本不起作用:
将MsgBox Err.description放在最后以调试错误
MsgBox Err.description End Sub
仍在尝试找出实际解决方案。
邮件发送宏对我有用但在一段时间后停止了。不确定为什么。我甚至试图添加一些注册表项(https://social.technet.microsoft.com/Forums/ie/en-US/e2c89fec-beb3-4224-a6cb-112704406907/cannot-change-programmatic-access-security?forum=outlook)。但那并没有奏效。
*最有趣的是,如果您禁用MS Outlook警告以进行程序访问(https://www.slipstick.com/developer/change-programmatic-access-options/) - 在管理员中启动MS O并转到该设置 - 它停止显示warnig但也停止请求和功能。这些天通常的微软行为......
答案 1 :(得分:0)
尝试踩过它。
当你运行脚本时,它会在你到达这行代码时显示,
.display
但是下面的代码已经运行,这会导致它停止显示。
ActiveWorkbook.EnvelopeVisible = False
如果您单步执行代码,则应该能够看到自己的内容,直到找到上面的代码行。