|
-
Nov 22nd, 2011, 09:55 AM
#1
Thread Starter
Junior Member
[RESOLVED] Separate words in a label
I've tried this every which way, and I know it will be something simple, so I thought I'd come on and quickly ask if anyone knows how to do this.
I basically have a label with words in it, I'd like to separate each word and dump them into individual variables.
For example, if I had Label1 with the caption of "Hello, this is me testing", this code would dump each word, *minus* any periods, commas or other characters into individual variables and would look like this:
Label1 = "Hello, this is me testing"
Would become:
var1 = "Hello"
var2 = "this"
var3 = "is"
var4 = "me"
var5 = "testing"
If anyone could help me out, I'd be most appreciative.
-
Nov 22nd, 2011, 10:14 AM
#2
Re: Separate words in a label
Welcome to the forums 
VB Code:
Private Sub Command1_Click()
Dim strString As String
Dim var1 As String, var2 As String, var3 As String
Dim var4 As String, var5 As String
Dim arrWords() As String
strString = "Hello, this is me testing"
strString = Replace(strString, ",", "") 'NOTE: You would need a REPLACE statement for each
'character in the string the needs to be removed
arrWords = Split(strString, " ")
var1 = arrWords(0)
var2 = arrWords(1)
var3 = arrWords(2)
var4 = arrWords(3)
var5 = arrWords(4)
MsgBox var1 & " " & _
var2 & " " & _
var3 & " " & _
var4 & " " & _
var5
End Sub
-
Nov 22nd, 2011, 10:56 AM
#3
Re: Separate words in a label
Declaring variable for each word isn't reasonable - what would you do if label caption (text) is dynamic (actual text changes)?
Setting up array is more than enough:
Code:
Dim i As Long
arrWords = Split(strString, " ")
For i = 0 To UBound(arrWords)
Debug.Print arrWords(i)
Next i
-
Nov 22nd, 2011, 11:01 AM
#4
Thread Starter
Junior Member
Re: Separate words in a label
Thanks for your help!
As much as I can understand and respect using an array, for this particular function, variables are necessary.
-
Nov 22nd, 2011, 11:27 AM
#5
Re: Separate words in a label
 Originally Posted by ksmithtwn
...for this particular function, variables are necessary.
Why? Each member of array is already a variable. Why do you have to allocate twice as much memory?
-
Nov 22nd, 2011, 01:07 PM
#6
Thread Starter
Junior Member
Re: [RESOLVED] Separate words in a label
Hmm - you have a point - maybe I'm just set in my ways - could you explain how the outcome would work if I used an array?
For example -- if I wanted to to populate a listbox with each word?
Thanks for your help.
-
Nov 22nd, 2011, 01:34 PM
#7
Re: [RESOLVED] Separate words in a label
Code:
For i = 0 To UBound(arrWords)
List1.Additem arrWords(i)
Next
-
Nov 22nd, 2011, 03:27 PM
#8
Thread Starter
Junior Member
Re: [RESOLVED] Separate words in a label
This is great, and a much more efficient way to do things. Thanks for your help with this.
What I do with the output of this is throw it into a WindowsMediaPlayer playlist and then it basically plays back the audio files in order. However, using this code below, it only plays the last file, meaning it only sends the last word to WMP. What am I doing wrong? I want it to add each output word to the playlist, not just the last one.
Code:
For i = 0 To UBound(arrWords)
List1.AddItem arrWords(i)
Dim smedia As Variant
WindowsMediaPlayer1.currentPlaylist.Clear
Set smedia = WindowsMediaPlayer1.mediaCollection.Add("C:\audio\" & arrWords(i) & ".mp3")
WindowsMediaPlayer1.currentPlaylist.insertItem 0, smedia
Next
-
Nov 23rd, 2011, 10:57 AM
#9
Re: [RESOLVED] Separate words in a label
Maybe this will help you:
Code:
Option Explicit
Private Sub Command1_Click()
Dim arList() As String
Dim oMedia As IWMPMedia
Dim oPlst As IWMPPlaylist
Dim i As Long
ReDim arList(2) '<<<add 3 items
arList(0) = "C:\Windows\Media\chimes.wav"
arList(1) = "C:\Windows\Media\ringout.wav"
arList(2) = "C:\Windows\Media\tada.wav"
WindowsMediaPlayer1.currentPlaylist.Clear
For i = 0 To UBound(arList)
Set oMedia = WindowsMediaPlayer1.mediaCollection.Add(arList(i))
WindowsMediaPlayer1.currentPlaylist.insertItem 0, oMedia
Next i
Set oPlst = WindowsMediaPlayer1.currentPlaylist
For i = 0 To oPlst.Count - 1
Debug.Print oPlst.Item(i).Name
WindowsMediaPlayer1.Controls.playItem oPlst.Item(i)
Do
DoEvents
Loop Until WindowsMediaPlayer1.playState = wmppsReady
Next i
WindowsMediaPlayer1.Close
Set oMedia = Nothing
Set oPlst = Nothing
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|