Can I print selected lines using a Macro in Word

W

Waitsu

I want to create a form in Word but I only want specific lines to print on
each day of the week.

For Example

On Sunday I want to print lines 1 - 5 , 10,12,16,20-40
On Monday I want to print lines 1 - 5, 11,13,17,20-40
On Wednesday I want to print lines 1 - 5, 14, 18, 20-40
etc...

I can create multiple documents but I am trying to keep this information in
one document for ease of updating.
 
J

Jay Freedman

I want to create a form in Word but I only want specific lines to print on
each day of the week.

For Example

On Sunday I want to print lines 1 - 5 , 10,12,16,20-40
On Monday I want to print lines 1 - 5, 11,13,17,20-40
On Wednesday I want to print lines 1 - 5, 14, 18, 20-40
etc...

I can create multiple documents but I am trying to keep this information in
one document for ease of updating.

This can be done.

Start by defining seven paragraph styles in the form template, naming them with
the days of the week. They can all be based on Normal style (or on Body Text
style) with identical formatting; only the seven different names are important.
In the body of the template, apply each style to every line that should appear
only on that day; in your example, the style named Monday would be applied to
lines 10, 12, and 16, while lines 1-5 and 20-40 should stay in Normal style so
they appear on every day.

You can make a choice at this point: If the form is a protected form with form
fields, you can put a dropdown form field somewhere in the form and put the
seven day names into it as the choices. If it isn't already a protected form,
there's no need to make it one; instead you can create a custom dialog (a
"userform") to pop up and let you select the day. Finally, if you'll always be
printing the form for the current day (that is, you never want to print a form
for tomorrow or some other day of the week), you can just program the macro to
automatically use the current day's name.

Now you need a macro that changes the Hidden attribute to True for each style
except the one for the chosen day, prints the document, and then restores all
the styles to visibility. Name the macro FilePrint so it will run instead of the
built-in Print command. Here's the code for a protected form containing a
dropdown named fldDay:

Sub FilePrint()
Dim currDay As String
currDay = ActiveDocument.FormFields("fldDay").Result

ChangeStyles currDay

Application.ScreenRefresh

Dialogs(wdDialogFilePrint).Show

UnChangeStyles
End Sub

Private Sub ChangeStyles(StyleName As String)
' Hide all the day styles, then unhide one
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

.Styles("Monday").Font.Hidden = True
.Styles("Tuesday").Font.Hidden = True
.Styles("Wednesday").Font.Hidden = True
.Styles("Thursday").Font.Hidden = True
.Styles("Friday").Font.Hidden = True
.Styles(StyleName).Font.Hidden = False

.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

Private Sub UnChangeStyles()
' Hide all the day styles, then unhide one
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

.Styles("Monday").Font.Hidden = False
.Styles("Tuesday").Font.Hidden = False
.Styles("Wednesday").Font.Hidden = False
.Styles("Thursday").Font.Hidden = False
.Styles("Friday").Font.Hidden = False

.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub
 
G

Gordon Bentley-Mix on news.microsoft.com

It can probably be done, but it would depend a lot on how your document is
configured and your programming skills.

One approach would be to wrap bookmarks around each of the "variable"
sections and then write code to hide/show the various sections according to
the current day of the week. Then you could simply print the document making
sure not to print hidden content.

The macro could be written to automatically hide/show the variable content
automatically when the document is opened, or it could be invoked through a
toolbar button or keyboard shortcut.

If you would like to pursue this course, I suggest that you at least attempt
to create the macro on your own and then post a follow up to the Word
Programming newsgroup if you run into any problems.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
W

Waitsu

Thanks - My Word skills are fairly good but have not done too much with
macros. i am alweays looking for a challenge so I will give both suggestions
a try and see if I can make it work.
 
W

Waitsu

OK - I am able to hide the lines that I do not want to see manually, i have
not worked on the macro yet.

My next issue is that the text is in a table, when I hide the items I do not
want to see, I get a blank box - is there any way to hide the table row that
is empty or am I out of luck on that?

Thanks
 
J

Jay Freedman

If you select the entire row, including the row marker in the right margin
(which is visible if you display nonprinting characters with the ¶ button), and
then apply the paragraph style to that, then the table row will hide when you
set the style's Hidden attribute to True. If you select any less than the entire
row before applying the style, then the row won't hide.
 
W

Waitsu

Thanks - that does hide the row.

The next issue that I am having is that when I hide any text it adds
multiple blank pages in the document. When I unhide the font the blank pages
dissappear.

Here is the piece of code that is hiding the text:

If MyStr = "Sunday" Then
.Styles("Sunday").Font.Hidden = False
.Styles("Monday").Font.Hidden = True
.Styles("Tuesday").Font.Hidden = True
.Styles("Wednesday").Font.Hidden = True
.Styles("Thursday").Font.Hidden = True
.Styles("Friday").Font.Hidden = True
.Styles("Saturday").Font.Hidden = True
.Styles("Not Sunday").Font.Hidden = True
.Styles("Normal").Font.Hidden = False

I am doing this because there are a couple of days that I need items hidden
that I need to see every other day of the week.

Thanks
 
J

Jay Freedman

I can't think of any reason that _hiding_ anything should cause new blank
pages to appear. Something is seriously wrong here. If you email a copy of
the template to me, I'll try to figure out what's happening.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top