View Full Version : Word, Print Pages with Highlighted Text
railroad
03-01-2012, 02:42 PM
Hi all,
I'm not very sophisticated with VBA, but I would like to create a macro that prints only the pages of a an MS Word document that contains highlighted text. Any help would be much appreciated.
Regards.
gmaxey
03-01-2012, 03:31 PM
Word is not really a "page" based application but here is one crude method that might work:
\Sub ScrathMacro()
Dim MyPane As Pane
Dim MyPage As Page
Dim MyRect As Rectangle
Dim PageCount As Long
Dim RectCount As Long
Dim i As Long
Dim j As Long
Dim myRng As Range
Dim oPageRange As Word.Range
Set MyPane = ActiveWindow.ActivePane
PageCount = MyPane.Pages.Count
For i = 1 To PageCount
Set MyPage = MyPane.Pages(i)
RectCount = MyPage.Rectangles.Count
Set MyRect = MyPage.Rectangles(1)
For j = 1 To RectCount
Set MyRect = MyPage.Rectangles(j)
If MyRect.RectangleType = wdTextRectangle Then
Set myRng = MyRect.Range
Set oPageRange = myRng.Duplicate
With myRng.Find
.Highlight = True
If .Execute Then
oPageRange.Select
ActiveDocument.PrintOut Range:=wdPrintCurrentPage
End If
End With
Set myRng = Nothing
End If
Next
Next
End Sub
railroad
03-01-2012, 04:23 PM
Thank you! Could the code be modified so that it can be printed to one PDF file? When I run the script, it works perfectly but it wants to create multiple PDF files.
Thanks again.
gmaxey
03-01-2012, 04:29 PM
I'm going to have call UNCLE. It sounds like you are printing to a PDF printer. I suppose there is a way to append to an existing PDF but I am just not familiar with the process. Sorry.
railroad
03-01-2012, 04:52 PM
I appreciate your help. In case it is helpful, I think the reason this happens is because the script continuously prints the current page. I thought about trying to create a script that collected the page numbers with highlights and send a command to print all those page numbers, but wasn't even sure how to start once I got into it.
Frosty
03-01-2012, 08:21 PM
Does this help? Greg-- you're a marvel. I learn something new from you almost every time. I've never even heard of the Rectangles collection. Wacky.
Sub ScrathMacro()
Dim MyPane As Pane
Dim MyPage As Page
Dim MyRect As Rectangle
Dim PageCount As Long
Dim RectCount As Long
Dim i As Long
Dim j As Long
Dim myRng As Range
Dim oPageRange As Word.Range
Dim sPages As String
Set MyPane = ActiveWindow.ActivePane
PageCount = MyPane.Pages.Count
For i = 1 To PageCount
Set MyPage = MyPane.Pages(i)
RectCount = MyPage.Rectangles.Count
Set MyRect = MyPage.Rectangles(1)
For j = 1 To RectCount
Set MyRect = MyPage.Rectangles(j)
If MyRect.RectangleType = wdTextRectangle Then
Set myRng = MyRect.Range
Set oPageRange = myRng.Duplicate
With myRng.Find
.Highlight = True
If .Execute Then
oPageRange.Select
sPages = sPages & i & ", "
'ActiveDocument.PrintOut Range:=wdPrintCurrentPage, printtofile:=True
End If
End With
Set myRng = Nothing
End If
Next
Next
sPages = Left(sPages, Len(sPages) - 2)
MsgBox sPages
ActiveDocument.PrintOut Pages:=sPages, PrintToFile:=True, OutputFileName:="C:\Test.doc"
End Sub
As for the printing... railroad, you're on your own. There are too many variables between systems, but pressing F1 on the .PrintOut method in VBA will give you some pointers to other options you have using it. This at least builds the string and then waits to use it.
gmaxey
03-01-2012, 08:30 PM
A marvel? Ha! A piddler is more descriptive.
Frosty
03-01-2012, 08:33 PM
A bit more than a piddler.
Also, to the OP- I second Greg's reservations about whether this will work. The concept of "pages" in Word is pretty nebulous, so you can get in trouble relying on it too much.
But if all you're trying to do is identify pages (and not do any manipulating), then this *could* work.
One other thing to do is to turn off paragraph marks before you run this macro... hidden text, field codes... that can all affect what looks to be the pagination but actually isn't when you really print out the document.
In short-- don't trust this macro for a month of sundays and you've found it's never wrong.
fumei
03-01-2012, 10:03 PM
Just a comment. This will not work with versions prior to 2007, as there is no Page object.
Dim MyPage As Page
Just a suggestion...if something is truly version specific, it is probably a good idea to mention that.
gmaxey
03-02-2012, 04:42 AM
Good point.
Frosty
03-02-2012, 10:11 AM
Fumei-- that code works for me in Word 2003. I don't have a word 2000 machine to test as well, but just wanted to point that out that it does actually work in a prior version
I had actually checked to see if it was something new in 2007, since I'd never heard of it before, but rectangles and pages exist in 2003 (at least).
fumei
03-02-2012, 10:36 AM
Ah, yes they are in 2003. But not in 2002 (my version). Pages fails. They are not a valid object.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.