Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-25-2025, 08:54 AM
brucemc777 brucemc777 is offline Select text, click button, hyphens replace spaces Windows 10 Select text, click button, hyphens replace spaces Office 2019
Advanced Beginner
Select text, click button, hyphens replace spaces
 
Join Date: Jul 2014
Location: Fredericksburg, VA
Posts: 48
brucemc777 is on a distinguished road
Default Select text, click button, hyphens replace spaces

I must be trying to achieve something rather useless because the only code i can find for this is:
Code:
Sub HyphenateSelection()
    Dim rng As Range
    Set rng = Selection.Range
    rng.Text = Replace(rng.Text, " ", "-")
End Sub
Whereupon i find Outlook does not use the Range Type. Either someone coded without testing or MS has once again "improved" their Outlook product.

Though i have a bit of a grasp with VBA in Excel, it looks like i'm blind in a forest bumping into trees when it comes to Outlook. Would any of you good folks help me out here?

Thank you for your consideration!

Best Regards,
-Bruce
Reply With Quote
  #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
  #3  
Old 07-25-2025, 10:11 PM
Hornblower409's Avatar
Hornblower409 Hornblower409 is offline Select text, click button, hyphens replace spaces Windows 10 Select text, click button, hyphens replace spaces Office 2010
Competent Performer
 
Join Date: Jan 2025
Posts: 136
Hornblower409 will become famous soon enoughHornblower409 will become famous soon enough
Default

An alternative using Word Find/Replace

Code:
Public Sub ReplaceInSelection()

    Word_ReplaceInSelection "Find This", "Replace With This"
    
End Sub

Private Function Word_ReplaceInSelection(ByVal FindTxt As String, ByVal ReplaceText As String) As Boolean
Word_ReplaceInSelection = False

    '   Must have an Active Inspector
    '
    If Not (TypeOf ActiveWindow Is Outlook.Inspector) Then
        MsgBox "The Active Window must be an Inspector.", vbExclamation
        Exit Function
    End If
    
    '   Get the Active Inspector Word Doc
    '
    Dim wDoc As Word.Document
    Set wDoc = ActiveInspector.WordEditor
            
    '   Word Doc must be editable
    '
    If wDoc Is Nothing Then
        MsgBox "Active Inspector has no Word Editor.", vbExclamation
        Exit Function
    End If
    
    If wDoc.ProtectionType <> wdNoProtection Then
        MsgBox "Active Inspector is Locked For Editing (Read Only).", vbExclamation
        Exit Function
    End If
    
    '   Word Doc must have a Selection
    '
    Dim wDocSelection As Word.Selection
    Set wDocSelection = wDoc.Application.Selection
    If wDocSelection Is Nothing Then
        MsgBox "Active Inspector Selection is Nothing.", vbExclamation
        Exit Function
    End If
    If wDocSelection.Start = wDocSelection.End Then
        MsgBox "Active Inspector Selection is empty.", vbExclamation
        Exit Function
    End If
    
    '   Replace all occurances
    '
    Dim wDocSearch As Word.Range
    Set wDocSearch = Word_FindDefault(wDocSelection.Range.Duplicate)
    wDocSearch.Find.Text = FindTxt
    wDocSearch.Find.Replacement.Text = ReplaceText
    wDocSearch.Find.Execute Replace:=wdReplaceAll

Word_ReplaceInSelection = True
End Function

'   Reset a Word .Find object to defaults
'
'   From https://gregmaxey.com/word_tip_pages/words_fickle_vba_find_property.html
'
Private Function Word_FindDefault(ByVal wRange As Word.Range) As Word.Range

    Set Word_FindDefault = wRange
    With Word_FindDefault.Find
    
        .ClearFormatting
        .Format = False
        .Forward = True
        .Highlight = wdUndefined
        .IgnorePunct = False
        .IgnoreSpace = False
        .MatchAllWordForms = False
        .MatchCase = False
        .MatchPhrase = False
        .MatchPrefix = False
        .MatchSoundsLike = False
        .MatchSuffix = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .Replacement.ClearFormatting
        .Replacement.Text = ""
        .Text = ""
        .Wrap = wdFindStop

    End With
    
End Function
Reply With Quote
  #4  
Old 07-25-2025, 10:13 PM
Hornblower409's Avatar
Hornblower409 Hornblower409 is offline Select text, click button, hyphens replace spaces Windows 10 Select text, click button, hyphens replace spaces Office 2010
Competent Performer
 
Join Date: Jan 2025
Posts: 136
Hornblower409 will become famous soon enoughHornblower409 will become famous soon enough
Default

How to use the VBA Editor, Self Sign a Macro, and add a Ribbon Button to execute it.

How to use Outlook's VBA Editor | Slipstick Systems
Reply With Quote
  #5  
Old 07-26-2025, 11:52 AM
Hornblower409's Avatar
Hornblower409 Hornblower409 is offline Select text, click button, hyphens replace spaces Windows 10 Select text, click button, hyphens replace spaces Office 2010
Competent Performer
 
Join Date: Jan 2025
Posts: 136
Hornblower409 will become famous soon enoughHornblower409 will become famous soon enough
Default

I put my example code in a GitHub repo.
GitHub - Hornblower409/Outlook-Find_Replace: Outlook VBA Macro to Find and Replace text in the current Selection of an Outlook Item.
Reply With Quote
  #6  
Old 07-28-2025, 08:40 AM
brucemc777 brucemc777 is offline Select text, click button, hyphens replace spaces Windows 10 Select text, click button, hyphens replace spaces Office 2019
Advanced Beginner
Select text, click button, hyphens replace spaces
 
Join Date: Jul 2014
Location: Fredericksburg, VA
Posts: 48
brucemc777 is on a distinguished road
Default

Thank you June7 and Hornblower409!

Though how to assign a macro in Outlook is easy, i am amazed at how complicated they make a very simple find & replace in selection. Kind of like i found learning VBA in Access when i had some 20 years (with a heck of a lot of short-term memory loss) in Excel VBA, they might as well name "VBA" something completely different for each program, or at lease practically ALL of the functions-

Very much appreciate you folks stepping up to help!

Best Regards,
-Bruce
Reply With Quote
Reply



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 11:36 AM.


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