Printing in VB...

EMyles used Ask the Experts™
What is the best way to Print in VB?  I have had trouble in the past getting my forms to print nicely haveing the same quality as if it were printed from excel or word.  They would come out grey, not centered, etc.  What is the code for the best way to print?  Also, two of the forms that will be printed have DataGrids.  How to I make VB print the entire grid?  I mean, if the grid contains more than what can fit on one page, how to I make it continue printing the remainder of the grid to the next page with the next page having the same heading printed on it as the first page?  Thanks ahead of time!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Hello EMyles !

Try this if word does it fine.

If you don't know how automation works:

1. Create a reference to Microsoft Word 9.0 Object Library

    Dim appWord As Word.Application
    Call Clipboard.SetData Picture
    Set appWord = New Word.Application
    appWord.Visible = True
    set appWord=nothing

If you have Word you could simply record a MACRO (you have to paste the picture which you already have copied to the clipboard into the word-document and print it). Copy the macro-code to your vb-project an insert if where i have ...

But how to get the picture of the form into the clipboard ?


Sounds like you are using frm.Print - don't.

The simplest use is the printer object, eg:

printer.paintpicture [...]

This way you would handle the complete drawing of your report/output first then simply print it to the printer - of course, you could use the other printer methods such as .line and .print

By using paintpicture, you could pre-draw the image eg to a picture box (memory bitmap would be the most efficient, but certainly not the simplest).

The only way to handle printing of the entire grid would be to manually code the printing routines.


VK i did what you said and im getting an object variable not set error where it says 'selection.paste'... my code looks like:

Private Sub cmdPrint_Click()
Dim appWord As Word.Application
   Clipboard.SetData Picture1
   Set appWord = New Word.Application
   appWord.Visible = True
    Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
        wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
        Collate:=True, Background:=True, PrintToFile:=False, PrintZoomColumn:=0, _
        PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
   Set appWord = Nothing

End Sub
How does it know to copy the whole form into the picture box?

Nazdor...what goes into the brackets next to .paintpicture??   I need more im not getting it...Thanks guys!!
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Any StdPicture object, eg

  Printer.PaintPicture pic1.Picture


Im getting an 'argument not optional' with .paintpicture...I really need to be able to print with the best quality possible.  please help!  VK how do I copy the entire form in order to paste it to be printed?  

To answer both queries:

    Printer.PaintPicture Form1.Picture, 0, 0

However, I think you would benefit from reading the help on the printer object and .paintpicture which would have told you which arguments.

You've specifically asked for general information on how to do something, not for code to do it, so we would assume you would look how to do something, given an idea where to look.


Thanks VB help doesnt work for some reason...that is why I couldn't look it up...thank you for your help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial