Tuesday, July 3, 2012

Save VsFlexGrid data to excel


  1. Private Sub FlexToExcel()
  2. Dim xlObject    As Excel.Application
  3. Dim xlWB        As Excel.Workbook
  4.        
  5.     Set xlObject = New Excel.Application
  6.     'This Adds a new woorkbook, you could open the workbook from file also
  7.     Set xlWB = xlObject.Workbooks.Add
  8.                
  9.     Clipboard.Clear 'Clear the Clipboard
  10.     With MSFlexGrid1
  11.         'Select Full Contents (You could also select partial content)
  12.         .Col = 0               'From first column
  13.         .Row = 0               'From first Row (header)
  14.         .ColSel = .Cols - 1    'Select all columns
  15.         .RowSel = .Rows - 1    'Select all rows
  16.         Clipboard.SetText .Clip 'Send to Clipboard
  17.     End With
  18.            
  19.     With xlObject.ActiveWorkbook.ActiveSheet
  20.         .Range("A1").Select 'Select Cell A1 (will paste from here, to different cells)
  21.         .Paste              'Paste clipboard contents
  22.     End With
  23.    
  24.     ' This makes Excel visible
  25.     xlObject.Visible = True
  26. End Sub

No comments:

Post a Comment