Results 1 to 40 of 40

Thread: VB6 - Flexgrid to Excel / Excel to Flexgrid. Fast copy using the Clipboard.

Threaded View

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    VB6 - Flexgrid to Excel / Excel to Flexgrid. Fast copy using the Clipboard.

    The advantage of this methods is that they work pretty fast, compared to send/bring data cell by cell.

    Flexgrid to Excel Example:
    VB Code:
    1. Private Sub FlexToExcel()
    2. Dim xlObject    As Excel.Application
    3. Dim xlWB        As Excel.Workbook
    4.        
    5.     Set xlObject = New Excel.Application
    6.  
    7.     'This Adds a new woorkbook, you could open the workbook from file also
    8.     Set xlWB = xlObject.Workbooks.Add
    9.                
    10.     Clipboard.Clear 'Clear the Clipboard
    11.     With MSFlexGrid1
    12.         'Select Full Contents (You could also select partial content)
    13.         .Col = 0               'From first column
    14.         .Row = 0               'From first Row (header)
    15.         .ColSel = .Cols - 1    'Select all columns
    16.         .RowSel = .Rows - 1    'Select all rows
    17.         Clipboard.SetText .Clip 'Send to Clipboard
    18.     End With
    19.            
    20.     With xlObject.ActiveWorkbook.ActiveSheet
    21.         .Range("A1").Select 'Select Cell A1 (will paste from here, to different cells)
    22.         .Paste              'Paste clipboard contents
    23.     End With
    24.    
    25.     ' This makes Excel visible
    26.     xlObject.Visible = True
    27. End Sub
    Excel To Flexgrid Example:
    VB Code:
    1. Private Sub ExcelToFlexgrid()
    2. Dim xlObject    As Excel.Application
    3. Dim xlWB        As Excel.Workbook
    4.        
    5.     Set xlObject = New Excel.Application
    6.     Set xlWB = xlObject.Workbooks.Open("C:\Book1.xls") 'Open your book here
    7.                
    8.     Clipboard.Clear
    9.     With xlObject.ActiveWorkbook.ActiveSheet
    10.         .Range("A1:F7").Copy 'Set selection to Copy
    11.     End With
    12.        
    13.     With MSFlexGrid1
    14.         .Redraw = False     'Dont draw until the end, so we avoid that flash
    15.         .Row = 0            'Paste from first cell
    16.         .Col = 0
    17.         .RowSel = .Rows - 1 'Select maximum allowed (your selection shouldnt be greater than this)
    18.         .ColSel = .Cols - 1
    19.         .Clip = Replace(Clipboard.GetText, vbNewLine, vbCr) 'Replace carriage return with the correct one
    20.         .Col = 1            'Just to remove that blue selection from Flexgrid
    21.         .Redraw = True      'Now draw
    22.     End With
    23.        
    24.     xlObject.DisplayAlerts = False 'To avoid "Save woorkbook" messagebox
    25.    
    26.     'Close Excel
    27.     xlWB.Close
    28.     xlObject.Application.Quit
    29.     Set xlWB = Nothing
    30.     Set xlObject = Nothing
    31. End Sub
    This has been tested Using Windows XP-SP2, VB6-SP6, and Office XP (2002).
    This code might need minor modifications when using a different version of Office.
    Last edited by jcis; Apr 3rd, 2006 at 06:54 AM.

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