Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-31-2011, 04:24 AM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default Delete specific text


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!!!!!.
Attached Files
File Type: docx after delete..docx (14.8 KB, 14 views)
File Type: docx without delete.docx (15.0 KB, 11 views)
Reply With Quote
  #2  
Old 12-31-2011, 04:26 AM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 12-31-2011, 09:21 PM
macropod's Avatar
macropod macropod is offline Delete specific text Windows 7 64bit Delete specific text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

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]
Reply With Quote
  #4  
Old 01-01-2012, 02:43 AM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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?
Ok I need to delete The Heading 9 and the text after that Heading and not heading 3 or 4 as i said before.
Reply With Quote
  #5  
Old 01-01-2012, 04:32 AM
macropod's Avatar
macropod macropod is offline Delete specific text Windows 7 64bit Delete specific text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

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
This should do the job unless someones created their own Style named 'Heading 10' or something such.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 01-01-2012, 05:23 AM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default

Macro said run time error 5834. And when i click to debug makes yellow the .Style= "Heading 9"
Reply With Quote
  #7  
Old 01-01-2012, 04:12 PM
macropod's Avatar
macropod macropod is offline Delete specific text Windows 7 64bit Delete specific text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Jackson,

That suggests the 'Heading 9' Style doesn't exist in the document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 01-01-2012, 06:20 PM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default The same 5834 error and yellow the style heading 9

Quote:
Originally Posted by macropod View Post
Hi Jackson,

That suggests the 'Heading 9' Style doesn't exist in the document.

PS: You can delete the 'i = i + 1' line - it's left over from the code development.
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.
Attached Files
File Type: docx Tester.docx (15.9 KB, 11 views)
Reply With Quote
  #9  
Old 01-01-2012, 06:45 PM
macropod's Avatar
macropod macropod is offline Delete specific text Windows 7 64bit Delete specific text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

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]
Reply With Quote
  #10  
Old 01-02-2012, 03:23 AM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default Worked!!

Quote:
Originally Posted by macropod View Post
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 '.
Thanks Makropod, working fine you was right. This named with a different name.
What i must to change if i want to delete Heading 7 and the text below this and not text below 9 heading?
Reply With Quote
  #11  
Old 01-02-2012, 03:56 AM
macropod's Avatar
macropod macropod is offline Delete specific text Windows 7 64bit Delete specific text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

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
Note: As before, if you're using 'MM Topic ' Styles and note 'Heading ' Styles, you'll need to make the corresponding changes to the code.

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]
Reply With Quote
  #12  
Old 01-02-2012, 04:00 AM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default Major problem

Quote:
Originally Posted by Jackson View Post
Thanks Makropod, working fine you was right. This named with a different name.
What i must to change if i want to delete Heading 7 and the text below this and not text below 9 heading?
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.
Attached Files
File Type: doc Initial Document.doc (26.5 KB, 10 views)
File Type: doc Document after running the macro you ve made.doc (24.5 KB, 11 views)
File Type: docx This is what i need to do..docx (15.4 KB, 14 views)
Reply With Quote
  #13  
Old 01-02-2012, 04:15 AM
macropod's Avatar
macropod macropod is offline Delete specific text Windows 7 64bit Delete specific text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

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]
Reply With Quote
  #14  
Old 01-02-2012, 05:08 AM
Jackson Jackson is offline Delete specific text Windows 7 32bit Delete specific text Office 2010 32bit
Novice
Delete specific text
 
Join Date: Dec 2011
Posts: 8
Jackson is on a distinguished road
Default

Thread solved!!! thanks very much!!!! You are genius.....
Reply With Quote
  #15  
Old 12-06-2019, 03:26 PM
noslenwerd noslenwerd is offline Delete specific text Windows 10 Delete specific text Office 2016 for Mac
Novice
 
Join Date: Dec 2019
Posts: 15
noslenwerd is on a distinguished road
Default

@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?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete specific text Convert numbers to a specific text string francis Excel 1 10-06-2011 01:43 PM
Delete specific text delete all bookmark text hklein Word VBA 4 08-10-2011 04:33 AM
Delete specific text 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
Delete specific text Selecting specific text out of a series of columns speedycorn1 Word 3 11-01-2010 02:58 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:18 AM.


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