Solved

Select all and copy from data grid

Posted on 2006-11-21
3
187 Views
Last Modified: 2010-04-30
hello,

i have a data grid on a form that is populated from and sql query.

i have a number of users that would like to be able to copy the entire grid and paste it into excel is this possible as i cannot seem to select all.

also it might be nice if i could print just the grid striaght to the printer in landscape form

i am using VB6

thanks
0
Comment
Question by:Marcusw
  • 3
3 Comments
 
LVL 1

Accepted Solution

by:
RupaliV earned 250 total points
ID: 17993210
1. First clear the grid, and set the no of rows and no of cols.

Grid.Clear
Grid.Rows = No of rows
Grid.Cols = No of Cols

2. Add the data to the grid
Grid.TextMatrix(0, 0) = "One"
Grid.TextMatrix(0, 1) = "1"
Grid.TextMatrix(1, 0) = "Two"
Grid.TextMatrix(1, 1) = "2"

3. click on the button to Export the data to Excel
Private Sub Command1_Click()
    cdlgCommonDialog.CancelError = True
    cdlgCommonDialog.Flags = cdlOFNOverwritePrompt   'ask wheather to overwite if file already exists
    cdlgCommonDialog.DefaultExt = "xls"
    cdlgCommonDialog.Filter = "Text Files (*.xls)|*.xls"

    cdlgCommonDialog.ShowSave
   
    If Mid$(cdlgCommonDialog.FileName, Len(cdlgCommonDialog.FileName) - 3, 4) = ".xls" Then
        ExportDataToExcel (cdlgCommonDialog.FileName)
    Else
        ExportDataToExcel (cdlgCommonDialog.FileName & ".xls")
    End If
End Sub

Public Sub ExportDataToExcel(sFilename As String)
   
    Dim FileNum As Long
    Dim GridOfData As String
   
    With Grid
        .Row = 0
        .Col = 0
        .RowSel = .Rows - 1
        .ColSel = .Cols - 1
        GridOfData = .Clip
       
        ' Save the data to a file
        FileNum = FreeFile
        Open sFilename For Output As #FileNum
        Print #FileNum, GridOfData
        Close #FileNum
    End With
 
End Sub


4. To print the data
Private Function PrintData()
   
    Dim objApp As Object
    Dim objBook As Object
    Dim objSheet As Object
    Dim inti As Integer
    Dim intj As Integer
       
    Set objApp = CreateObject("Excel.Application") 'setting excel
    Set objBook = objApp.Workbooks.Add
    Set objSheet = objBook.Worksheets(1)
   
    objApp.Cells.Font.Name = "Courier New"
    objApp.Cells.Font.Size = 5
    objApp.Cells.Font.Bold = False
    objApp.Cells.Borders.Value = 0
    objApp.Cells.WrapText = True
   
    With objSheet
        .Range((.Cells(1, 1)), (.Cells(1, 13))).HorizontalAlignment = -4131
       
        inti = 0
        Do Until inti = Grid.Rows  'export until end of rows
            intj = 0
            Do Until intj = Grid.Cols  'get data until end of cols
                .Cells(inti + 1, intj + 1) = Trim$(Grid.TextMatrix(inti, intj))
                intj = intj + 1
            Loop
            inti = inti + 1
        Loop
    End With
   
    With objSheet.PageSetup
        .LeftMargin = objApp.InchesToPoints(0.25)
        .CenterHorizontally = False
        .CenterVertically = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Orientation = 2    'xlLandscape
        .PrintGridlines = True
      ' A- is the column no from where it starts printing. B-is the col no where it should stop printing;
      '1- Row no where it should start printing and Grid.Rows      is the row no where it should stop printing.
        .PrintArea = "$A$1:$B$" & Grid.Rows      
       
    End With
    objSheet.PrintOut
   
    Set objSheet = Nothing 'set to nothing and quit
    objBook.Close False
    Set objBook = Nothing
    objApp.Quit
    Set objApp = Nothing
   
    MsgBox "File Printed Successfully.", vbInformation, "File Printed..."

End Function
0
 
LVL 1

Expert Comment

by:RupaliV
ID: 18017702
Hi,

   Is the given sample code useful? Please let me know if its working.
0
 
LVL 1

Expert Comment

by:RupaliV
ID: 18290145
Hi,

     Please tell is the given sample code useful? Is it working?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question