PPL was telling us:
PPL nous racontait que :
I am trying to take a long string (that is split up with 2 tab
characters and contained on the clipboad) and place it
into 3 variables.
From
http://www.word.mvps.org/FAQs/MacrosVBA/ManipulateClipboard.htm
I have used the following code to place data from the clipboard into a
variable:
Dim MyData As DataObject
Dim strClip As String
Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText
How would I manipulate strClip to place its contents into 3
variables, as defined by the tab separation?
Something like this would work. There probably is a faster way..
'_______________________________________
Dim myString As String
Dim myVariant As Variant
Dim str1 As String
Dim str2 As String
Dim str3 As String
myString = "1111" & vbTab & "2222" & vbTab & "3333"
myVariant = Split(myString, vbTab)
str1 = myVariant(0)
str2 = myVariant(1)
str3 = myVariant(2)
'_______________________________________
Of course, this code relies on the fact that you are certain that the
initial string does contain 2 tabs. If it does not, it will fail.
You may want to add some error checking, something like:
'_______________________________________
Dim myString As String
Dim myVariant As Variant
Dim str1 As String
Dim str2 As String
Dim str3 As String
myString = "1111" & vbTab & "2222" & vbTab & "3333"
myVariant = Split(myString, vbTab)
If UBound(myVariant) = 2 Then
str1 = myVariant(0)
str2 = myVariant(1)
str3 = myVariant(2)
Else
MsgBox "The string does not contain two tabs."
End If
'_______________________________________
Or you may want to check the contents of the string before splitting it,
even better:
'_______________________________________
Dim myString As String
Dim myVariant As Variant
Dim str1 As String
Dim str2 As String
Dim str3 As String
Dim i As Long
Dim lngTabCount As Long
i = 0
lngTabCount = 0
myString = "1111" & vbTab & "2222" & vbTab & "3333"
Do
i = InStr(i + 1, myString, vbTab)
If i = 0 Then Exit Do
lngTabCount = lngTabCount + 1
Loop While i <= Len(myString)
If lngTabCount = 2 Then
myVariant = Split(myString, vbTab)
str1 = myVariant(0)
str2 = myVariant(1)
str3 = myVariant(2)
Else
MsgBox "The string does not contain two tabs."
End If
'_______________________________________
Again, there probably is a simple way of doing all that error checking, but
in the mean time, this works!
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org