#1
|
|||
|
|||
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 |
#2
|
||||
|
||||
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 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] |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
Don't worry - have worked it out! thank you
|
#5
|
|||
|
|||
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! |
#6
|
||||
|
||||
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] |
#7
|
|||
|
|||
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" |
#8
|
|||
|
|||
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! |
#9
|
||||
|
||||
Replace them with what?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
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. :-)
|
#11
|
||||
|
||||
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] |
#12
|
|||
|
|||
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! |
#13
|
||||
|
||||
The "[]{2,4}" in your reply is "[ ]{2,4}" in the code - a fundamental difference.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
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 |
#15
|
||||
|
||||
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] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Delete specific text | Jackson | Word VBA | 16 | 12-06-2019 08:24 PM |
Macro to change specific text styles | ljd108 | Word VBA | 3 | 10-22-2014 03:19 PM |
Delete specific mass text on document | JadeRisley | Word | 4 | 07-17-2013 11:11 PM |
Using macro to delete styles | fogharty | Word | 1 | 10-10-2011 03:42 PM |
Need help extracting specific text from one doument to another with macro/VBA. | zsmithku | Word | 1 | 04-15-2011 03:46 PM |