PDA

View Full Version : printout method



sessionone
10-03-2011, 04:26 AM
Hi all,

I've been looking for the answer for ages now, but can't seem to find it, I hope you guys may be able to help me.

Basically I have a "create letter" button and "print letter" on a word userform. The first button only creates a letter and the second creates and prints a letter straight away.

So what I don't understand is how can I put a single line "printout" to a "print letter" sub instead of copying and pasting the whole code of the "create letter" button to the "print letter" button click event and adding "printout" where it's needed. There's is a lot of code to paste, and I'm already finding it hard to edit it.

Thank you for help,
sessionone

Jay Freedman
10-03-2011, 10:59 AM
If the "create letter" subroutine is already there, then the "print letter" sub can just call it. Assuming the obvious names, the code behind the "print letter" button would be

Private Sub PrintLetter_Click()
Call CreateLetter
ActiveDocument.PrintOut Background:=False
End Sub

(and the keyword "Call" is optional, just the name CreateLetter alone on the line would be enough).

sessionone
10-04-2011, 08:00 AM
Thanks Jay,

Unfortunately, what's being printed out is the document on which the userform is being opened. I have button on the Word toolbar which opens the userform. So, when I click "Print Letter", the appropriate letter is created, but the original document, which opens when Word is started, is printed.

Thanks for your input though

sessionone

Jay Freedman
10-04-2011, 10:43 AM
The keyword ActiveDocument in the macro refers to whatever document has the focus at the time that statement executes. It appears that the CreateLetter macro is putting the focus into the original document before it exits. You'll have to examine the code of CreateLetter to figure out what it's doing.

If there's a statement near the end of CreateLetter that contains the Activate instruction to activate the original document, maybe you could remove (or comment out) that statement. Alternatively, maybe CreateLetter saves and closes the new document, and you could remove the Close statement. Of course, either of these things would change the behavior of CreateLetter when it's used alone.

sessionone
10-06-2011, 07:23 AM
Thanks again,

Yes, "Create letter" button saves the new letter, but doesn't close it. I don't have Activate statement in the subroutine. It just fills in the bookmarks and saves and that's all. I just can't think of a way to put the focus on the newly created document and just print it... :(

Cheers,
sesssionone