Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-23-2014, 10:27 AM
ljd108 ljd108 is offline Macro to delete text in specific styles Windows Vista Macro to delete text in specific styles Office 2010 32bit
Novice
Macro to delete text in specific styles
 
Join Date: Oct 2014
Posts: 24
ljd108 is on a distinguished road
Default Macro to delete text in specific styles

Hi,

I have found the following macro that deletes text in a particular style. I need to remove text that are made up of multiple styles in a document (about 15 of them) please could someone let me know if there is a quick way of adapting this macro to make it delete multiple styles rather than just "Heading 3"?

Thanks

Sub DeleteStyledText()
With ActiveDocument.Content.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 3")
Do While .Execute(FindText:="", Forward:=True, Format:=True) = True
.Parent.Delete
Loop

End With


End Sub
Reply With Quote
  #2  
Old 10-23-2014, 12:29 PM
macropod's Avatar
macropod macropod is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2010 32bit
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

Try:
Code:
Sub DeleteStyleText()
Application.ScreenUpdating = False
Dim strStyles As String, i As Long
strStyles = strStyles & "Style1,Style2,Style3,Style4,Style5,"
strStyles = strStyles & "Style6,Style7,Style8,Style9,Style10,"
strStyles = strStyles & "Style11,Style12,Style13,Style14,Style15"
With ActiveDocument.Content.Find
  .ClearFormatting
  .Format = True
  .Text = ""
  .Replacement.Text = ""
  .Wrap = wdFindContinue
  For i = 0 To UBound(Split(strStyles, ","))
    .Style = Split(strStyles, ",")(i)
    .Execute Replace:=wdReplaceAll
  Next i
End With
Application.ScreenUpdating = True
End Sub
where Style1 - Style15 are your actual Style names. You can use more Styles, or less; simply separate all the Style names with a comma.

PS: When posting code, please use the code tags - they're indicated by the # symbol on the posting menu.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-17-2014, 11:45 AM
ljd108 ljd108 is offline Macro to delete text in specific styles Windows Vista Macro to delete text in specific styles Office 2010 32bit
Novice
Macro to delete text in specific styles
 
Join Date: Oct 2014
Posts: 24
ljd108 is on a distinguished road
Default

Hi,

When I try and run this macro it comes up with an error in this part of the text:

For i = 0 To UBound(Split(strStyles, ","))
.Style = Split(strStyles, ",")(i)

Are you able to help with this at all?

Thanks
Reply With Quote
  #4  
Old 12-17-2014, 12:44 PM
ljd108 ljd108 is offline Macro to delete text in specific styles Windows Vista Macro to delete text in specific styles Office 2010 32bit
Novice
Macro to delete text in specific styles
 
Join Date: Oct 2014
Posts: 24
ljd108 is on a distinguished road
Default

Don't worry - have worked it out! thank you
Reply With Quote
  #5  
Old 03-24-2017, 03:59 AM
Shabbash Shabbash is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2013
Novice
 
Join Date: Mar 2017
Posts: 1
Shabbash is on a distinguished road
Default

Hi ljd108,

I'm trying to do something similar with above posted code as a baseline, but I'm getting the same line error:

.Style = Split(strStyles, ",")(i)

Could you please explain how you fixed this error?

Cheers!
Reply With Quote
  #6  
Old 03-24-2017, 04:15 PM
macropod's Avatar
macropod macropod is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2010 32bit
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

Without knowing what your implementation of strStyles contains and what the error message is, it's impossible to say.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-27-2017, 09:35 AM
Souriane Souriane is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2013
Advanced Beginner
 
Join Date: Feb 2017
Location: Quebec, Canada
Posts: 82
Souriane is on a distinguished road
Default

Hi!

All the styles mentionned in those lines bellow must exist in your list of available style. If you don't have one of the style in your list, you'll get an error 5834. Is it the one you get?

Code:
strStyles = strStyles & "Style1,Style2,Style3,Style4,Style5,"
strStyles = strStyles & "Style6,Style7,Style8,Style9,Style10,"
strStyles = strStyles & "Style11,Style12,Style13,Style14,Style15"
Reply With Quote
  #8  
Old 01-27-2018, 03:49 AM
jodecaesteker jodecaesteker is offline Macro to delete text in specific styles Windows 10 Macro to delete text in specific styles Office 2016
Novice
 
Join Date: Jan 2018
Posts: 6
jodecaesteker is on a distinguished road
Default

Interesting macro, especially because it works within a number of styles all at once.
I'm looking for a macro that replaces all occurences of double spaces, four spaces and tabs but only within 4 predefined styles. Could this macro be modified to do this?

I'm not really a programmer, but an architect, looking to automate my specifications and quantity system for building contractors.

Thanks in advance!
Reply With Quote
  #9  
Old 01-27-2018, 03:50 AM
macropod's Avatar
macropod macropod is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2010 32bit
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

Replace them with what?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 01-27-2018, 03:52 AM
jodecaesteker jodecaesteker is offline Macro to delete text in specific styles Windows 10 Macro to delete text in specific styles Office 2016
Novice
 
Join Date: Jan 2018
Posts: 6
jodecaesteker is on a distinguished road
Default

Wow, what a swift reply! ... Forgot to mention: replace them by triple spaces. It's a bit complicated to explain what I need that for. :-)
Reply With Quote
  #11  
Old 01-27-2018, 03:56 AM
macropod's Avatar
macropod macropod is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2010 32bit
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

Try:
Code:
Sub EditStyleSpaces()
Application.ScreenUpdating = False
Dim strStyles As String, i As Long
strStyles = "Style1,Style2,Style3,Style4"
With ActiveDocument.Content.Find
  .ClearFormatting
  .Format = True
  .Replacement.Text = "   "
  .MatchWildcards = True
  .Wrap = wdFindContinue
  For i = 0 To UBound(Split(strStyles, ","))
    .Style = Split(strStyles, ",")(i)
    .Text = "[ ]{2,4}"
    .Execute Replace:=wdReplaceAll
    .Text = "^t"
    .Execute Replace:=wdReplaceAll
  Next i
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 01-27-2018, 04:07 AM
jodecaesteker jodecaesteker is offline Macro to delete text in specific styles Windows 10 Macro to delete text in specific styles Office 2016
Novice
 
Join Date: Jan 2018
Posts: 6
jodecaesteker is on a distinguished road
Default

Wow, thanks! I just get an error on the line ".Execute Replace:=wdReplaceAll" (the first one): error 5560 (invalid search criterium). Could it have to do with the "[]{2,4}"? I changed it to " " (literally four spaces) and added two lines for the double spaces (.text = " " and .execute replace:wdReplaceAll)

Works great, thanks!
Reply With Quote
  #13  
Old 01-27-2018, 04:24 AM
macropod's Avatar
macropod macropod is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2010 32bit
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

Quote:
Originally Posted by jodecaesteker View Post
I just get an error on the line ".Execute Replace:=wdReplaceAll" (the first one): error 5560 (invalid search criterium). Could it have to do with the "[]{2,4}"?
The "[]{2,4}" in your reply is "[ ]{2,4}" in the code - a fundamental difference.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 01-22-2019, 05:46 AM
melp melp is offline Macro to delete text in specific styles Windows 10 Macro to delete text in specific styles Office 2010
Novice
 
Join Date: Jan 2019
Posts: 1
melp is on a distinguished road
Default

Hello I tried the below code but this is not working ie no text was deleted in my doc.
Could someone explain to me why?

Sub DeleteStyledText()
With ActiveDocument.Content.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 3")
Do While .Execute(FindText:="", Forward:=True, Format:=True) = True
.Parent.Delete
Loop
End With
End Sub

Thanks in advance

Mel
Reply With Quote
  #15  
Old 01-22-2019, 01:22 PM
macropod's Avatar
macropod macropod is offline Macro to delete text in specific styles Windows 7 64bit Macro to delete text in specific styles Office 2010 32bit
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

You don't need a loop for this. Indeed you don't even need a macro - a simple Find/Replace would do. That said, as a macro, you could use:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Style = "Heading 3"
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
  End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to delete text in specific styles Delete specific text Jackson Word VBA 16 12-06-2019 08:24 PM
Macro to delete text in specific styles Macro to change specific text styles ljd108 Word VBA 3 10-22-2014 03:19 PM
Macro to delete text in specific styles Delete specific mass text on document JadeRisley Word 4 07-17-2013 11:11 PM
Macro to delete text in specific styles Using macro to delete styles fogharty Word 1 10-10-2011 03:42 PM
Macro to delete text in specific styles Need help extracting specific text from one doument to another with macro/VBA. zsmithku Word 1 04-15-2011 03:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:32 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