Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-29-2019, 01:57 AM
harpsichord harpsichord is offline How can I delete pages with no highlighted text? Windows 10 How can I delete pages with no highlighted text? Office 2010
Novice
How can I delete pages with no highlighted text?
 
Join Date: Apr 2019
Posts: 4
harpsichord is on a distinguished road
Default How can I delete pages with no highlighted text?

Hello all,




I am using the following module to highlight specific words in my word documents:

Code:
Sub HiLightList()
Application.ScreenUpdating = False
Dim StrFnd As String, Rng As Range, i As Long
StrFnd = "cat,pig,dog,whatever"
For i = 0 To UBound(Split(StrFnd, ","))
  Set Rng = ActiveDocument.Range
  With Rng.Find
    .ClearFormatting
    .Text = Split(StrFnd, ",")(i)
    .Replacement.ClearFormatting
    .Replacement.Highlight = True
    .Replacement.Text = "^&"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = True
    .Execute Replace:=wdReplaceAll
  End With
Next
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
It's working quite well but since I am working with thousands of Word pages of text a majority of which are irrelevant for me and contain zero information I need, I would like to know if there is a way i could somehow insert a code that would delete all pages that contain zero highlighted words? I don't need to extract only highlighted words, I need to preserve the whole text (specifically, it's 1-page-long newspaper articles) in which the highlighted words were found. Is there a simple way to do this and what would it be?


I am quite illiterate when it comes to coding or VBA or pretty much anything so I am relying on the kindness and skills of others willing to help me and write this down so I can simply insert it


Thank you!

Last edited by macropod; 04-29-2019 at 09:17 PM. Reason: Added code tags & restored formatting
Reply With Quote
  #2  
Old 04-29-2019, 06:45 AM
gmayor's Avatar
gmayor gmayor is offline How can I delete pages with no highlighted text? Windows 10 How can I delete pages with no highlighted text? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There are no 'pages' in a word document. The appearance of pages is an artifice of text flow. If you delete what word considers to be a displayed page, then the page flow is disrupted and the new 'page' structure changes. What you propose doesn't seem at all practical using VBA.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 04-29-2019, 06:36 PM
macropod's Avatar
macropod macropod is offline How can I delete pages with no highlighted text? Windows 7 64bit How can I delete pages with no highlighted 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

You should be able to do it with something like:
Code:
Sub DeletePages()
Application.ScreenUpdating = False
Dim p As Long, Rng As Range
With ActiveDocument
  For p = .ComputeStatistics(wdStatisticPages) To 1 Step -1
    Set Rng = .Range.GoTo(What:=wdGoToPage, Count:=p).GoTo(What:=wdGoToBookmark, Name:="\Page")
    If Rng.HighlightColorIndex = wdNoHighlight Then Rng.Delete
  Next
End With
Application.ScreenUpdating = True
End Sub
On my system, though, Word isn't implementing the .Goto correctly - seems an MS Update has broken it.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 04-29-2019, 09:11 PM
gmayor's Avatar
gmayor gmayor is offline How can I delete pages with no highlighted text? Windows 10 How can I delete pages with no highlighted text? Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Paul - that too was my first thought, but it didn't work for me either. However looking at it again, it seems to work if you add a line to the code

Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\Page")
Rng.Select
If Rng.HighlightColorIndex = wdNoHighlight Then Rng.Delete

Why that should be needed I am not sure, but it certainly changes the outcome.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 04-29-2019, 09:15 PM
macropod's Avatar
macropod macropod is offline How can I delete pages with no highlighted text? Windows 7 64bit How can I delete pages with no highlighted 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

Thanks Graham

Curious that adding Rng.Select changes things for you - I'm not seeing any such behaviour. Clearly Microsoft has broken something!
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 04-29-2019, 09:23 PM
macropod's Avatar
macropod macropod is offline How can I delete pages with no highlighted text? Windows 7 64bit How can I delete pages with no highlighted 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

Here's another way to skin that cat:
Code:
Sub DeletePages()
Dim p As Long
With ActiveDocument
  For p = .ComputeStatistics(wdStatisticPages) To 1 Step -1
    With .ActiveWindow.Panes(1).Pages(p).Rectangles(1).Range
      If .HighlightColorIndex = wdNoHighlight Then .Delete
    End With
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 04-30-2019, 02:06 AM
harpsichord harpsichord is offline How can I delete pages with no highlighted text? Windows 10 How can I delete pages with no highlighted text? Office 2010
Novice
How can I delete pages with no highlighted text?
 
Join Date: Apr 2019
Posts: 4
harpsichord is on a distinguished road
Default

The one with Rng.Select worked like a charm! Thank you so much, both of you, this will save me and my colleagues at work lot of time

One little thing - it removed all of the text from the headers in the document, is this preventable? It's not that relevant though...
Reply With Quote
  #8  
Old 04-30-2019, 02:23 AM
harpsichord harpsichord is offline How can I delete pages with no highlighted text? Windows 10 How can I delete pages with no highlighted text? Office 2010
Novice
How can I delete pages with no highlighted text?
 
Join Date: Apr 2019
Posts: 4
harpsichord is on a distinguished road
Default

Another thing - there will be documents with newspaper articles longer than one page in which case this won't work because it will not preserve the whole article if a highlighted word is found only in one of the pages of the article, if I understand correctly how you set the range.

In that case, I'm guessing that it could be possible to set ranges between two article titles because they are the only size 14 bold text in these documents?
Reply With Quote
  #9  
Old 04-30-2019, 02:48 AM
macropod's Avatar
macropod macropod is offline How can I delete pages with no highlighted text? Windows 7 64bit How can I delete pages with no highlighted 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

Quote:
Originally Posted by harpsichord View Post
One little thing - it removed all of the text from the headers in the document, is this preventable? It's not that relevant though...
If headers are being removed, that indicates there are Section breaks on the pages being deleted. Since you hadn't mentioned that as a possibility, it wasn't coded for.
Quote:
Originally Posted by harpsichord View Post
Another thing - there will be documents with newspaper articles longer than one page in which case this won't work because it will not preserve the whole article if a highlighted word is found only in one of the pages of the article, if I understand correctly how you set the range.
In other words, your reference to page deletion was nothing more than misdirection. It really would be helpful if you could actually specify what you want done up front, rather than waiting till something's been developed on the basis of entirely wrong specifications.
Quote:
Originally Posted by harpsichord View Post
In that case, I'm guessing that it could be possible to set ranges between two article titles because they are the only size 14 bold text in these documents?
If your articles use one of Word's Heading Styles for the articles, that would be quite easy.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 04-30-2019, 03:38 AM
harpsichord harpsichord is offline How can I delete pages with no highlighted text? Windows 10 How can I delete pages with no highlighted text? Office 2010
Novice
How can I delete pages with no highlighted text?
 
Join Date: Apr 2019
Posts: 4
harpsichord is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
If headers are being removed, that indicates there are Section breaks on the pages being deleted. Since you hadn't mentioned that as a possibility, it wasn't coded for.
In other words, your reference to page deletion was nothing more than misdirection. It really would be helpful if you could actually specify what you want done up front, rather than waiting till something's been developed on the basis of entirely wrong specifications.

If your articles use one of Word's Heading Styles for the articles, that would be quite easy.

The specifications I wrote were not wrong, I am dealing with one page articles right now exclusively and I was looking for a solution for that specific problem. Now that I solved that thanks to you, I am trying to predict problems in the next phase with a different set of data, that's all.

If I knew all the relevant things I need to mention I would probably be able to do the thing myself.

The articles don't use Word's Heading Styles.

Thank you.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I delete pages with no highlighted text? Cannot see highlighted text for moving text in a do ument Jonfrank1@me.com Project 1 01-09-2017 06:04 PM
How can I delete pages with no highlighted text? I need to convert shaded text into highlighted text on about 80 different long documents. VBA macro? AustinBrister Word VBA 8 05-28-2015 02:42 PM
How can I delete pages with no highlighted text? Formatting- Apply changes to highlighted text results in same change to other text sential Word 6 01-10-2014 03:22 PM
Delete button does not work with highlighted text edmund36 Word 3 10-17-2011 02:34 AM
Highlighted text won't delete - when I press enter Gague Word 2 07-09-2010 12:53 PM

Other Forums: Access Forums

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