Ask kind help: VB6 How to read the string line by line from the MS_Office Clipboard
Thanks in advance,

Dani AI

Generated

Short answer: normalize every possible line-break into one delimiter, then Split. That fixes the mixed CR / LF / CRLF cases that commonly trip VB6 code copied from Office. Good points from (use the clipboard object) and (look for line breaks) — combine those ideas and handle Unicode/odd separators and a couple of common VB6 gotchas.

Example (requires a reference to "Microsoft Forms 2.0 Object Library"):

' Returns a zero-based string array of lines (returns empty array on error)
Function GetClipboardLines() As Variant
  Dim dobj As MSForms.DataObject
  Dim raw As String
  Dim arr() As String

  On Error GoTo ErrHandler
  Set dobj = New MSForms.DataObject
  dobj.GetFromClipboard
  raw = dobj.GetText

  ' Normalize all common line breaks to LF, then split on LF
  raw = Replace(raw, vbCrLf, vbLf)
  raw = Replace(raw, vbCr, vbLf)
  raw = Replace(raw, vbLf & vbCr, vbLf)
  raw = Replace(raw, ChrW(&H2028), vbLf)   ' unicode line separator
  raw = Replace(raw, ChrW(&H2029), vbLf)   ' unicode paragraph separator

  If Len(raw) = 0 Then
    GetClipboardLines = Array()
    Exit Function
  End If

  If Right$(raw, 1) = vbLf Then raw = Left$(raw, Len(raw) - 1)
  arr = Split(raw, vbLf)
  GetClipboardLines = arr
  Exit Function

ErrHandler:
  GetClipboardLines = Array()
End Function

Usage: call GetClipboardLines, iterate from LBound to UBound and populate your MultiLine textbox. Notes and troubleshooting:

  • The pasted code in post #5 shows comparisons like If iPos 0 Then — that is a paste/formatting error; use If iPos <> 0 Then.
  • If non‑Latin characters are garbled, the MSForms.DataObject path may lose Unicode; in that case use the Win32 clipboard API with CF_UNICODETEXT (OpenClipboard/GetClipboardData/GlobalLock + RtlMoveMemory) — look for a tested CF_UNICODETEXT routine rather than inventing one.
  • Test what you actually received: Debug.Print AscW(Mid$(raw, 1, 1)) or show Hex bytes to see which separators are present.
  • Ensure the destination TextBox has MultiLine = True and you handle empty/trailing lines as needed.

This approach handles the variety of separators Office can put on the clipboard and avoids relying on a single constant like vbNewLine.

Recommended Answers

All 4 Replies

Well i don't know how but you can use the clipboard object in vb6 to get and set clipboard content.

Jhai, Thanks a lot, I know vb6 can use the GetText to get clipboard content.
The question is how to read it line by line? (to a string array)

I wonder if you can check for the new line in the clipboard. If yes, you can check

if (looping thru the array) = vbNewLine then

' do whatever

else

'do something else

end if

I have used this when reading a string so you must be able to use it in anarray
strVariable = Replace(strVariable, vbNewLine, vbNullString)

' Sorry to advise you
' the vbNewline does not be recognized by vb6, I had tred following code always rturn False
' first retrieve contents to a variable string OneClpBrd_Content

Private Sub Form_Load()
 TextBox2.Text = ""       '' setup MultiLine=True
 TextBox2.Text = vbNullString        '' setup MultiLine=True
 iPos = InStr(1, OneClpBrd_content, Chr(10))
 If iPos  0 Then
     q = q
 End If
 iPos = InStr(1, OneClpBrd_content, Chr(11))
 If iPos  0 Then
     q = q
 End If
 iPos = InStr(1, OneClpBrd_content, Chr(13))
 If iPos  0 Then
     q = q
 End If
 iPos = InStr(1, OneClpBrd_content, vbCrLf)
 If iPos  0 Then
     q = q
 End If
 iPos = InStr(1, OneClpBrd_content, vbNewLine)
 If iPos  0 Then
     q = q
 End If

 m_dtasplit = Split(OneClpBrd_content, vbNewLine)  ''  , -1, vbTextCompare
 If UBound(m_dtasplit)  0 Then
     a = a
 End If
 m_dtasplit = Split(OneClpBrd_content, Chr(10))  ''  , -1, vbTextCompare
 If UBound(m_dtasplit)  0 Then
     a = a
 End If
  m_dtasplit = Split(OneClpBrd_content, Chr(11))  ''  , -1, vbTextCompare
 If UBound(m_dtasplit)  0 Then
     a = a
 End If
 m_dtasplit = Split(OneClpBrd_content, Chr(13))  ''  , -1, vbTextCompare
 If UBound(m_dtasplit)  0 Then
     a = a
 End If

 m_dtasplit = Split(OneClpBrd_content, vbCrLf)  ''  , -1, vbTextCompare
 If UBound(m_dtasplit)  0 Then
     TextBox2.Text = m_dtasplit(0)
 End If

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.