connecting to printer port using hp printer commands in vb code??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suvarna
    New Member
    • Aug 2007
    • 24

    connecting to printer port using hp printer commands in vb code??

    hi
    i am having problem in firing commands to the hp printer through vb code. i used hp printer commands in my code but when the commands are fired it gives error "failed to print the doccument". printer shows no movements.
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by suvarna
    hi
    i am having problem in firing commands to the hp printer through vb code. i used hp printer commands in my code but when the commands are fired it gives error "failed to print the doccument". printer shows no movements.
    Whatever printer you have to use LPT1 or any Printer Port.
    Will you post what you had been tryed?

    Comment

    • suvarna
      New Member
      • Aug 2007
      • 24

      #3
      Originally posted by hariharanmca
      Whatever printer you have to use LPT1 or any Printer Port.
      Will you post what you had been tryed?

      the code i used is

      'selects the printer model from the dropdown

      Private Sub cmbPrinters_Cli ck()

      mvarPrinterName = cmbPrinters.Tex t
      InitialisePrint er
      ExitPacketMode

      End Sub

      Private Sub Command1_Click( )

      MoveHorizontal
      'MoveVertical
      End Sub

      Private Sub Form_Load()

      UpdateCmbPrinte rs

      End Sub



      Private Sub UpdateCmbPrinte rs()
      Dim i As Integer

      i = 0
      For Each X In Printers
      cmbPrinters.Lis t(i) = X.DeviceName
      i = i + 1
      Next
      cmbPrinters.Lis tIndex = 0

      End Sub

      'module2 code

      Option Explicit

      Public iStartHour As Integer
      Public buffer As String
      Public textFile As String
      Public graphFile As String


      Public Type TCharacterStrin g
      CharacterID As Integer
      arrayCharacterM ap(rowCount, colCount) As Boolean
      End Type

      Public Type TParaProp
      mCharacterStrin g() As TCharacterStrin g
      End Type

      Public mParaProp(5) As TParaProp


      Public iGraphBufferCou nt As Integer
      Public ArrayGraphBuffe r(GraphBufferCo unt) As String


      Public gFso As New FileSystemObjec t, gFil As File, gTs As TextStream
      Public LineCount As Long 'local copy

      Public iTextBuffer(89) As Integer
      Public iCmdBuffer() As Integer
      Public iGraphBuffer() As Long
      Public lGraphData As Long

      Public mvarPrinterID As Integer
      Public mvarPrinterName As String


      Private Sub OpenTextFile()

      Set fso = CreateObject("S cripting.FileSy stemObject")
      If fso.FileExists( textFile) = True Then
      fso.DeleteFile (textFile)
      fso.CreateTextF ile (textFile)
      Else
      fso.CreateTextF ile (textFile)
      End If
      Set fil = fso.GetFile(tex tFile)
      Set ts = fil.OpenAsTextS tream(ForWritin g)

      End Sub

      Private Sub CloseTextFile()

      ts.Close
      Set ts = Nothing
      Set fil = Nothing
      Set fso = Nothing

      End Sub

      Public Sub InitialisePrint er()
      Dim i As Integer
      Dim t As Integer

      iStartHour = 0
      LineCount = 0

      lGraphData = 2800
      ReDim iGraphBuffer(lG raphData)
      'ReDim iCmdBuffer(rsCm dMaster.RecordC ount + lGraphData)




      textFile = "C:\f" & mvarPrinterName & "." & "txt"
      graphFile = "C:\g" & mvarPrinterName & "." & "txt"

      iGraphBufferCou nt = 0


      End Sub

      Public Sub ExitPacketMode( )

      OpenTextFile
      buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
      ts.Write buffer
      CloseTextFile
      Test (textFile)

      OpenTextFile
      buffer = Chr(27) + "@"
      ts.Write buffer
      CloseTextFile
      Test (textFile)

      End Sub

      Public Sub TerminatePrinte r()

      End Sub
      Public Sub ResetPrinter()

      OpenTextFile
      buffer = Chr(27) + "E"
      ts.Write buffer
      CloseTextFile
      Test (textFile)

      End Sub
      Public Sub MoveVertical()
      Dim i As Integer

      OpenTextFile
      buffer = Chr(27) + "J" + Chr(4)
      ts.Write buffer
      CloseTextFile
      Test (textFile)

      End Sub





      Public Sub MoveHorizontal( )
      Dim i As Integer

      OpenTextFile
      'ascii value for paper source
      buffer = Chr(27) + Chr(38) + Chr(108) + Chr(48) + Chr(72)
      ts.Write buffer
      CloseTextFile
      Test (textFile)

      End Sub

      Public Sub Home()
      Dim i As Integer

      OpenTextFile
      buffer = Chr(13)
      ts.Write buffer
      CloseTextFile
      Test (textFile)

      End Sub

      Private Sub outputBuffer()
      Dim i As Integer

      Set fso = CreateObject("S cripting.FileSy stemObject")
      If fso.FileExists( textFile) = True Then
      fso.DeleteFile (textFile)
      fso.CreateTextF ile (textFile)
      Else
      fso.CreateTextF ile (textFile)
      End If
      Set fil = fso.GetFile(tex tFile)
      Set ts = fil.OpenAsTextS tream(ForWritin g)

      buffer = Chr(iTextBuffer (0))
      For i = 1 To 79
      buffer = buffer + Chr(iTextBuffer (i))
      Next i
      ts.Write buffer

      ts.Close
      Set ts = Nothing
      Set fil = Nothing
      Set fso = Nothing

      End Sub


      Private Sub Test(ByVal FileName As String)
      Dim i As Integer

      i = vbSendPrnToPrin ter(FileName, mvarPrinterName )

      End Sub

      Private Sub FillBufferBlank ()
      Dim i As Integer

      For i = 0 To 89
      iTextBuffer(i) = 32
      Next i

      End Sub

      Private Sub FillGraphBuffer Blank()
      Dim i As Integer

      For i = 0 To 2000
      iGraphBuffer(i) = 0
      Next i

      End Sub


      Private Sub outputGraphBuff er()
      Dim i As Integer
      Dim GraphBuffer As String

      If iGraphBufferCou nt = (GraphBufferCou nt - 1) Then

      Set gFso = CreateObject("S cripting.FileSy stemObject")
      If gFso.FileExists (graphFile) = True Then
      gFso.DeleteFile (graphFile)
      gFso.CreateText File (graphFile)
      Else
      gFso.CreateText File (graphFile)
      End If
      Set gFil = gFso.GetFile(gr aphFile)
      Set gTs = gFil.OpenAsText Stream(ForWriti ng)

      GraphBuffer = ArrayGraphBuffe r(0)
      For i = 1 To GraphBufferCoun t - 1
      GraphBuffer = GraphBuffer & ArrayGraphBuffe r(i)
      Next i

      gTs.Write GraphBuffer
      gTs.Close
      Set gTs = Nothing
      Set gFil = Nothing
      Set gFso = Nothing
      iGraphBufferCou nt = 0
      Test (graphFile)
      Else
      ArrayGraphBuffe r(iGraphBufferC ount) = Chr(iCmdBuffer( 0))
      For i = 1 To rsCmdMaster.Rec ordCount + lGraphData - 1
      ArrayGraphBuffe r(iGraphBufferC ount) = ArrayGraphBuffe r(iGraphBufferC ount) + Chr(iCmdBuffer( i))
      Next i
      iGraphBufferCou nt = iGraphBufferCou nt + 1
      End If
      End Sub


      'module1 code

      Public ColumnWidth As Long
      Public Const PageWidth = 360
      Public Const rowCount = 7
      Public Const colCount = 4
      Public Const GraphBufferCoun t = 60
      Public Const StartPos = 5

      'Return codes
      Public Const ERR_NOERROR = 0 'No error
      Public Const ERR_DEFPRINTER = 1 'Error obtaining default printer
      Public Const ERR_OPENFILE = 2 'Error opening file
      Public Const ERR_OPENPRINTER = 3 'Error opening printer
      Public Const ERR_PRINT = 4 'Error sending date to printer
      Public Const ERR_FILE = 5 'Error reading file

      'Function declarations
      Public Declare Function vbSendPrnToPrin ter Lib "BINPRN32.D LL" (ByVal pFilename As String, ByVal pPrinterName As String) As Integer
      Public Declare Function vbGetDefPrinter Name Lib "BINPRN32.D LL" (ByVal pPrinterName As String) As Integer
      Public Declare Function vbSendPrnToDefP rinter Lib "BINPRN32.D LL" (ByVal pFilename As String) As Integer
      Public fso As New FileSystemObjec t, fil As File, ts As TextStream

      Public Function CNull(Variable As Variant, Value As Variant)
      On Error Resume Next
      If IsNull(Variable ) Then
      CNull = Value
      Else
      If IsNull(Variable ) = False Then
      CNull = Variable
      Else
      CNull = Value
      End If
      End If
      End Function



      'different values of ascii are not accepted too.

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by suvarna
        the code i used is

        'selects the printer model from the dropdown

        Private Sub cmbPrinters_Cli ck()

        mvarPrinterName = cmbPrinters.Tex t
        InitialisePrint er
        ExitPacketMode

        End Sub

        Private Sub Command1_Click( )

        MoveHorizontal
        'MoveVertical
        End Sub

        Private Sub Form_Load()

        UpdateCmbPrinte rs

        End Sub



        Private Sub UpdateCmbPrinte rs()
        Dim i As Integer

        i = 0
        For Each X In Printers
        cmbPrinters.Lis t(i) = X.DeviceName
        i = i + 1
        Next
        cmbPrinters.Lis tIndex = 0

        End Sub

        'module2 code

        Option Explicit

        Public iStartHour As Integer
        Public buffer As String
        Public textFile As String
        Public graphFile As String


        Public Type TCharacterStrin g
        CharacterID As Integer
        arrayCharacterM ap(rowCount, colCount) As Boolean
        End Type

        Public Type TParaProp
        mCharacterStrin g() As TCharacterStrin g
        End Type

        Public mParaProp(5) As TParaProp


        Public iGraphBufferCou nt As Integer
        Public ArrayGraphBuffe r(GraphBufferCo unt) As String


        Public gFso As New FileSystemObjec t, gFil As File, gTs As TextStream
        Public LineCount As Long 'local copy

        Public iTextBuffer(89) As Integer
        Public iCmdBuffer() As Integer
        Public iGraphBuffer() As Long
        Public lGraphData As Long

        Public mvarPrinterID As Integer
        Public mvarPrinterName As String


        Private Sub OpenTextFile()

        Set fso = CreateObject("S cripting.FileSy stemObject")
        If fso.FileExists( textFile) = True Then
        fso.DeleteFile (textFile)
        fso.CreateTextF ile (textFile)
        Else
        fso.CreateTextF ile (textFile)
        End If
        Set fil = fso.GetFile(tex tFile)
        Set ts = fil.OpenAsTextS tream(ForWritin g)

        End Sub

        Private Sub CloseTextFile()

        ts.Close
        Set ts = Nothing
        Set fil = Nothing
        Set fso = Nothing

        End Sub

        Public Sub InitialisePrint er()
        Dim i As Integer
        Dim t As Integer

        iStartHour = 0
        LineCount = 0

        lGraphData = 2800
        ReDim iGraphBuffer(lG raphData)
        'ReDim iCmdBuffer(rsCm dMaster.RecordC ount + lGraphData)




        textFile = "C:\f" & mvarPrinterName & "." & "txt"
        graphFile = "C:\g" & mvarPrinterName & "." & "txt"

        iGraphBufferCou nt = 0


        End Sub

        Public Sub ExitPacketMode( )

        OpenTextFile
        buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
        ts.Write buffer
        CloseTextFile
        Test (textFile)

        OpenTextFile
        buffer = Chr(27) + "@"
        ts.Write buffer
        CloseTextFile
        Test (textFile)

        End Sub

        Public Sub TerminatePrinte r()

        End Sub
        Public Sub ResetPrinter()

        OpenTextFile
        buffer = Chr(27) + "E"
        ts.Write buffer
        CloseTextFile
        Test (textFile)

        End Sub
        Public Sub MoveVertical()
        Dim i As Integer

        OpenTextFile
        buffer = Chr(27) + "J" + Chr(4)
        ts.Write buffer
        CloseTextFile
        Test (textFile)

        End Sub





        Public Sub MoveHorizontal( )
        Dim i As Integer

        OpenTextFile
        'ascii value for paper source
        buffer = Chr(27) + Chr(38) + Chr(108) + Chr(48) + Chr(72)
        ts.Write buffer
        CloseTextFile
        Test (textFile)

        End Sub

        Public Sub Home()
        Dim i As Integer

        OpenTextFile
        buffer = Chr(13)
        ts.Write buffer
        CloseTextFile
        Test (textFile)

        End Sub

        Private Sub outputBuffer()
        Dim i As Integer

        Set fso = CreateObject("S cripting.FileSy stemObject")
        If fso.FileExists( textFile) = True Then
        fso.DeleteFile (textFile)
        fso.CreateTextF ile (textFile)
        Else
        fso.CreateTextF ile (textFile)
        End If
        Set fil = fso.GetFile(tex tFile)
        Set ts = fil.OpenAsTextS tream(ForWritin g)

        buffer = Chr(iTextBuffer (0))
        For i = 1 To 79
        buffer = buffer + Chr(iTextBuffer (i))
        Next i
        ts.Write buffer

        ts.Close
        Set ts = Nothing
        Set fil = Nothing
        Set fso = Nothing

        End Sub


        Private Sub Test(ByVal FileName As String)
        Dim i As Integer

        i = vbSendPrnToPrin ter(FileName, mvarPrinterName )
        End Sub

        Private Sub FillBufferBlank ()
        Dim i As Integer

        For i = 0 To 89
        iTextBuffer(i) = 32
        Next i

        End Sub

        Private Sub FillGraphBuffer Blank()
        Dim i As Integer

        For i = 0 To 2000
        iGraphBuffer(i) = 0
        Next i

        End Sub


        Private Sub outputGraphBuff er()
        Dim i As Integer
        Dim GraphBuffer As String

        If iGraphBufferCou nt = (GraphBufferCou nt - 1) Then

        Set gFso = CreateObject("S cripting.FileSy stemObject")
        If gFso.FileExists (graphFile) = True Then
        gFso.DeleteFile (graphFile)
        gFso.CreateText File (graphFile)
        Else
        gFso.CreateText File (graphFile)
        End If
        Set gFil = gFso.GetFile(gr aphFile)
        Set gTs = gFil.OpenAsText Stream(ForWriti ng)

        GraphBuffer = ArrayGraphBuffe r(0)
        For i = 1 To GraphBufferCoun t - 1
        GraphBuffer = GraphBuffer & ArrayGraphBuffe r(i)
        Next i

        gTs.Write GraphBuffer
        gTs.Close
        Set gTs = Nothing
        Set gFil = Nothing
        Set gFso = Nothing
        iGraphBufferCou nt = 0
        Test (graphFile)
        Else
        ArrayGraphBuffe r(iGraphBufferC ount) = Chr(iCmdBuffer( 0))
        For i = 1 To rsCmdMaster.Rec ordCount + lGraphData - 1
        ArrayGraphBuffe r(iGraphBufferC ount) = ArrayGraphBuffe r(iGraphBufferC ount) + Chr(iCmdBuffer( i))
        Next i
        iGraphBufferCou nt = iGraphBufferCou nt + 1
        End If
        End Sub


        'module1 code

        Public ColumnWidth As Long
        Public Const PageWidth = 360
        Public Const rowCount = 7
        Public Const colCount = 4
        Public Const GraphBufferCoun t = 60
        Public Const StartPos = 5

        'Return codes
        Public Const ERR_NOERROR = 0 'No error
        Public Const ERR_DEFPRINTER = 1 'Error obtaining default printer
        Public Const ERR_OPENFILE = 2 'Error opening file
        Public Const ERR_OPENPRINTER = 3 'Error opening printer
        Public Const ERR_PRINT = 4 'Error sending date to printer
        Public Const ERR_FILE = 5 'Error reading file

        'Function declarations
        Public Declare Function vbSendPrnToPrin ter Lib "BINPRN32.D LL" (ByVal pFilename As String, ByVal pPrinterName As String) As Integer
        Public Declare Function vbGetDefPrinter Name Lib "BINPRN32.D LL" (ByVal pPrinterName As String) As Integer
        Public Declare Function vbSendPrnToDefP rinter Lib "BINPRN32.D LL" (ByVal pFilename As String) As Integer
        Public fso As New FileSystemObjec t, fil As File, ts As TextStream

        Public Function CNull(Variable As Variant, Value As Variant)
        On Error Resume Next
        If IsNull(Variable ) Then
        CNull = Value
        Else
        If IsNull(Variable ) = False Then
        CNull = Variable
        Else
        CNull = Value
        End If
        End If
        End Function



        'different values of ascii are not accepted too.

        One thing, For network printer you cannot give just printer name.

        you have to give like

        \\<System Name>\<Printer Name>

        Comment

        • suvarna
          New Member
          • Aug 2007
          • 24

          #5
          Originally posted by hariharanmca
          One thing, For network printer you cannot give just printer name.

          you have to give like

          \\<System Name>\<Printer Name>


          the printer is not a network printer.. it is connected to this system for testing and will remain for implementation also.

          Comment

          • hariharanmca
            Top Contributor
            • Dec 2006
            • 1977

            #6
            Originally posted by suvarna
            Public Sub InitialisePrint er()Dim i As Integer
            Dim t As Integer

            iStartHour = 0
            LineCount = 0

            lGraphData = 2800
            ReDim iGraphBuffer(lG raphData)

            'ReDim iCmdBuffer(rsCm dMaster.RecordC ount + lGraphData)




            textFile = "C:\f" & mvarPrinterName & "." & "txt"
            graphFile = "C:\g" & mvarPrinterName & "." & "txt"

            iGraphBufferCou nt = 0


            End Sub

            Can you explain what this method is doing?

            Comment

            • suvarna
              New Member
              • Aug 2007
              • 24

              #7
              Originally posted by hariharanmca
              Can you explain what this method is doing?

              this method is used to print a graph which takes data from gas sacnner n calculates a graph for it..

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Originally posted by suvarna
                this method is used to print a graph which takes data from gas sacnner n calculates a graph for it..
                Okay, Can you post which part of code is showing error? and what error?
                It is not easy to go through your bulky code.

                [CODE=vb]Public Sub ExitPacketMode( )

                OpenTextFile
                buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
                ts.Write buffer
                CloseTextFile
                Test (textFile)

                OpenTextFile
                buffer = Chr(27) + "@"
                ts.Write buffer
                CloseTextFile
                Test (textFile)

                End Sub[/CODE]

                1. where is declaration of ts in ts.Write buffer
                2. Is your Print output an Billing or some thing related to data report (Because your pasing .txt file to print).
                2. above code seems to be there is no any image or graphical line to print.
                3. If you do not have any image or graphical line there is some other way to print.
                4. Just use a .bat file to print.

                Comment

                • suvarna
                  New Member
                  • Aug 2007
                  • 24

                  #9
                  Originally posted by hariharanmca
                  Okay, Can you post which part of code is showing error? and what error?
                  It is not easy to go through your bulky code.

                  [CODE=vb]Public Sub ExitPacketMode( )

                  OpenTextFile
                  buffer = Chr(0) + Chr(0) + Chr(0) + Chr(27) + Chr(1) + "@EJL" + Chr(32) + "1284.4" + Chr(10) + "@EJL" + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(10)
                  ts.Write buffer
                  CloseTextFile
                  Test (textFile)

                  OpenTextFile
                  buffer = Chr(27) + "@"
                  ts.Write buffer
                  CloseTextFile
                  Test (textFile)

                  End Sub[/CODE]

                  1. where is declaration of ts in ts.Write buffer
                  2. Is your Print output an Billing or some thing related to data report (Because your pasing .txt file to print).
                  2. above code seems to be there is no any image or graphical line to print.
                  3. If you do not have any image or graphical line there is some other way to print.
                  4. Just use a .bat file to print.


                  [code= vb]Public fso As New FileSystemObjec t, fil As File, ts As TextStream

                  end sub[/code]

                  i have mentioned the module code also for any doubts u can go trough.
                  how do i print graphics through this code??

                  Comment

                  • hariharanmca
                    Top Contributor
                    • Dec 2006
                    • 1977

                    #10
                    Originally posted by suvarna
                    [code= vb]Public fso As New FileSystemObjec t, fil As File, ts As TextStream

                    end sub[/code]

                    i have mentioned the module code also for any doubts u can go trough.
                    how do i print graphics through this code??
                    I cannot find error in such a huge code and i do not want that module code (Unable to find starting point and ending point).
                    Okay, Can you post which part of code is showing error? and what error?
                    It is not easy to go through your bulky code.

                    Comment

                    • suvarna
                      New Member
                      • Aug 2007
                      • 24

                      #11
                      Originally posted by hariharanmca
                      I cannot find error in such a huge code and i do not want that module code (Unable to find starting point and ending point).
                      Okay, Can you post which part of code is showing error? and what error?
                      It is not easy to go through your bulky code.

                      neway thanks i found the solution for it.. it worked the same way i just included it to another class defn. thanks a lot... actually the code was not written by me it was my senior so had problems..

                      Comment

                      Working...