Printing from VB Express

I have come across several posts indicating that it is nearly impossible to print documents, much less interact with Word, when using Visual Basic Express. Is this true If so, will this functionality ever be included or will I have to either purchase Visual Studio or learn another language


Answer this question

Printing from VB Express

  • Andrew Baker

    1st indication: When I use the methods such as Print: VB2005 abort with error

    2nd indication:The only times that I have success is in Step 1: it works, but steps 2 - 5, with error. The line code with error is Dim pd As TextFilePrintDocument = New TextFilePrintDocument(streamToPrint)

    'I change it with (the error stop but don'print anithing):

    Dim pd As PrintDocument = New PrintDocument()

    In other samples in GotDotNet, he says that I need install Framework SDK 1 and it needs IIS.

    Microsoft makes the Print very very problematic, and in the most of the tests: don't works.

    Hours, days and nights with no success. Only problems. It's a crazy world.

    3rd indication:

    The Alex Calvo article assumes you're familiar with C# and Windows Forms, but I have VB2005.

    Sorry and Thanks.

    Simeao



  • MajaA

  • Delusion7

    I assure you all that printing is actually very simple in the .NET Framework. It's the way people look at the problem that complicates things. You create a PrintDocument, you call its Print method, you handle its PrintPage event. You then draw whatever you want on the printing surface. If you want a string you call DrawString, if you want a line you call DrawLine, if you want an image you call DrawImage, etc. Once you've finished drawing a page, you set e.HasMorePages to either True or False, depending on whether there are more pages to print or not. I don't see what's hard about that. Is it the fact that you have to do some mathematical calculations to determine where to place objects on the page I for one am glad you do because that gives me control, to the pixel, over where everything goes. Like so many things, you just have to think about how you would do something in the physical world, like writing on paper with a pen, then translate the algorithm to code. That's what developers do.
  • Adeze

    I can't see how printing can get much easier. Sure, you could come up with a print designer - Word or Crystal Reports comes to mind - but then you would have: Word and Crystal Reports.

    This seems to crop up with amazing regularity, and more and more. Personally, I found VB6 very hackish when it came to printing but worked OK with text, but was awful for graphics. VB2005 has much improved control over text printing and excellent graphics support. VB isn't a word processor or WYSIWYG editor, but you could make it one if you like.

    I'm interested in what you are expecting to be able to do.



  • Avinash21191

    jmcilhinney, I agree to a point with you that a lot of people would like to be able to just call Print and be done with it. BUT, not everyone here is a developer. Some are people being put into the position of developing an application by circumstances or because they are trying to learn VB2005. ( look at which forum we are discussing this in.......VB Express). Given the fact that currently, VB2005 Express is available free, there will be a lot of new, non-programmers, or people used to using VBA, downloading and trying VB2005. So, it is not safe to assume that everyone here is a developer, in the sense that they program as a profession.

    I agree that VB2005 has a lot of power when it comes to printing. But, it is not always that easy to grasp, especially when a person may be wrestling with learning the language too. And what documentation that is available, is not very clear to some people. I have wished on more than one occasion, that someone would write a good book on printing in Visual Basic. ( any version) If someone were to write a book on nothing but Printing in VB2005, I would be the first in line to purchase it. I am one of those people that learn not only by reading, but also, by example.

    james

    aka:trucker


  • umpate22

    I'm sort of in the same boat as jeffg90, I work for a bank and I have an Excel VBA app that prints temporary MICR Checks and I would like to convert it to VB. The check stock comes to us blank, so I will need to produce every line, underline, & graphic, and I will also need to use several different fonts, including a special MICR E13-B font. This was pretty easy to setup in excel, but I have held off on beginning to work on it with VB2005 because I don't really know where to start with creating a (temporary) document with all of that formatting and underlines and everything. I have a good grasp of the Visual Basic language, but not necessarily of different aspects of the framework.

    Any help anyone can give me in getting started would be greatly appreciated.

  • dmestel

    My self-trainig begin Nov,7 2005 with the VB2005. Several articles tested (more 2000 pages) are for VB6 and the result is ERRORS. In MSDN'Spain I found a course the all the samples works in VB2005. If MSDN allert what examples works in VB2005, will be the best way for amatteurs like me.

    Why study VB6 if is the excelent and free VB2005 to begin. It WAS a good solution, but today, for newbies, why not go directly to VB2005.

    Thanks for yous attention



  • hadstj

    I'm with you SJW. Everbody just wants to be able to call Print and have it all done for them. We're developers people. We're the ones who write the code. .NET printing gives you absolute control over every aspect of your print job, down to the pixel. There has to be a certain amount of complexity involved with that much power, but the PrintDocument does a good job of balancing power and ease of use. The problem is that people don't understand events and scope properly, so they can't cope with having the PrintPage event raised multiple times for multiple pages, or else they don't understand how to use GDI+ so they can't put what they want where they want. Printing is just a matter of making a few measurements to get locations and calling the appropriate method of the Graphics object to draw something.
  • jodonnell74

    A option is to send datas to Excel and use their good capabilities to print

    A list of articles:

    Q245017 - Versions: WinNT Level: Intermediate
    How to: Redirect a DOS Application Print Job to a Network Printer

    Q252388 - Versions: WinNT Level: Intermediate
    HOWTO: Add a Default Printer for All New Users

    Q190411 - Versions: VB6 Level: Beginner
    HOWTO: Bind a DataReport To an ADO Recordset at Run Time

    Q254496 - Versions: VB5 VB6 Level: Intermediate
    HOWTO: Change Printer Settings for Kodak Image Edit Control

    Q154007 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Connect to a Network Printer with the Win32 API

    Q242045 - Versions: VB4 VB5 VB6 Level: Intermediate
    HOWTO: Control Line Spacing When Printing From Visual Basic

    Q194789 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Determine Available PaperBins With DeviceCapabilities API

    Q264036 - Versions: VB6 Level: Advanced
    HOWTO: Determine Whether a Printer Is a PostScript Printer

    Q181276 - Versions: VB5 VB6 Level: Intermediate
    HOWTO: Display Extended ASCII WingDings on DBCS Systems

    Q240019 - Versions: VB6 Level: Beginner
    HOWTO: Dynamically Populate a Data Report in Visual Basic

    Q231726 - Versions: VB6 Level: Intermediate
    HOWTO: Export a DataReport to an RTF file

    Q251329 - Versions: VB6 Level: Beginner
    HOWTO: Modify the Command of a Hierarchical DataReport

    Q198860 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Open the Printer Properties Dialog

    Q194580 - Versions: VB5 VB6 Level: Intermediate
    HOWTO: Print a Composite Image From a RichTextBox

    Q248882 - Versions: VB5 VB6 Level: Intermediate
    HOWTO: Print a Range of Pages with the CommonDialog Control

    Q175083 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Print Line-by-Line to a Printer

    Q193379 - Versions: VB5 VB6 Level: Intermediate
    HOWTO: Print Preview in Visual Basic Applications

    Q244779 - Versions: VB6 Level: Intermediate
    HOWTO: Refresh a Parameterized DataReport

    Q143274 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Retrieve Printer Name from Windows 95 Registry in VB

    Q190218 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Retrieve Settings From a Printer Driver

    Q154078 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Send Raw Data to a Printer Using the Win32 API from VB

    Q243075 - Versions: Win95 Win98 WinNT Level: Intermediate
    HOWTO: Troubleshoot Network Printing Problems

    Q178076 - Versions: VB5 VB6 Level: Intermediate
    HOWTO: Use a PictureBox to Control Orientation Printing a Form

    Q193943 - Versions: VB4/32 VB5 VB6 Level: Intermediate
    HOWTO: Use GetDeviceCaps to Determine Margins on a Page

    A excellent article is in portuguese (VB6 again...):

    http://www.macoratti.net/printer.htm

    Simeao (simeaogomes@brturbo.com.br - Agronomist Engeneer)



  • Caioshin

    While not impossible to print in VB 2005 ( this includes the Express Edition too) it takes a bit of work to get the results you want. There are a lot of printing capabilities in VB 2005, but, you need to do some reading to maximize your results. Doing a Google Search in the Groups section will find a lot of info on printing in VB.NET and VB2005. I think with time there will be more simplified printing features added to VB. I seriously doubt you will find another language that has better printing ability than VB currently has available to it. I wouldn't purchase Visual Studio 2005 , just for Printing alone. For one reason, it will not give you any added printing abilities. Check out the product comparison page and you will see some of the major differences between the different editions of Visual Studio 2005 compared to the Express Editions ( single language).

    james

    aka:Trucker


  • MarkDeibert

    I have the same problem. See http://msdn.microsoft.com/vbrun/greatesthits/default.aspx pull=/library/en-us/dnexpvb/html/printingtext.asp

    is a good article. The problem is the same: the oldies VB6

    Simeao - from Pelotas (RS) Brasil



  • Anthony Wong 6

    Checks are quite easy to print: if you have a ruler and a piece of paper, you have your font. (and color, of course). To print a line at a specific location is a single line of VB code.

    However, you have to ask yoursef - if it's so easy to do what you want from Excel, why are you moving away from it There's no point in complaining how bad cadillacs are because they don't climb mountains. It's all a trade off. I recommend you actually try it before slamming it for being incapable. What you've 'heard' may not be a true representation of capability: what you can do with VB is purely down to the capability of the nut behind the keyboard.



  • dfiorucci

    Thanks for the comments. I am a Medical Physicist (Cancer Center) working on moving my Excel Apps to VB. As you might expect in a hospital setting documentation is very important and required - at least for the time being. Anyway, as you are well aware it is easy to print whatever report from Excel or Word. However, VBA is limited. Therfore, I am converting to VB and am just learning VB. Thus, if there was an easy way to add to data to a word file and then print it from VB I was going to be all over it - even if it meant buying VB Studio. If I know that it will just require coding then I will work on that.

    Thanks again for your hep. By the way VB is proving to be awesome.

    Jeff


  • Stylius

    What you really need to work with VB2005 Express Edition is the Dotnet Framework SDK 2.0 at this link:

    http://www.microsoft.com/downloads/details.aspx FamilyID=FE6F2099-B7B4-4F47-A244-C96D69C35DEC&displaylang=en

    It is a very large download ( 354MB) but, if you can download it, and install it , you will have a lot of good examples for printing and a complete Help system for VB2005.

    It is well worth the time to download ( unless you are on dialup of course).

    As for the problem with TextFilePrintDocument, there is no such thing currently in the Framework 2.0. The code you are using may be a leftover from one of the Beta releases of the 2.0 Framework and VB2005 and was left out of the final release.

    There were some examples on GotDotNet and the CodeProject that used code from the betas and have not been updated to reflect the fact that specific functions and namespaces from the beta framework did not make it into the final release.

    It would be a good idea at the top of your project to include this when doing printing:

    Imports System.Drawing.Printing

    Here's a slightly modified (by me) printing example taken from the SDK's help. Add a Button to a form and from the Dialog Controls add an: OpenFileDialog control. In the code section just select everything ( Public Class Form1 and End Class) and Delete them. Then, Copy and Paste the code below back into the code section. Run it and Click on the button and the OpenFileDialog will popup and you can navigate to any Text file and once selected, it will print it. And it works with multiple pages too.

    This is but one of the many examples that are available on printing from the Help after installing the Framework 2.0 SDK. And there will be more examples from the Online connection too. If you are setup for that.

    james

    aka:Trucker

    Imports System.IO

    Imports System.Drawing.Printing

    Public Class Form1

    Private printFont As Font

    Private streamToPrint As StreamReader

    Dim pd As New PrintDocument

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    OpenFileDialog1.FileName = ""

    OpenFileDialog1.DefaultExt = "txt files (*.txt)|*.txt"

    OpenFileDialog1.Filter = "txt files (*.txt)|*.txt"

    Dim rslt As System.Windows.Forms.DialogResult = OpenFileDialog1.ShowDialog()

    If rslt = System.Windows.Forms.DialogResult.Cancel Then

    Return

    End If

    Try

    streamToPrint = New StreamReader(OpenFileDialog1.FileName)

    Try

    printFont = New Font("Arial", 10)

    Dim pd As New PrintDocument()

    AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage

    pd.Print()

    Finally

    streamToPrint.Close()

    End Try

    Catch ex As Exception

    MessageBox.Show(ex.Message)

    End Try

    End Sub

    ' The PrintPage event is raised for each page to be printed.

    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)

    Dim linesPerPage As Single = 0

    Dim yPos As Single = 0

    Dim count As Integer = 0

    Dim leftMargin As Single = ev.MarginBounds.Left

    Dim topMargin As Single = ev.MarginBounds.Top

    Dim line As String = Nothing

    ' Calculate the number of lines per page.

    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

    ' Print each line of the file.

    While count < linesPerPage

    line = streamToPrint.ReadLine()

    If line Is Nothing Then

    Exit While

    End If

    yPos = topMargin + count * printFont.GetHeight(ev.Graphics)

    ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

    count += 1

    End While

    ' If more lines exist, print another page.

    If Not (line Is Nothing) Then

    ev.HasMorePages = True

    Else

    ev.HasMorePages = False

    End If

    End Sub

    End Class


  • Printing from VB Express