Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-05-2022, 11:11 AM
JTell JTell is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Novice
Create Word macro to delete text throughout the entire document
 
Join Date: Jul 2022
Posts: 5
JTell is on a distinguished road
Default Create Word macro to delete text throughout the entire document

I need to create a macro that will turn on change tracking, delete all text that is formatted with red/underline, and then turns off change tracking. I have the macro working for one block of text at a time, but I'd like it to loop through the entire document.
I played a bit with Do Until loops, but this is my first time using Macros in Word and I suspect I am just not experienced enough to get it working... any help is appreciated.

Code:
Sub ClearRed()
'
' ClearRed Macro
'
'
ActiveDocument.TrackRevisions = True
Selection.Find.ClearFormatting
With Selection.Find.Font
.StrikeThrough = True
.DoubleStrikeThrough = False
.Color = wdColorRed
End With
With Selection.Find


.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Delete Unit:=wdCharacter, Count:=1
ActiveDocument.TrackRevisions = False
End Sub
Reply With Quote
  #2  
Old 07-05-2022, 12:42 PM
gmaxey gmaxey is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,224
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub ClearRed1()
Dim oRng As Range
ActiveDocument.TrackRevisions = True
  Selection.Find.ClearFormatting
  Set oRng = ActiveDocument.Range
  With oRng.Find
    With .Font
      .StrikeThrough = True
      .DoubleStrikeThrough = False
      .Color = wdColorRed
    End With
    While .Execute
      oRng.Delete
    Wend
  End With
  ActiveDocument.TrackRevisions = False
lbl_Exit:
  Exit sub

End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-06-2022, 05:50 AM
JTell JTell is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Novice
Create Word macro to delete text throughout the entire document
 
Join Date: Jul 2022
Posts: 5
JTell is on a distinguished road
Default

That works perfectly - thank you!
Is there any chance you can explain how that works? I have another macro that I need to loop through as well... it will find blue text with an underline, remove the formatting, cut it, turn on change tracking, paste it back in, then turn off change tracking. I've tried to mimic what you had, but it doesn't seem to work...

Code that works (without the loop):
Sub ChangeTrackBlueFont()
'
' ChangeTrackBlueFont Macro
'
'
Selection.Find.ClearFormatting
With Selection.Find.Font
.Underline = wdUnderlineSingle
.Color = wdColorBlue
End With
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Font.UnderlineColor = wdColorAutomatic
Selection.Font.Underline = wdUnderlineNone
Selection.Cut
ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions
Selection.PasteAndFormat (wdFormatOriginalFormatting)
ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions
End Sub
Reply With Quote
  #4  
Old 07-06-2022, 08:13 AM
gmaxey gmaxey is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,224
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

What you posted looks nothing like the solution I posted earlier.


Code:
Sub ChangeTrackBlueFont()
Dim oRng As Range
  Selection.Find.ClearFormatting
  Set oRng = ActiveDocument.Range
  With oRng.Find
    With .Font
      .StrikeThrough = True
      .DoubleStrikeThrough = False
      .Color = wdColorBlue
    End With
    While .Execute
      oRng.Delete
    Wend
  End With
  ActiveDocument.TrackRevisions = False
  Set oRng = ActiveDocument.Range
  Selection.Find.ClearFormatting
    With oRng.Find
    With .Font
      .Underline = wdUnderlineSingle
      .Color = wdColorBlue
    End With
    While .Execute
      With oRng.Font
        .UnderlineColor = wdColorAutomatic
        .Underline = wdUnderlineNone
      End With
      oRng.Cut
      ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions
      oRng.PasteAndFormat (wdFormatOriginalFormatting)
      ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 07-06-2022, 08:36 AM
JTell JTell is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Novice
Create Word macro to delete text throughout the entire document
 
Join Date: Jul 2022
Posts: 5
JTell is on a distinguished road
Default

I apologize if I was unclear - I had two macros that needed to loop. I asked the question on the first one, and was hoping that I'd be able to mimic the solution on my second one - but it didn't work.
So I posted the second one to try and get more information.

So you are right - the second one does not look like the first, as I was hoping to understand how to create the loop and do it myself... but it looks like you saved me the effort. It works flawlessly - thank you!
Reply With Quote
  #6  
Old 07-06-2022, 11:23 AM
JTell JTell is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Novice
Create Word macro to delete text throughout the entire document
 
Join Date: Jul 2022
Posts: 5
JTell is on a distinguished road
Default

I just noticed that these do not look at the content of footnotes - though they do seem to look at endnotes. Is there something additional that needs to be done to include looping through footnotes?
Reply With Quote
  #7  
Old 07-06-2022, 11:50 AM
gmaxey gmaxey is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,224
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

That is because we didn't search in the footnote or endnote story ranges. It also wouldn't find the formatted text in textboxes, headers or footers, comments etc.


Note: Your macros are not processing "real" endnotes.


To search in footnotes and "real" endnotes modify as follows:

Code:
Sub ClearRed1()
Dim oRng As Range
ActiveDocument.TrackRevisions = True
  Selection.Find.ClearFormatting
  Set oRng = ActiveDocument.Range
  With oRng.Find
    With .Font
      .StrikeThrough = True
      .DoubleStrikeThrough = False
      .Color = wdColorRed
    End With
    While .Execute
      oRng.Delete
    Wend
  End With
  On Error Resume Next
  Set oRng = ActiveDocument.StoryRanges(wdFootnotesStory)
  With oRng.Find
    With .Font
      .StrikeThrough = True
      .DoubleStrikeThrough = False
      .Color = wdColorRed
    End With
    While .Execute
      oRng.Delete
    Wend
  End With
  Set oRng = ActiveDocument.StoryRanges(wdEndnotesStory)
  With oRng.Find
    With .Font
      .StrikeThrough = True
      .DoubleStrikeThrough = False
      .Color = wdColorRed
    End With
    While .Execute
      oRng.Delete
    Wend
  End With
  On Error GoTo 0
  ActiveDocument.TrackRevisions = False
lbl_Exit:
  Exit Sub

End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 07-06-2022, 11:52 AM
gmaxey gmaxey is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,224
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

You might find this helpful:

Using a Macro to Replace Text Wherever It Appears in a Document
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 07-06-2022, 12:19 PM
JTell JTell is offline Create Word macro to delete text throughout the entire document Windows 10 Create Word macro to delete text throughout the entire document Office 2016
Novice
Create Word macro to delete text throughout the entire document
 
Join Date: Jul 2022
Posts: 5
JTell is on a distinguished road
Default

Thank you again - and that article is actually really helpful to explain what's going on. Much appreciated!
Reply With Quote
Reply

Tags
loop, macro

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Word macro to delete text throughout the entire document Macro to search for a particular word, copy the entire paragraph in w and use create a new document Productivity Word VBA 2 10-25-2019 06:40 AM
Create Word macro to delete text throughout the entire document Delete entire paragraph after key word jeffreybrown Word 2 07-27-2018 02:29 PM
Create Word macro to delete text throughout the entire document Need Word Macro to Delete Text rsrasc Word VBA 4 04-18-2018 11:32 PM
Microsoft Word macro to find text, select all text between brackets, and delete helal1990 Word VBA 4 02-05-2015 03:52 PM
Create Word macro to delete text throughout the entire document Macro to create new word doc and save the file using String found in the document VBNation Word VBA 2 02-08-2013 07:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:08 AM.


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