![]() |
|
#1
|
|||
|
|||
|
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
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 |
|
#2
|
||||
|
||||
|
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
|
|
#3
|
||||
|
||||
|
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
|
|
#4
|
||||
|
||||
|
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 |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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 |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Find, select, and replace part of text with bold
|
paik1002 | Word VBA | 4 | 12-07-2015 11:24 PM |
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 |