Search code examples
vbavb6clipboardmsflexgrid

vb6 and msflexgrid, how to count columns and rows in clipboard text


Back to vb6 and msflexgrid, there is a weakness in pasting text to this control. If the user wanted for example to paste a 2*3 array in msflexgrid he should select 2 rows and 3 columns to paste the data, otherwise only one cell will fill in the msflexgrid (insted of 6 cells). i found if i colud split clipboard and count its rows and columns this problem should solve (select msflexgrid cells based on the array size) . I created the "editpaste"SUB:

Private Sub EditPaste()
Dim ARRAYLINES As Variant ' array with the lines
Dim ARRAYCELLS As Variant ' array with the cells of 1 line to count the cols needed
Dim ARRAYLINESidx As Integer

   '§ put clipboard in a  textbox named "cliper"
   With cliper
      .Text = Clipboard.GetText
      If .Text = "" Then
         Exit Sub
      Else
         ARRAYLINES = Split(.Text, Chr(13)) 'i also used the Chr(10) vbnewline  andvbCRLF to count the lines
      End If
   End With
   '§ put textbox in grid
   If ARRAYLINES(0) = "" Then

      Exit Sub
   Else
      ARRAYCELLS = Split(ARRAYLINES(0), vbTab) 'to count the columns
  msgbox UBound(ARRAYLINES) & UBound(ARRAYCELLS)
   End If
   '§ clear array
   ReDim ARRAYLINES(0)
   ReDim ARRAYCELLS(0)

End Sub

But my problem is that i have two types of text arrays (text matrixs). the array that came from msflixgrid to clipboard and the array that came from excell to clipboard and i can not make differencess between them in that sub. bellow is a screenshot from them into MSword:

enter image description here

The arrows n that picture are the TAb Characters i have no problem in counting them and the results are equal for all text arrays. but the paragraph signs are tricky and i knew in the second array they are "vbnewline" but in first array my code can not find them and suppose like i have only one line. Do you know a better way to get equal result in counting these columns and rows?


Solution

  • I used the following code to view the clipboard data :

    '1 form with
    '  1 msflexgrid control
    '  1 textbox control
    '  2 command buttons
    
    Option Explicit
    
    Private Sub Command1_Click()
      Dim strText As String
      strText = Clipboard.GetText
      ShowAscii strText
    End Sub
    
    Private Sub Command2_Click()
      Clipboard.SetText MSFlexGrid1.Clip
    End Sub
    
    Private Sub Form_Load()
      Dim intRow As Integer, intCol As Integer
      With MSFlexGrid1
        For intRow = 0 To .Rows - 1
          For intCol = 0 To .Cols - 1
            .TextMatrix(intRow, intCol) = CStr(100 * intRow + intCol)
          Next intCol
        Next intRow
      End With 'MSFlexGrid1
    End Sub
    
    Private Sub ShowAscii(strText As String)
      Dim intChar As Integer
      Dim strShow As String
      strShow = ""
      For intChar = 1 To Len(strText)
        strShow = strShow & Right$("00" & Hex$(Asc(Mid$(strText, intChar, 1))), 2) & " "
      Next intChar
      Text1.Text = strShow
    End Sub
    

    When I select the cells with 200, 201, 300, 301 in it and click on command2 and then on command1 then the textbox shows :

    32 30 30 09 32 30 31 0D 33 30 30 09 33 30 31 
    

    When I put the same data in excel and copy it, and then click command1 then the textbox shows :

    32 30 30 09 32 30 31 0D 0A 33 30 30 09 33 30 31 0D 0A 
    

    The difference between those 2 are that excel used vbCrLF to separate the rows, while the MSFlexGrid only used vbCr

    I think you should be ok when you remove all vbLF from the clipboard data before processing it :

    strText = Replace(strText, vbLf, "")
    

    After that both input methods only use vbCR as row separators