You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Excel VBA导出带加粗格式的Bloomberg商品价格至Outlook邮件

解决Outlook邮件中商品价格加粗的问题

你的需求很明确——要让邮件里类似US$92.71的价格内容显示为加粗格式。现有代码里的RangetoHTML函数只是导出单元格的原始文本格式,我们可以通过在生成HTML内容后,用正则表达式匹配价格字符串并添加HTML加粗标签的方式实现这个效果。

下面是修改后的完整代码,关键改动集中在RangetoHTML函数里:

Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
' -------------------------------------------------------
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Range("S3:S26").SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
'Set rng = Sheets("Output").Range("D4:D12").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Cells(41, 2)
.CC = Cells(41, 3)
.BCC = ""
.Subject = Cells(1, 12)
.HTMLBody = RangetoHTML(rng)
.Display
'.Send 'or use .Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Modified to bold price strings (US$XXX.XX) - current solution
' Working in Office 2000-2016
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
Dim regex As Object ' 新增正则表达式对象

TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close

' --- 核心改动:匹配价格并添加加粗标签 ---
Set regex = CreateObject("VBScript.RegExp")
With regex
    .Pattern = "(US\$\d+\.\d{2})" ' 精准匹配US$开头+整数+两位小数的格式
    .Global = True ' 替换所有符合条件的价格
    .IgnoreCase = False
End With
RangetoHTML = regex.Replace(RangetoHTML, "<b>$1</b>")
' --- 结束核心改动 ---

RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
Set regex = Nothing ' 释放正则对象
End Function

关键说明:

  • 新增的正则表达式会自动匹配所有US$XXX.XX格式的价格字符串
  • 匹配到的价格会被包裹在<b>标签里,实现邮件中的加粗效果
  • 如果你的价格格式有变动(比如小数位数不是固定两位),可以把正则模式改成(US\$\d+\.\d+)来适配任意小数位数

直接替换原有代码后,测试发送邮件,就能看到所有价格内容自动加粗了!

内容的提问来源于stack exchange,提问作者Brighter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:33:55