Results 1 to 9 of 9

Thread: [RESOLVED] Separate words in a label

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Resolved [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.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Separate words in a label

    Welcome to the forums
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strString As String
    3. Dim var1 As String, var2 As String, var3 As String
    4. Dim var4 As String, var5 As String
    5. Dim arrWords() As String
    6. strString = "Hello, this is me testing"
    7. strString = Replace(strString, ",", "") 'NOTE:  You would need a REPLACE statement for each
    8.                                         'character in the string the needs to be removed
    9. arrWords = Split(strString, " ")
    10. var1 = arrWords(0)
    11. var2 = arrWords(1)
    12. var3 = arrWords(2)
    13. var4 = arrWords(3)
    14. var5 = arrWords(4)
    15. MsgBox var1 & " " & _
    16. var2 & " " & _
    17. var3 & " " & _
    18. var4 & " " & _
    19. var5
    20. End Sub

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    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.

  5. #5

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    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.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] Separate words in a label

    Code:
    For i = 0 To UBound(arrWords)
        List1.Additem arrWords(i)
    Next

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    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

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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
  •  



Click Here to Expand Forum to Full Width