Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-27-2022, 11:19 AM
NLDLC NLDLC is offline highlight text in array Windows 10 highlight text in array Office 2021
Novice
highlight text in array
 
Join Date: Jan 2022
Posts: 5
NLDLC is on a distinguished road
Default highlight text in array

Hi. I'm currently using the below VBA to find specific words (the myWords array) and then print those pages.



Code:
Sub PrintSpecificWords()
Dim myWords()
Dim j As Long
myWords = Array("Word1", "Word2", "Word3", "Word4", "Word5")

For j = 0 To UBound(myWords())
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
Do While .Execute(FindText:=myWords(j), _
Forward:=True) = True
Application.PrintOut _
Range:=wdPrintCurrentPage
Loop
End With
End With
Next j
End Sub
I have attempted unsuccessfully to modify this to include additional code which would highlight the text found within the array after printing it (I plan to use this as to reconcile which pages have been printed).

Is there a way to do this using my existing "myWords" array?
Reply With Quote
  #2  
Old 01-27-2022, 12:00 PM
gmaxey gmaxey is offline highlight text in array Windows 10 highlight text in array Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Sub PrintSpecificWordsII()
Dim myWords()
Dim lngIndex As Long
Dim oRng As Range
myWords = Array("Word1", "Word2", "Word3", "Word4", "Word5")
For lngIndex = 0 To UBound(myWords)
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = myWords(lngIndex)
.Forward = True
While .Execute
oRng.HighlightColorIndex = wdBrightGreen
Application.PrintOut Range:=wdPrintCurrentPage
oRng.Collapse wdCollapseEnd
Wend
End With
Next lngIndex
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-28-2022, 05:39 PM
NLDLC NLDLC is offline highlight text in array Windows 10 highlight text in array Office 2021
Novice
highlight text in array
 
Join Date: Jan 2022
Posts: 5
NLDLC is on a distinguished road
Default

Thank you, gmaxey! This is perfect.

Is there a way to "myWords" array can search for text that is included in a textbox? At this time the search appears to be missing words that are within textbox.
Reply With Quote
  #4  
Old 01-28-2022, 10:32 PM
gmayor's Avatar
gmayor gmayor is offline highlight text in array Windows 10 highlight text in array Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,105
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

Documents are created from an assortment of story ranges of which the ActiveDocument range is only one. Greg's macro addresses only that document range. The following will also address text boxes in that range. It will not however address text boxes within another range, such as the header range. If that is an issue, you will need a belt and braces approach to look through all the story ranges in the document.

Code:
Sub PrintSpecificWordsII()
Dim myWords()
Dim lngIndex As Long
Dim oStory As Range
    myWords = Array("Word1", "Word2", "Word3", "Word4", "Word5")
    For lngIndex = 0 To UBound(myWords)
        For Each oStory In ActiveDocument.StoryRanges
            With oStory.Find
                .ClearFormatting
                .Text = myWords(lngIndex)
                .Forward = True
                While .Execute
                    oStory.HighlightColorIndex = wdBrightGreen
                    Application.PrintOut Range:=wdPrintCurrentPage
                    oStory.Collapse wdCollapseEnd
                Wend
            End With
            If oStory.StoryType <> wdMainTextStory Then
                While Not (oStory.NextStoryRange Is Nothing)
                    Set oStory = oStory.NextStoryRange
                    With oStory.Find
                        .ClearFormatting
                        .Text = myWords(lngIndex)
                        .Forward = True
                        While .Execute
                            oStory.HighlightColorIndex = wdBrightGreen
                            Application.PrintOut Range:=wdPrintCurrentPage
                            oStory.Collapse wdCollapseEnd
                        Wend
                    End With
                Wend
            End If
        Next oStory
    Next lngIndex
lbl_Exit:
    Set oStory = Nothing
    Exit Sub
End Sub
__________________
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 01-31-2022, 04:06 PM
NLDLC NLDLC is offline highlight text in array Windows 10 highlight text in array Office 2021
Novice
highlight text in array
 
Join Date: Jan 2022
Posts: 5
NLDLC is on a distinguished road
Default

Hi. Thanks for helping me search with text/ word boxes. When using this version however the correct text, based off the array, is highlighted but a different page (other than the one which incudes the text from the array) ends up printing. Any idea and would cause this print issue? I wasn’t having it before.
Reply With Quote
  #6  
Old 02-01-2022, 11:06 AM
NLDLC NLDLC is offline highlight text in array Windows 10 highlight text in array Office 2021
Novice
highlight text in array
 
Join Date: Jan 2022
Posts: 5
NLDLC is on a distinguished road
Default

@gmaxey and @gmayor - In further testing both of these scripts further it it seems like the highlight aspect of it works (it highlights the given text in the array) but it does not print the page which includes the text in the array (instead it prints a seemingly random different page).
Reply With Quote
  #7  
Old 02-01-2022, 02:50 PM
Guessed's Avatar
Guessed Guessed is offline highlight text in array Windows 10 highlight text in array Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I assume that would be due to the code using ranges and not selection. What happens if you add the bold line in the position as shown
Code:
oStory.HighlightColorIndex = wdBrightGreen
oStory.Select
Application.PrintOut Range:=wdPrintCurrentPage
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 02-01-2022, 11:04 PM
NLDLC NLDLC is offline highlight text in array Windows 10 highlight text in array Office 2021
Novice
highlight text in array
 
Join Date: Jan 2022
Posts: 5
NLDLC is on a distinguished road
Default

Hi. This seem to have helped address the random pages printing. Thank you!

That said, i'm now running into the issue that pages that contain (but don't match) are printing. For example, "Word1" is in the array so "Word1", "Word10", "Word11", "Word 12", etc. also print.

I attempted to change:
Code:
With oStory.Find
to

Code:
.MatchWholeWord = True
With oStory.Find
however doing this didn't seem to allow the array to execute for "Word2", "Word3", "Word4", etc.

Is there a better way to require the find command to match the exact word?
Reply With Quote
  #9  
Old 02-02-2022, 03:30 PM
Guessed's Avatar
Guessed Guessed is offline highlight text in array Windows 10 highlight text in array Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,975
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I think you put the wholeword line in the wrong place. Try this variation on Graham's code
Code:
Sub PrintSpecificWordsII()
  Dim myWords() As String
  Dim lngIndex As Long
  Dim oStory As Range
  myWords = Split("Word1|Word2|Word3|Word4|Word5", "|")
  For lngIndex = LBound(myWords) To UBound(myWords)
      For Each oStory In ActiveDocument.StoryRanges
          With oStory.Find
              .ClearFormatting
              .Text = myWords(lngIndex)
              .Forward = True
              .MatchWholeWord = True
              While .Execute
                  oStory.HighlightColorIndex = wdBrightGreen
                  oStory.Select
                  Application.PrintOut Range:=wdPrintCurrentPage
                  oStory.Collapse wdCollapseEnd
              Wend
          End With
          If oStory.StoryType <> wdMainTextStory Then
              While Not (oStory.NextStoryRange Is Nothing)
                  Set oStory = oStory.NextStoryRange
                  With oStory.Find
                      .ClearFormatting
                      .Text = myWords(lngIndex)
                      .Forward = True
                      .MatchWholeWord = True
                      While .Execute
                          oStory.HighlightColorIndex = wdBrightGreen
                          oStory.Select
                          Application.PrintOut Range:=wdPrintCurrentPage
                          oStory.Collapse wdCollapseEnd
                      Wend
                  End With
              Wend
          End If
      Next oStory
  Next lngIndex
lbl_Exit:
  Set oStory = Nothing
  Exit Sub
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
like to highlight web text, enter cntl key, have text appended to same onenote pate bobk544 OneNote 0 12-10-2017 11:49 AM
highlight text in array Create an array or list of highlighted text in selection rjrichar40 Word VBA 1 09-03-2014 01:02 PM
highlight text in array Convert String Array to Integer Array from a User Input? tinfanide Excel Programming 4 12-26-2012 08:56 PM
highlight text in array Array into ComboBox + Macro-Text into ActiveDocument Vivi Word VBA 1 01-27-2010 07:03 AM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

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