Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 07-25-2025, 05:33 PM
June7's Avatar
June7 June7 is offline Select text, click button, hyphens replace spaces Windows 10 Select text, click button, hyphens replace spaces Office 2010
Novice
 
Join Date: Nov 2023
Posts: 23
June7 is on a distinguished road
Default

Search "outlook vba replace within selected text in email". CoPilot shows me code that invokes WordEditor object. Seems I've seen something like this long before AI. I've never coded behind Outlook so no idea how you establish a "button" to trigger code.

Here's the code as presented, not tested:
Code:
Sub ReplaceInSelectedText()
    Dim objInspector As Outlook.Inspector
    Dim objWordEditor As Object
    Dim objSelection As Object
    Dim selectedText As String
    Dim replacedText As String
    Dim searchText As String
    Dim replaceText As String

    ' Get the current Inspector
    Set objInspector = Application.ActiveInspector

    ' Ensure the Inspector is open and in compose mode
    If Not objInspector Is Nothing And objInspector.EditorType = olEditorWord Then
        ' Access the Word editor
        Set objWordEditor = objInspector.WordEditor
        Set objSelection = objWordEditor.Application.Selection

        ' Get the selected text
        selectedText = objSelection.Text

        ' Define the text to search for and replace
        searchText = "oldWord"  ' Replace with the word/phrase to search for
        replaceText = "newWord" ' Replace with the word/phrase to replace with

        ' Replace the text
        replacedText = Replace(selectedText, searchText, replaceText)

        ' Update the selection with the replaced text
        objSelection.Text = replacedText

        MsgBox "Text replaced successfully!", vbInformation
    Else
        MsgBox "Please ensure you are editing an email and have selected text.", vbExclamation
    End If
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Select text, click button, hyphens replace spaces How to get rid of Hyphens and spaces in result using dollartext switch Strogg Word 1 01-29-2019 01:43 PM
Select Cell Text to paste into Find/Replace CBarry Word VBA 2 02-16-2017 04:37 AM
Select text, click button, hyphens replace spaces Find, select, and replace part of text with bold paik1002 Word VBA 4 12-07-2015 11:24 PM
Select text, click button, hyphens replace spaces Vba with three click button help poem Excel Programming 15 09-22-2015 09:49 PM
Animations on Button Click dvogler PowerPoint 1 03-01-2012 12:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:36 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft