如何将所选电子邮件从 RichText 格式转换为 HTML 格式?
How can I convert a selection of emails from RichText to HTML Format?
背景:
我的工作地点使用 RichText 的默认电子邮件格式。
许多人在他们的电子邮件中添加图像。
带图片的 RichText 电子邮件比带图片的 HTML 电子邮件大得多。
我可以手动打开、编辑、将格式更改为 HTML,并保存以大大减小电子邮件的大小。格式在转换过程中保持不变。
但是,当我使用VBA打开邮件项目,更改格式并保存时,邮件没有转换。
如何使用 vba 将一组 Richtext 电子邮件更改为格式正确的 html 电子邮件,获得与使用功能区手动编辑和保存相同的结果?
下面是我的示例代码,当我 运行 这行代码时,我可以看到:
myMailItem.BodyFormat = olFormatHTML
转换方式与功能区的转换方式不同。
Public Sub myConvertHTML()
Dim mySelectedItems As Selection
Dim myMailItem As MailItem
Dim myRichText As String
Dim myHtmlText As String
' Set reference to the Selection.
Set mySelectedItems = ActiveExplorer.Selection
' Loop through each item in the selection.
For Each myMailItem In mySelectedItems
'if the current format is RichText proceed
If myMailItem.BodyFormat = olFormatRichText Then
myMailItem.Display
'this line does not convert the RichText to Html properly
'it seems to convert the images into an attached file ATT#### that is an ole object
'instead of converting the image and using it in html format
'when using the Ribbon and changing the format to HTML,
'the email is converted and formatting maintained
myMailItem.BodyFormat = olFormatHTML
myMailItem.Save
myMailItem.Close olSave
Else
MsgBox "Already in RichText Format: " & myMailItem.Subject, vbInformation
End If
Next
MsgBox "All Done. Email converted to HTML.", vbOKOnly, "Message"
Set mySelectedItems = Nothing
Set myMailItem = Nothing
End Sub
我尝试从内部操作功能区 VBA,但我一直无法弄清楚如何调用功能区元素
(Format Text/Format/HTML).
如有任何建议,我们将不胜感激。
感谢 Niton,这是我的最终解决方案。
Public Sub myConvertHTML002()
'takes selected list of items in Outlook and converts all mailItems that are RichText into HTML Format
Dim mySelectedObjects As Selection
Dim myObject As Object
Dim myMailItem As MailItem
Dim objItem As Object
' Set reference to the Selection.
Set mySelectedObjects = ActiveExplorer.Selection
' Loop through each item in the selection.
For Each myObject In mySelectedObjects
If myObject.Class = olMail Then
Set myMailItem = myObject
'if the current format is RichText proceed
If myMailItem.BodyFormat = olFormatRichText Then
'have to display the email so we can use the command bars
myMailItem.Display
'special code because we can't change the format of the item from within the item
'we have to use the ActiveInspector
On Error Resume Next
Set objItem = ActiveInspector.CurrentItem
On Error GoTo 0
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
ActiveInspector.CommandBars.ExecuteMso ("EditMessage")
ActiveInspector.CommandBars.ExecuteMso ("MessageFormatHtml")
End If
End If
myMailItem.Close olSave
Else
'MsgBox "Already in RichText Format: " & myMailItem.Subject, vbInformation
End If
End If
Next
MsgBox "All Done. Email converted to HTML.", vbOKOnly, "Message"
Set mySelectedItems = Nothing
Set myMailItem = Nothing
End Sub
您可以使用 ExecuteMso 单击按钮。
CommandBars.ExecuteMso Method "Works on controls that are built-in buttons..."
Sub ChangeToHTML()
Dim objItem As Object
Dim objMail As mailitem
On Error Resume Next
Set objItem = ActiveInspector.currentItem
On Error GoTo 0
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
ActiveInspector.CommandBars.ExecuteMso ("MessageFormatHtml")
End If
End If
End Sub
为快速访问工具栏或功能区添加内置按钮时,将鼠标悬停在所选内容上时,您可以看到 MessageFormatHtml、IdMso 作为文本的最后一位。
背景: 我的工作地点使用 RichText 的默认电子邮件格式。 许多人在他们的电子邮件中添加图像。 带图片的 RichText 电子邮件比带图片的 HTML 电子邮件大得多。
我可以手动打开、编辑、将格式更改为 HTML,并保存以大大减小电子邮件的大小。格式在转换过程中保持不变。
但是,当我使用VBA打开邮件项目,更改格式并保存时,邮件没有转换。
如何使用 vba 将一组 Richtext 电子邮件更改为格式正确的 html 电子邮件,获得与使用功能区手动编辑和保存相同的结果?
下面是我的示例代码,当我 运行 这行代码时,我可以看到:
myMailItem.BodyFormat = olFormatHTML
转换方式与功能区的转换方式不同。
Public Sub myConvertHTML()
Dim mySelectedItems As Selection
Dim myMailItem As MailItem
Dim myRichText As String
Dim myHtmlText As String
' Set reference to the Selection.
Set mySelectedItems = ActiveExplorer.Selection
' Loop through each item in the selection.
For Each myMailItem In mySelectedItems
'if the current format is RichText proceed
If myMailItem.BodyFormat = olFormatRichText Then
myMailItem.Display
'this line does not convert the RichText to Html properly
'it seems to convert the images into an attached file ATT#### that is an ole object
'instead of converting the image and using it in html format
'when using the Ribbon and changing the format to HTML,
'the email is converted and formatting maintained
myMailItem.BodyFormat = olFormatHTML
myMailItem.Save
myMailItem.Close olSave
Else
MsgBox "Already in RichText Format: " & myMailItem.Subject, vbInformation
End If
Next
MsgBox "All Done. Email converted to HTML.", vbOKOnly, "Message"
Set mySelectedItems = Nothing
Set myMailItem = Nothing
End Sub
我尝试从内部操作功能区 VBA,但我一直无法弄清楚如何调用功能区元素
(Format Text/Format/HTML).
如有任何建议,我们将不胜感激。
感谢 Niton,这是我的最终解决方案。
Public Sub myConvertHTML002()
'takes selected list of items in Outlook and converts all mailItems that are RichText into HTML Format
Dim mySelectedObjects As Selection
Dim myObject As Object
Dim myMailItem As MailItem
Dim objItem As Object
' Set reference to the Selection.
Set mySelectedObjects = ActiveExplorer.Selection
' Loop through each item in the selection.
For Each myObject In mySelectedObjects
If myObject.Class = olMail Then
Set myMailItem = myObject
'if the current format is RichText proceed
If myMailItem.BodyFormat = olFormatRichText Then
'have to display the email so we can use the command bars
myMailItem.Display
'special code because we can't change the format of the item from within the item
'we have to use the ActiveInspector
On Error Resume Next
Set objItem = ActiveInspector.CurrentItem
On Error GoTo 0
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
ActiveInspector.CommandBars.ExecuteMso ("EditMessage")
ActiveInspector.CommandBars.ExecuteMso ("MessageFormatHtml")
End If
End If
myMailItem.Close olSave
Else
'MsgBox "Already in RichText Format: " & myMailItem.Subject, vbInformation
End If
End If
Next
MsgBox "All Done. Email converted to HTML.", vbOKOnly, "Message"
Set mySelectedItems = Nothing
Set myMailItem = Nothing
End Sub
您可以使用 ExecuteMso 单击按钮。
CommandBars.ExecuteMso Method "Works on controls that are built-in buttons..."
Sub ChangeToHTML()
Dim objItem As Object
Dim objMail As mailitem
On Error Resume Next
Set objItem = ActiveInspector.currentItem
On Error GoTo 0
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
ActiveInspector.CommandBars.ExecuteMso ("MessageFormatHtml")
End If
End If
End Sub
为快速访问工具栏或功能区添加内置按钮时,将鼠标悬停在所选内容上时,您可以看到 MessageFormatHtml、IdMso 作为文本的最后一位。