You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

Outlook中用VBScript替换邮件逗号为换行符的技术咨询

Outlook Solution to Replace Commas with Line Breaks

Hey there! Let’s get this sorted for you. Since you’re already familiar with Word VBA, the Outlook approach follows similar logic but targets email content directly—specifically the HTML body, since you want those line breaks to render as <br> tags for readability. Here’s how to make it work:

Step 1: Use Outlook VBA (Most Convenient for In-App Use)

Press Alt + F11 to open Outlook’s VBA Editor. Right-click your Outlook project in the Project Explorer, go to Insert > Module, then paste this code:

Sub ReplaceCommasWithLineBreaks()
    Dim objMail As MailItem
    Dim originalContent As String
    Dim modifiedContent As String
    
    ' Ensure only one email is selected to avoid bulk changes
    If Application.ActiveExplorer.Selection.Count <> 1 Then
        MsgBox "Please select exactly one email to process!", vbExclamation
        Exit Sub
    End If
    
    Set objMail = Application.ActiveExplorer.Selection(1)
    
    ' Grab the email's HTML content (where <br> tags will work)
    originalContent = objMail.HTMLBody
    
    ' Replace ", " (comma + space) with "<br> " to match your example format
    ' Adjust to "," if your commas don't have trailing spaces
    modifiedContent = Replace(originalContent, ", ", "<br> ")
    
    ' Update and save the email
    objMail.HTMLBody = modifiedContent
    objMail.Save
    
    MsgBox "Commas replaced with line breaks successfully!", vbInformation
    
    Set objMail = Nothing
End Sub

How This Works

  • Selection Check: It first verifies you’ve picked exactly one email to prevent accidental edits.
  • HTML Focus: We use HTMLBody instead of plain text because <br> tags only render properly in HTML-formatted emails.
  • Replace Logic: The Replace function swaps every comma-space pair with a line break tag plus space—perfect for aligning your result items like in your example.

Test with Your Example

Original email content:

Request ID: 527 Results: [06-641-20_XABY_C-terminally designated_region A, 46-643-00_AASC_C-terminally designated_region C, 06-642-60_TTTS_C-terminally designated_region S]

After running the macro, it becomes:

Request ID: 527 Results: [06-641-20_XABY_C-terminally designated_region A
46-643-00_AASC_C-terminally designated_region C
06-642-60_TTTS_C-terminally designated_region S]

Bonus: Standalone VBScript Version

If you prefer a script you can run outside Outlook, save this as ReplaceCommas.vbs and double-click it (make sure Outlook is open and an email is selected):

Set objOutlook = CreateObject("Outlook.Application")
Set objExplorer = objOutlook.ActiveExplorer
Set objSelection = objExplorer.Selection

If objSelection.Count <> 1 Then
    WScript.Echo "Please select exactly one email in Outlook first!"
    WScript.Quit
End If

Set objMail = objSelection.Item(1)
originalContent = objMail.HTMLBody
modifiedContent = Replace(originalContent, ", ", "<br> ")
objMail.HTMLBody = modifiedContent
objMail.Save

WScript.Echo "Commas replaced with line breaks successfully!"

' Clean up objects
Set objMail = Nothing
Set objSelection = Nothing
Set objExplorer = Nothing
Set objOutlook = Nothing

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

火山引擎 最新活动