#1
|
|||
|
|||
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 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 |
#2
|
||||
|
||||
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 |
#3
|
||||
|
||||
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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
||||
|
||||
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 |
#5
|
||||
|
||||
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] |
#6
|
||||
|
||||
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] |
#7
|
|||
|
|||
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... |
#8
|
|||
|
|||
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? |
#9
|
||||
|
||||
Quote:
Quote:
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] |
#10
|
|||
|
|||
Quote:
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. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Cannot see highlighted text for moving text in a do ument | Jonfrank1@me.com | Project | 1 | 01-09-2017 06:04 PM |
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 |
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 |