Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-01-2021, 04:00 PM
jeffreybrown jeffreybrown is offline Find word and replace tab with two spaces Windows 10 Find word and replace tab with two spaces Office 2016
Expert
Find word and replace tab with two spaces
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default Find word and replace tab with two spaces

I have a paragraph



MESSAGE: What Storm Goers Need to Know

Between the "MESSAGE:" and the What is a tab character, but I would like to replace that tab character with two spaces.

This is the macro I found to find the paragraph that starts with MESSAGE, just not sure how to replace the tab character with two spaces.

Note: I added the With ORng.Find part to see if that would work. Apparently not.

Code:
Sub CleanUp()
    Dim oRng As Word.Range: Set oRng = ActiveDocument.Range
    With oRng.Find
            .Text = "MESSAGE:"
        While .Execute
            With oRng.Paragraphs(1).Range
                With oRng.Find
                    .Text = "^t"
                    .Replacement.Text = "  "
                End With
                    oRng.Find.Execute Replace:=wdReplaceAll
            End With
        Wend
    End With
End Sub
Reply With Quote
  #2  
Old 05-01-2021, 06:24 PM
Guessed's Avatar
Guessed Guessed is offline Find word and replace tab with two spaces Windows 10 Find word and replace tab with two spaces Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You are overthinking this as it doesn't need an inner search unless you want to replace every tab in that paragraph.
Code:
Sub CleanUp()
    Dim oRng As Word.Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .ClearFormatting
        .Text = "MESSAGE:^t"
        .Replacement.Text = "Message:  "
        .Execute Replace:=wdReplaceAll
    End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 05-01-2021, 06:53 PM
jeffreybrown jeffreybrown is offline Find word and replace tab with two spaces Windows 10 Find word and replace tab with two spaces Office 2016
Expert
Find word and replace tab with two spaces
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thanks Andrew. That makes so much more sense.
Reply With Quote
  #4  
Old 05-01-2021, 07:49 PM
jeffreybrown jeffreybrown is offline Find word and replace tab with two spaces Windows 10 Find word and replace tab with two spaces Office 2016
Expert
Find word and replace tab with two spaces
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Hi Andrew,

I added an array to attack more than one word, but also need to reset the left indent. Can't seem to find the right syntax.

This works for the array, but not the left indent.

Code:
Sub ReplaceWords()
    Dim MyList As Variant: MyList = Array("MESSAGE:", "SCRIPTURE:")
    Dim oRng As Word.Range: Set oRng = ActiveDocument.Range
    Dim i As Integer
    For i = 0 To UBound(MyList)
        With oRng.Find
            .ClearFormatting
            .Text = MyList(i) & vbTab
            .Replacement.Text = MyList(i) & "  "
            .Execute Replace:=wdReplaceAll
'            .ParagraphFormat.LeftIndent = InchesToPoints(0)
        End With
    Next i
End Sub
Reply With Quote
  #5  
Old 05-01-2021, 11:54 PM
Guessed's Avatar
Guessed Guessed is offline Find word and replace tab with two spaces Windows 10 Find word and replace tab with two spaces Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Firstly, there is a commonality there so you could search for "E:^t"
Secondly, you can replace formatting with a find but if you do, the actual text replacement doesn't happen at the same time. So I would do it with a pair of replacements - first just doing the formatting change, second doing the text change.
Code:
Sub CleanUp()
  Dim oRng As Word.Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .MatchCase = True
    .Text = "E:^t"
    .Replacement.ParagraphFormat.LeftIndent = InchesToPoints(0)
    .Execute Replace:=wdReplaceAll
    .Replacement.ClearFormatting
    .Replacement.Text = "E:  "
    .Execute Replace:=wdReplaceAll
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 05-02-2021, 05:02 AM
macropod's Avatar
macropod macropod is offline Find word and replace tab with two spaces Windows 10 Find word and replace tab with two spaces Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

This works for me:
Code:
Sub CleanUp()
With ActiveDocument.Range.Find
  .ClearFormatting
  .Text = "([MR][EI][PS][ST][AU][GR]E:)[^t^s ]@<"
  .Replacement.Text = "\1  "
  .Replacement.ParagraphFormat.LeftIndent = 0
  .Format = True
  .Forward = True
  .Wrap = wdFindContinue
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-02-2021, 07:40 AM
jeffreybrown jeffreybrown is offline Find word and replace tab with two spaces Windows 10 Find word and replace tab with two spaces Office 2016
Expert
Find word and replace tab with two spaces
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thank you both. These work great.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find word and replace tab with two spaces Find and replace query re missing spaces Johanna Word 3 11-22-2019 01:06 AM
Find word and replace tab with two spaces How to find and replace random empty spaces in beginning of lines? Pauliina Word 4 05-24-2018 10:01 PM
Find word and replace tab with two spaces Macro to find and replace specific types of spaces dita Word VBA 7 05-06-2018 12:47 AM
Find word and replace tab with two spaces Find and Replace/Insert 'CR', delete leading Spaces wherever a user Clicks in a Word document DavidL Word VBA 7 04-04-2018 12:01 AM
Find word and replace tab with two spaces Find and replace mutiple spaces between lowercase words only Dave T Word VBA 2 07-16-2015 11:23 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:33 PM.


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