Hello Jumpercable,
What you describe should work pretty well with the macro provided below.
The macro as written makes a few assumptions in the layout of your
information and how you want it printed. I am also including
instructions on how to change the macro if the assumptions are not
correct (or make a clarification request - I will be glad to make the
changes). I also describe where to change the file names for the
template references for your locations (for my tests I used template
files provided by Microsoft).
First, I suggest you create a "Macro Workbook" in the following way:
[1] Use Excel to create an empty workbook.
[2] Use the menu
Tools -> Macro -> Visual Basic Editor
at this point, Visual Basic should start and display some windows. In
the upper left should be one titled "projects".
[3] In the projects window, select the empty workbook (likely called
Workbook1). Use the menu
Insert -> Module
a window should appear on the right with a title like Module1.
[4] Copy / Paste the code below into that window. Fix the line breaks
if necessary. Near the top are three lines starting with
Letter$ =
Body$ =
Summary$ =
Replace the strings on each line with the full filename / path to each
of your template files. This will obviously vary with your set up but
look something like
Letter$ = "D:\Mytemplates\Letterhead.DOT"
where the drive letter is replace by the drive for your templates and
the directory / file name matches the location within that drive.
References to a file on a file server like
Letter$ = \\myserver\myvolume\Letterhead.DOT"
should work as well.
[5] Use the menu
Tools -> References ...
and a window will appear. Make sure that the line with Microsoft Word
Object Library is checked and then select OK.
At this point, the macro may need another change. I assumed that pages
2 through 6 (referred to as Sheets(2) through Sheets(7)) are to be in
one file. The same with the last four pages. If this is incorrect,
remove the comments (the ' symbol at the start of each line) so each
page is printed in a separate file.
Use the menu to "Close and Return to Microsoft Excel" and save the worksheet.
To use the macro worksheet, do the following:
[1] Open the macro worksheet. At this point, you may have to say - Yes
I want the macros active (the message varies by Excel version).
[2] Open the worksheet to print out
[3] Use the menu
Tools -> Macro -> Macros...
and select the PrintWS macro (it will probably look something like
Worksheet1!PrintWS - the file name will be whatever you named the
macro file)
[4] When you hit Run, the macro should run without error. And the last
worksheet will be displayed.
(no modifications to that worksheet)
You can repeat steps 2-4 as many times as needed for each worksheet.
Let me explain the macro as well:
Starting near the top, the macro activates Microsoft Word, and creates
a reference to it called "appWD".
The details for the "Letterhead" step follows:
appWD.Documents.Add Template:=Letter$ _
, NewTemplate:=False, DocumentType:=0
Create a new document based on the template specified by the name
assigned to Letter$.
Sheets(1).Select
Select the first sheet of the workbook (the "first page")
Selection.CurrentRegion.Select
Expand the selection to the "Current Region". This is the area around
the current cell that is bounded by blank rows / columns. If this is
NOT correct, make a clarification request and describe the range of
cells that need to be selected.
Selection.Copy
Copy the selection to the clipboard.
appWD.Selection.Paste
In Microsoft Word, Paste the clipboard to the template file. In the
Microsoft examples, this looks OK, but if it does not work in your
case, make a clarification request and I can suggest some alternative
methods to locate where to paste & paste the data.
appWD.ActiveDocument.PrintOut
Print the active document in Microsoft Word. There are a number of
options available - let me know if you need them explained.
appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
Close the active document in Microsoft Word w/o changes being saved
(does NOT modify the template).
The rest of the code works in a similar manner, modified only to refer
to different pages (sheets) and template files.
If any part of the answer is unclear or you have ANY problems with the
macro or how it works, please make a clarification request. I would be
glad to help you further on this problem.
--Maniac
PrintWS macro follows
Note - if some of the lines are split into two, be sure to fix those
after copy / pasting the code.
Sub PrintWS()
'
' PrintWS Macro
' Macro created on 10/27/2005 by Maniac
' Prints a worksheet in three sections (letterhead, body, summary)
' 10 pages total (one letterhead, six body, 3 summary)
'
Letter$ = "Accessory.DOT"
Body$ = "Bar.DOT"
Summary$ = "Capsule.DOT"
' Get Microsoft Word active and make a reference to it
Dim appWD As Word.Application
Set appWD = GetObject(, "Word.Application")
appWD.Visible = True
' Do letterhead processing
appWD.Documents.Add Template:=Letter$ _
, NewTemplate:=False, DocumentType:=0
Sheets(1).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
appWD.ActiveDocument.PrintOut
appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Do body processing
' Note: two alternatives available
' 1 - do five sheets in one file (current implementation)
' 2 - do five sheets separately (remove comments that follow)
appWD.Documents.Add Template:=Body$ _
, NewTemplate:=False, DocumentType:=0
Sheets(2).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
' appWD.ActiveDocument.PrintOut
' appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' appWD.Documents.Add Template:= Body$ _
' , NewTemplate:=False, DocumentType:=0
Sheets(3).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
' appWD.ActiveDocument.PrintOut
' appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' appWD.Documents.Add Template:= Body$ _
' , NewTemplate:=False, DocumentType:=0
Sheets(4).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
' appWD.ActiveDocument.PrintOut
' appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' appWD.Documents.Add Template:= Body$ _
' , NewTemplate:=False, DocumentType:=0
Sheets(5).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
' appWD.ActiveDocument.PrintOut
' appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' appWD.Documents.Add Template:= Body$ _
' , NewTemplate:=False, DocumentType:=0
Sheets(6).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
appWD.ActiveDocument.PrintOut
appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Do summary processing
' Note: two alternatives available
' 1 - do four sheets in one file (current implementation)
' 2 - do four sheets separately (remove comments that follow)
appWD.Documents.Add Template:=Summary$ _
, NewTemplate:=False, DocumentType:=0
Sheets(7).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
' appWD.ActiveDocument.PrintOut
' appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' appWD.Documents.Add Template:= Summary$ _
' , NewTemplate:=False, DocumentType:=0
Sheets(8).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
' appWD.ActiveDocument.PrintOut
' appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' appWD.Documents.Add Template:= Summary$ _
' , NewTemplate:=False, DocumentType:=0
Sheets(9).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
' appWD.ActiveDocument.PrintOut
' appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' appWD.Documents.Add Template:= Summary$ _
' , NewTemplate:=False, DocumentType:=0
Sheets(10).Select
Selection.CurrentRegion.Select
Selection.Copy
appWD.Selection.Paste
appWD.ActiveDocument.PrintOut
appWD.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End Sub |