Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 



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:20 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