Active Explorer vs Active Inspector Code

Chris Church 0 Reputation points
2023-11-25T07:43:23.91+00:00
The below macro works great with ActiveInspector but I don't know how to write the code for it to be compatible whether it is ActiveExplorer or ActiveInspector as sometimes I like to respond to emails in the folder view:

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
Microsoft 365 and Office | Development | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Eric Legault (MVP) 165 Reputation points MVP
    2023-12-13T22:13:00.6333333+00:00

    You can use the Explorer.Selection property, but you'll have to check if one or more messages are selected. For example:

    Sub GetSelectedItems()     
    	Dim myOlExp As Outlook.Explorer   
    	Dim myOlSel As Outlook.Selection   
    	Dim MsgTxt As String   
    	Dim x As Integer      
    
    	MsgTxt = "You have selected items from: "   
    	Set myOlExp = Application.ActiveExplorer   
    	Set myOlSel = myOlExp.Selection     
    	For x = 1 To myOlSel.Count  	
    		MsgTxt = MsgTxt & myOlSel.Item(x).SenderName & ";"   
    	Next x     
    
    	MsgBox MsgTxt  
    End Sub
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.