Set oApp = CreateObject("Outlook.Application")
Set oMailItem = oApp.CreateItem(0)
oMailItem.BodyFormat = olFormatRichText
Set oInspector = oMailItem.GetInspector
oInspector.Display
MsgBox "IsWordMail = " & oInspector.IsWordMail & vbLf & "EditorType = " & (oInspector.EditorType = olEditorWord) ' Both are true.
设置wdDoc = oInspector.WordEditor' < - 运行时错误' 287'应用程序定义或对象定义的错误
这种格式的option-mail-composer消息是" HTML" outlook参考16.0已设置
答案 0 :(得分:0)
如果代码在迁移到Office 365后或由于其他任何原因突然停止工作,请参阅下面的代码。添加了注释,以便于理解和实施。
如果您具有管理权限,则还可以尝试以下链接中给出的注册表更改: https://support.microsoft.com/en-au/help/926512/information-for-administrators-about-e-mail-security-settings-in-outlo
但是,我建议使用一个独立于Excel版本的代码,而不需要进行系统更改,因为每个最终用户的计算机上也都需要进行系统更改。
Option Explicit
Sub Create_Email(ByVal strTo As String, ByVal strSubject As String)
Dim rngToPicture As Range
Dim outlookApp As Object
Dim Outmail As Object
Dim strTempFilePath As String
Dim strTempFileName As String
'Name it anything, doesn't matter
strTempFileName = "RangeAsPNG"
'rngToPicture is defined as NAMED RANGE in the workbook, do modify this name before use
Set rngToPicture = Range("rngToPicture")
Set outlookApp = CreateObject("Outlook.Application")
Set Outmail = outlookApp.CreateItem(olMailItem)
'Create an email
With Outmail
.To = strTo
.Subject = strSubject
'Create the range as a PNG file and store it in temp folder
Call createPNG(rngToPicture, strTempFileName)
'Embed the image in Outlook
strTempFilePath = Environ$("temp") & "\" & strTempFileName & ".png"
.Attachments.Add strTempFilePath, olByValue, 0
'Change the HTML below to add Header (Dear John) or signature (Kind Regards) using newline tag (<br />)
.HTMLBody = "<img src='cid:DashboardFile.png' style='border:0'>"
.Display
End With
Set Outmail = Nothing
Set outlookApp = Nothing
Set rngToPicture = Nothing
End Sub
Sub createPNG(ByRef rngToPicture As Range, nameFile As String)
Dim wksName As String
wksName = rngToPicture.Parent.Name
'Delete the existing PNG file of same name, if exists
On Error Resume Next
Kill Environ$("temp") & "\" & nameFile & ".png"
On Error GoTo 0
'Copy the range as picture
rngToPicture.CopyPicture
'Paste the picture in Chart area of same dimensions
With ThisWorkbook.Worksheets(wksName).ChartObjects.Add(rngToPicture.Left, rngToPicture.Top, rngToPicture.Width, rngToPicture.Height)
.Activate
.Chart.Paste
'Export the chart as PNG File to Temp folder
.Chart.Export Environ$("temp") & "\" & nameFile & ".png", "PNG"
End With
Worksheets(wksName).ChartObjects(Worksheets(wksName).ChartObjects.Count).Delete
End Sub