![]() |
|
#1
|
|||
|
|||
|
I've struggled myself to do something working. I need to delete to a word document all text from specific heading and the text included to this. Is there any way to do that or a macro. I include 2 word documents one without delete one after delete (which is the one i want to do). Plz help!!!!!. |
|
#2
|
|||
|
|||
|
To the example i post, i wanted to delete text and heading, after heading 3 but this could be changed, i have 5000 pgs to do that. Other time maybe is after heading 4.
|
|
#3
|
||||
|
||||
|
Hi Jackson,
It could possibly be done with a macro, but not with an ordinary Find/Replace. You'd need to clarify what you mean by "i wanted to delete text and heading, after heading 3 but this could be changed" and "Other time maybe is after heading 4", though. How is the macro supposed to know which heading to act on and whether you want something deleted or changed?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#4
|
|||
|
|||
|
Quote:
|
|
#5
|
||||
|
||||
|
Hi Jackson,
In that case, try: Code:
Sub DeleteH9Text()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Style = "Heading 9"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
Set Rng = .Paragraphs(1).Range.Duplicate
With Rng
On Error GoTo ParaLast
While InStr(.Paragraphs.Last.Next.Style, "Heading ") = 0
.MoveEnd wdParagraph, 1
Wend
ParaLast:
.Delete
End With
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#6
|
|||
|
|||
|
Macro said run time error 5834. And when i click to debug makes yellow the .Style= "Heading 9"
|
|
#7
|
||||
|
||||
|
Hi Jackson,
That suggests the 'Heading 9' Style doesn't exist in the document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#8
|
|||
|
|||
|
I try this and i delete the left over code. The same error and the same yellow text. Look at this i will give you my document to try, to see what happening. I have inside this macro.
|
|
#9
|
||||
|
||||
|
Hi Jackson,
The macro runs fine for me in your document, without any errors. At the same time, however, the macro doesn't do anything because your document doesn't have any text in Word's 'Heading 9' Style. Your 'Heading 9' text is in a Style named 'MM Topic 9'. If that's what you're really working with, you need to change both 'Heading ' strings in the macro to 'MM Topic '.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#10
|
|||
|
|||
|
Quote:
What i must to change if i want to delete Heading 7 and the text below this and not text below 9 heading? |
|
#11
|
||||
|
||||
|
hi Jackson,
Try: Code:
Sub DeleteH7Text()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Style = "Heading 7"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
Set Rng = .Paragraphs(1).Range.Duplicate
With Rng
On Error GoTo ParaLast
Do
Select Case .Paragraphs.Last.Next.Style
Case "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Heading 5", "Heading 6"
Exit Do
Case Else
.MoveEnd wdParagraph, 1
End Select
Loop
ParaLast:
.Delete
End With
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
You could use the same basic code for any heading level - simply change the heading # to find, then adjust the list of headings in the 'Case' statement to suit.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#12
|
|||
|
|||
|
I try this code and working, but i ve see that this deletes all text after heading 9. So if i have a document with many headings and some of them have the heading 9, after the heading 9 deletes all text even if is text of heading 1, 2, or other. I need this to only deletes heading 9 and the text below this heading not the text below this heading and all the document. Plz i include what need to do, before and after in the attachments with the macro.
|
|
#13
|
||||
|
||||
|
Hi Jackson,
Your document still does not use Word's Heading Styles. Did you make the 'MM Topic ' changes I mentioned in my previous post? The macro does not delete Heading 1-6, or any text associated with them - only Heading 7-9 and the text associated with them. If you want to preserve Heading 9 and the text associated with it, but not Heading 8 and its text, that makes things much more complicated for the macro. What you could do is to: 1: add ', "Heading 9"' to the 'Case' statement; and 2. run the macro twice, the first time with '.Style = "Heading 8"' and the second time with '.Style = "Heading 7"'.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#14
|
|||
|
|||
|
Thread solved!!! thanks very much!!!! You are genius.....
|
|
#15
|
|||
|
|||
|
@macropod
Thank you for this code. This is brilliant! Question if I could. I modified this for Heading 1 tags. I found that the code above DOES NOT delete the nested heading 2 and heading 3 tags inside of heading 1. Any idea why that might be? |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Convert numbers to a specific text string
|
francis | Excel | 1 | 10-06-2011 01:43 PM |
delete all bookmark text
|
hklein | Word VBA | 4 | 08-10-2011 04:33 AM |
Need help extracting specific text from one doument to another with macro/VBA.
|
zsmithku | Word | 1 | 04-15-2011 03:46 PM |
| Delete text | Michael007 | Office | 13 | 01-01-2011 10:38 PM |
Selecting specific text out of a series of columns
|
speedycorn1 | Word | 3 | 11-01-2010 02:58 PM |