CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2006
    Posts
    147

    how to use printer to print a datagrid

    i have a problem about the database in vb. how can i use the printer in vb to print the table in my database.. can i use print the datagrid in my form??.

  2. #2
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,901

    Re: how to use printer to print a datagrid

    This question is asked frequently (Check the search function on this forum)

    The short answer is "You don't want to print the DataGrid"
    (The grid may be 1000 lines long - a grid is what you see on the screen.)


    Ie, to just print the grid, make the grid as big as you want on a form, then issue the command Form1.PrintForm - the form prints along with the grid
    (with all its controls) These you can made invisible of course. Multiple pages become a bit of a challenge.

    You don't actually want to print the DataGrid - you want to print all the lines inside the datagrid

    An option is to export the Grid lines to Excel - then the user prints out an excel sheet.

    If you users don't own excel, then you are forced to create a report format programatically

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to use printer to print a datagrid

    Although you can download the Excel Viewer, as can your clients. It makes it easy to view spreadsheets.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4

    Re: how to use printer to print a datagrid

    im useing this code to load to excel first is there a way to make it load excel without useing the path to it

    Shell """" & "C:\Program Files\Office10\excel.exe" & """" & " " & _
    """" & "C:\search.xls" & """"

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: how to use printer to print a datagrid

    Try the API. It wlll find the app for you!

    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMINIMIZE As Long = 2
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWNOACTIVATE As Long = 4
    Private Const SW_SHOW As Long = 5
    Private Const SW_MINIMIZE As Long = 6
    Private Const SW_SHOWMINNOACTIVED As Long = 7
    Private Const SW_SHOWNA As Long = 8
    Private Const SW_RESTORE As Long = 9
    Private Const SW_SHOWDEFAULT As Long = 10
    
    'SW_HIDE 0
    'Hides the window and activates another window.
    'SW_MAXIMIZE 3
    'Maximizes the specified window.
    'SW_MINIMIZE 6
    'Minimizes the specified window and activates
    'the next top-level window in the Z order.
    'SW_RESTORE 9
    'Activates and displays the window. If the window
    'is minimized or maximized, Windows restores it
    ' to its original size and position. An application
    'should specify this flag when restoring a minimized window.
    'SW_SHOW 5
    'Activates the window and displays it in its current size and position.
    'SW_SHOWDEFAULT 10
    'Sets the show state based on the SW_ flag
    ' specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
    'SW_SHOWMAXIMIZED 3
    'Activates the window and displays it as a maximized window.
    'SW_SHOWMINIMIZE 2
    'Activates the window and displays it as a minimized window.
    'SW_SHOWMINNOACTIVED 7
    'Displays the window as a minimized window. The active window remains active.
    'SW_SHOWNA 8
    'Displays the window in its current state. The active window remains active.
    'SW_SHOWNOACTIVATE 4
    'Displays a window in its most recent size
    'and position. The active window remains active.
    'SW_SHOWNORMAL 1
    'Activates and displays a window. If the window is minimized or
    'maximized, Windows restores it to its original size and position.
    'An application should specify this flag when displaying the window
    'for the first time
    
    
    Private Sub Command1_Click()
      ShellExecute 0&, "OPEN", "www.msn.com", vbNullString, "C:\", SW_SHOWMINIMIZE
    '    ShellExecute 0&, "OPEN", "D:\temp\pps-cd\play.bat", vbNullString, "C:\", SW_SHOWNORMAL
    '    ShellExecute 0&, "OPEN", "outlook.exe", vbNullString, "C:\", SW_SHOWNORMAL
    '    ShellExecute 0&, "PRINT", "D:\temp\jazz.txt", vbNullString, "D:\temp", SW_SHOWMINIMIZE
        Beep
     '  c:\program files\outlook express\msimn.exe
     '  c:\ = Destination Folder
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,901

    Re: how to use printer to print a datagrid

    OK ! Here it is !

    MSFLEXGRID REPORTING USING EXCEL

    You need to create a REFERENCE to Excel in the Project
    (You will need Excel on the Generating Machine - Excel or Excel Reader on the viewing machines)


    Code:
    Public Sub FlexGrid_To_Excel(TheFlexgrid As MSFlexGrid, _
      TheRows As Integer, TheCols As Integer, _
      Optional GridStyle As Integer = 1, Optional WorkSheetName _
      As String)
        
    Dim objXL As New Excel.Application
    Dim wbXL As New Excel.Workbook
    Dim wsXL As New Excel.Worksheet
    Dim intRow As Integer ' counter
    Dim intCol As Integer ' counter
    
    If Not IsObject(objXL) Then
        MsgBox "You need Microsoft Excel to use this function", _
           vbExclamation, "Print to Excel"
        Exit Sub
    End If
    
    'On Error Resume Next is necessary because
    'someone may pass more rows
    'or columns than the flexgrid has
    
    'you can instead check for this,
    'or rewrite the function so that
    'it exports all non-fixed cells
    'to Excel
    
    On Error Resume Next
    
    ' open Excel
    objXL.Visible = True
    Set wbXL = objXL.Workbooks.Add
    Set wsXL = objXL.ActiveSheet
    
    ' name the worksheet
    With wsXL
        If Not WorkSheetName = "" Then
            .Name = WorkSheetName
        End If
    End With
        
    ' fill worksheet
    For intRow = 1 To TheRows
        For intCol = 1 To TheCols
            With TheFlexgrid
                wsXL.Cells(intRow, intCol).Value = _
                   .TextMatrix(intRow - 1, intCol - 1) & " "
            End With
        Next
    Next
    
    ' format the look
    For intCol = 1 To TheCols
        wsXL.Columns(intCol).AutoFit
        'wsXL.Columns(intCol).AutoFormat (1)
        wsXL.Range("a1", Right(wsXL.Columns(TheCols).AddressLocal, _
             1) & TheRows).AutoFormat GridStyle
    Next
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured