在 Outlook 中自动设置时间、货币值和电话号码的格式

问题描述 投票:0回答:1

有没有一种方法可以在输入一天中的某个时间(例如上午/下午 10:24)或任何货币值或电话号码时,自动设置文本格式?我需要正则表达式或通配符吗?我如何使其与 ActiveExplorer 和 ActiveInspector 一起使用?

现在我有类似下面的东西,但它只是作为弹出窗口工作

Sub ReplaceWithBoldHTML2()
    Dim regExp As Object
    Set regExp = CreateObject("vbscript.regexp")

    Dim item As Object
    Set item = Application.ActiveInspector.CurrentItem
    
    If TypeOf item Is Outlook.mailItem Then
        Dim bodyHTML As String
        bodyHTML = item.htmlBody
    
        ' Find the position of the signature
        Dim signaturePos As Long
        signaturePos = InStr(1, bodyHTML, "Best,")

        ' Check if the signature is found
        If signaturePos > 0 Then
            ' Extract the portion of the body above the signature
            Dim bodyAboveSignature As String
            bodyAboveSignature = Left(bodyHTML, signaturePos - 1)
            
            ' Apply the regex pattern to the extracted portion
            With regExp
                .pattern = "\$[0-9]{1,3}(([\.?,?][0-9]{1,3}){1,})?"
                .Global = True
                
                ' Iterate through each match in the HTML body above the signature
                For Each match In .Execute(bodyAboveSignature)
                    ' Replace each match with a bold version
                    bodyAboveSignature = Replace(bodyAboveSignature, match.Value, "<b>" & match.Value & "</b>")
                Next match
            End With
            
            ' Update the HTMLBody property with the modified HTML
            item.htmlBody = bodyAboveSignature & Mid(bodyHTML, signaturePos)
        Else
            MsgBox "Signature not found.", vbExclamation
        End If
    Else
        MsgBox "This code is designed for MailItems in Outlook.", vbExclamation
    End If
End Sub
regex vba outlook wildcard
1个回答
0
投票

所以我能够创建一个有点工作的代码。然而,我遇到了一个新问题。以下是其中一些:

  1. 我通过私有子进程通过 ThisOutlookSession 调用了宏。每当电子邮件位于文件夹视图中并且我按“发送”时,就会调用宏,但会将其带入弹出窗口。所以我再次运行代码并进行格式化。当玩这个时,我让 ThisOutlookSession 中的私有子调用宏两次,所以因为我不明白该怎么做。

  2. 有时 Microsoft Outlook 无法识别签名“Best”。一旦该消息弹出,电子邮件将发送,但 Outlook 将崩溃。这可能是因为如果我在弹出窗口中显示电子邮件并且它运行两次,但我不太确定。

  3. 出于某种原因,当我将此正则表达式添加到 .pattern '[0-9]{1,2}[/-][0-9]{1,2}([/-][0-9 ]{2,4})?'它会在代码顶部添加随机代码,例如 04/12/omml" xmlns="http://www.w3.org/TR/REC-html40"> 或每次类似的内容。没有我的其他正则表达式就是这样做的。

以下是代码:

'''' 子粗体DarkBlueValues() Dim regExp 作为对象 设置 regExp = CreateObject("vbscript.regexp")

Dim Item As Object
Dim bodyHTML As String
Dim signaturePos As Long
Dim bodyAboveSignature As String

' Check if the active window is an Explorer or an Inspector
If TypeName(Application.ActiveWindow) = "Explorer" Then
    ' For Explorer, work with the selected items
    Set Item = ActiveExplorer.selection.Item(1)
    
    ' Get the Inspector associated with the selected item
    Dim insp As Outlook.Inspector
    Set insp = Item.GetInspector
    ' Activate the Inspector to ensure it's fully loaded
    insp.Activate
ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then
    ' For Inspector, work with the currently open item
    Set Item = ActiveInspector.CurrentItem
Else
    MsgBox "Unsupported window type.", vbExclamation
    Exit Sub
End If

If TypeOf Item Is Outlook.mailItem Then
    ' Get the HTML body of the email
    bodyHTML = Item.htmlBody

    ' Find the position of the signature
    signaturePos = InStr(1, bodyHTML, "Best,")

    ' Check if the signature is found
    If signaturePos > 0 Then
        ' Extract the portion of the body above the signature
        bodyAboveSignature = Left(bodyHTML, signaturePos - 1)

        ' Apply the regex pattern to the extracted portion
        With regExp
            .pattern = "(\$[0-9]{1,3}(([\.?,?][0-9]{1,3}){1,})?)|(\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4})|(\d{1,2}:\d{2}(?:\s?[AaPp](\.?)[Mm]\1)?)|([0-9]{1,3}(.[0-9]{1,2})?\%)"
            .Global = True

            ' Iterate through each match in the HTML body above the signature
            For Each match In .Execute(bodyAboveSignature)
                ' Replace each match with a bold version
                bodyAboveSignature = Replace(bodyAboveSignature, match.Value, "<span style=""color: #081E36; font-weight: bold;"">" & match.Value & "</span>")
            Next match
        End With

        ' Update the HTMLBody property with the modified HTML
        Item.htmlBody = bodyAboveSignature & Mid(bodyHTML, signaturePos)
    Else
        MsgBox "Signature not found for some reason.", vbExclamation
    End If
Else
    MsgBox "This code is designed for MailItems in Outlook.", vbExclamation
End If

结束子

'''

© www.soinside.com 2019 - 2024. All rights reserved.