Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word macro for printing pages with colored font

Status
Not open for further replies.

davlak39

Technical User
Oct 3, 2019
4
GB
Very new to programming and VBA, but I'm looking to develop a Word macro to allow me to print only those pages (e.g. of a several-hundred-page document) that contain text of a particular color font (e.g. red). I have an idea of how to execute some of the required elements (finding all pages with red font, identifying and returning the page number(s) of a selection) but that's as far as I've got. Thoughts greatly appreciated.
 
Ok, if yoiu've figured out how to find all the necessary page numbers then it is about as easy as:

[tt][blue]ActiveDocument.PrintOut Background:=False, Range:=wdPrintRangeOfPages, Pages:="1,7,11-17,22"[/blue][green] ' pages just an example - this is where you'd build your own page selection string from the pages you have identified[/green][/tt]
 
Paul, the OP only posted this here because he was advised to!
 
Haha, what a stir! Just to say - thanks very much strongm for the reply. I think identifying and 'building' a selection is perhaps the bit I'm struggling with, but I acknowledge this appears to be a fairly basic element for which there is plenty of guidance elsewhere. I'll go away, do my homework and have a play, and will come back with more specific questions as and when...
 
Ah, I assumed that "I have an idea of how to execute … finding all pages with red font, identifying and returning the page number(s)" mean you had that bit done!

I feel I should probably point out here that Word is a word processor, not a page layout program, so its concept of a page is somewhat hazy and it provides few reliable methods of dealing with a document on a page by page basis. That being said, try the following to build your page list

Code:
[blue]Public Function GetPageSet() As String
    Dim myPage As Page
    Dim myRect As Rectangle
    Dim PageSet As String
     
    For Each myPage In ActiveDocument.ActiveWindow.Panes(1).Pages
        For Each myRect In myPage.Rectangles
            With myRect.Range.Find
                .ClearFormatting
                .Font.Color = wdColorRed
                .Text = ""
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindStop
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute
                If .Found Then
                    PageSet = PageSet & myRect.Range.Information(wdActiveEndPageNumber) & ","
                    Exit For
                End If
            End With
        Next
    Next
    If PageSet <> "" Then GetPageSet = Left(PageSet, Len(PageSet) - 1)
End Function[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top