Results 1 to 28 of 28

Thread: [Resolved]Searching string array

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    [Resolved]Searching string array

    hi,

    im new to VB - so hope ive come to the right place. basically i've got to create a macro that searches through highlighed text for characters (ie. comma's, fullstop, etc) and counts those occurents against actual words. Now i started this the basic way and highlighted text to detect the punctuation characters but im unable to get them to recoginse more than 1 punctuation character (is there an "OR" function in Instr?). Also not sure how to count them, i know i would store them in an array and then continue the count - but i'm having problems with that? could someone give me some insight on this?

    thanks a lot.
    Last edited by chaos7; Apr 30th, 2005 at 09:15 AM. Reason: Resolved

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Welcome to the Forums.

    Check out my code example in this thread. I assume your talking about Word.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    Hi Rob,

    Thanks for that. I will test it out once i get home. Basically i want select some text on MSWord and then search for these symbols ('.' ',' '?' '!') and count how many times they occur on the selected text, i assumed that i would make an array (for the selected words) and then go through finding and counting the above symbols? But i thought we would need to use a for loop for this (using UBound & LBound)?? Sorry im still trying to learn VB - and doing it at work is prob not the best thing
    Pls let me know if im on right track...or how to get there...

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    hi...

    okay i have been trying to test what i am supposed to do with arrays as i thought that would be the best way, but i was not having any luck, so i switched to do just a simple comma count loop. what i want to do is search the selected text for comma's and count how many times they occur but my program freezes everytime i click the button.....my code is as below, can someone pls help...

    Private Sub CommandButton1_Click()
    Dim myText As String
    Dim counter As Long

    myText = Selection.Text
    counter = 0

    Do While InStr(myText, ",")
    counter = counter + 1
    Loop

    MsgBox "Number of times a comma has occured is equal to " & counter

    End Sub

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Your instr function is missing its Start parameter.
    VB Code:
    1. Dim myText As String
    2.     Dim counter As Long
    3.    
    4.     myText = Selection.Text
    5.     counter = 1
    6.    
    7.     Do While InStr(counter, myText, ",")
    8.         counter = counter + 1
    9.     Loop
    10.     MsgBox "Number of times a comma has occured is equal to " & counter - 1
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    hi rob...i tried that code, but when i run it, it returns with the position of the comma?? for example i select the following code in a text "Analysis, should be" and my msg box indicates that there are 9 comma's in the text?!! so i think its finding the position or something??? do you know what im doing wrong??
    also in C++ when we search for more than one thing we can use the OR (||) command - can we do this in VBA as well in the InStr function in the loop?? anyway sorry to be bugging you all the time - my vb book sux!! theres not a whole lot on strings..more on integers and stuff..

    thanks

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Sorry, I forgot to increase by the position of the comma.
    VB Code:
    1. Dim myText As String
    2.     Dim counter As Integer
    3.     Dim iPos As Integer
    4.    
    5.     myText = "Testing, testing,testing"
    6.     counter = 0
    7.     iPos = 0
    8.     iPos = InStr(1, myText, ",")
    9.     Do While iPos > 0
    10.         iPos = InStr(iPos, myText, ",")
    11.         If iPos > 0 Then iPos = iPos + 1
    12.         counter = counter + 1
    13.     Loop
    14.     MsgBox counter '3
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    hi rob,

    thank you for that i added the script and placed a "counter = counter - 1" just after the loop and before the MsgBox prompt - as it was giving me an extra comma. now i will test it for my highligted text.

    do you know how i can add to this to allow to search for fullstop, question mark, and exclamation mark? i was looking for an OR command but there isnt one..something like...
    IF iPos = InStr(1, myText, ",") || iPos = InStr(1, myText, ".") || iPos = InStr(1, myText, "?") THEN
    do while loop etc...
    but syntax doesnt work?

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    actually...i dont even know if the OR will work for what i want to do. i just want to add the number of times the following symbols ("," , "." , "?", "!") occur in the text in proportion to the number of words selected. so its working for counting the first comma symbol but how do i get it to count for the other 3 as well?? i thought maybe to put it in the IF/OR statement above - but not sure if that will do the trick????

    plzzzzzzzzz help, i cant even get this right!!

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    You need to do three separate loops or use Or conditions in the Do While loop condition.
    VB Code:
    1. '|| = Or
    2.  
    3. Do While (Instr(1, Text1.Text, ",") > 0)  Or Instr(1, Text1.Text, "?") > 0) Or (Instr(1, Text1.Text, ".") > 0)
    4.     'do your stuff to test for each of the three.
    5.     'increment once for each occurance of the 3
    6. Loop
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    hi rob,

    okay i've got another idea...which may work as im not sure my other one did - and i actually even tried to code it myself well kind of but im getting an error, basically now im just trying to pass it off as a function and then call it from the main command...but im getting a compile error "expected function or call"? maybe im calling it wrong from the CommandButton1_Click() function????????

    Private Sub CommandButton1_Click()

    Dim myText As String
    Dim countComma As Integer
    Dim countFullstop As Integer


    myText = Selection.Text
    countComma = findChar(myText, ",")
    countFullstop = findChar(myText, ".")


    MsgBox "No. of comma's is: " & countComma
    MsgBox "No. of fullstop's is: " & countFullstop


    End Sub

    Private Sub findChar(Text As String, myChar As String)


    Dim counter As Integer
    Dim iPos As Integer

    'Text = Selection.Text

    counter = 0
    iPos = 0
    iPos = InStr(1, Text, myChar)
    Do While iPos > 0
    iPos = InStr(iPos, Text, myChar)
    If iPos > 0 Then iPos = iPos + 1
    counter = counter + 1
    Loop
    counter = counter - 1

    End Sub

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Ok, your calling a sub but trying to return a value from it to set in the msgbox.
    You need to change things a little.

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3.     Dim myText As String
    4.     Dim countComma As Integer
    5.     Dim countFullstop As Integer
    6.  
    7.     myText = Selection.Text
    8.     countComma = findChar(myText, ",")
    9.     countFullstop = findChar(myText, ".")
    10.  
    11.     MsgBox "No. of comma's is: " & countComma
    12.     MsgBox "No. of fullstop's is: " & countFullstop
    13.  
    14. End Sub
    15.  
    16. Private Function findChar(Text As String, myChar As String) As Integer 'Returns a Integer value
    17.  
    18.     Dim counter As Integer
    19.     Dim iPos As Integer
    20.  
    21.     'Text = Selection.Text
    22.  
    23.     counter = 0
    24.     iPos = 0
    25.     iPos = InStr(1, Text, myChar)
    26.     Do While iPos > 0
    27.         iPos = InStr(iPos, Text, myChar)
    28.         If iPos > 0 Then
    29.             iPos = iPos + 1
    30.             counter = counter + 1
    31.         End If
    32.     Loop
    33.     'counter = counter - 1
    34.     findChar = counter
    35.    
    36. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    hi rob,

    Thanks rob - i love you!!!!
    its working...i just changed the 'End Sub' at the end of the function to 'End Function' and it compiled. Now i'll try not to hassle you too much and do the word count all by myself

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Doh! I guess thats what I get for typing in the Reply box and not VB6.

    I already fixed the count = count - 1 at the end. It needed an if then endif block to only increment counter if it found a match.

    For the word count, try splitting the entire string into an array on the space character.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    Okay im stuck!! My program works now and gives me the ouput needed but ONLY if i select text. if i dont select text then i get a run-time error due to the proportion part of the essay (if i comment that out everything is okay))) but its the dividing by zero that is giving me the problem....i tried to make another small function out of it but that didn't work even after forcing it as a Double?? how do i fix this????

    Private Sub CommandButton1_Click()

    Dim myText As String 'holds text as string variable
    Dim countComma As Integer 'stores number of comma's in selected text
    Dim countFullstop As Integer 'stores number of fullstop's in selected text
    Dim propComma As Double 'proportion of commas in regard to words
    Dim propFullstop As Double

    'To count words
    Dim myRange As Range
    Set myRange = Selection.Range
    Dim wordCount As Integer
    wordCount = myRange.ComputeStatistics(wdStatisticWords)

    myText = Selection.Text

    countComma = findChar(myText, ",")
    countFullstop = findChar(myText, ".")

    propComma = propChar(countComma, wordCount)
    'propComma = countComma / wordCount
    ' propFullstop = countFullstop / wordCount

    MsgBox "No. of comma's: " & countComma
    MsgBox "No. of fullstop's: " & countFullstop
    MsgBox "No. of words selected: " & wordCount
    MsgBox "Proportion of comma's (,) to words is: " & propComma
    'MsgBox "Proportion of fullstop's (.) to words is: " & propFullstop

    End Sub

    'Private Function findChar(Text As String, myChar As String) As Integer 'Returns a Integer value
    'Searches for and counts the char in the text
    'End Function

    Private Function propChar(char As Integer, wcount As Integer) As Double 'Returns a Double value

    propChar = char / wcount 'THIS IS THE PROBLEM LINE...

    End Function

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Use the WholeStory of the range object to set a range to the entire document.
    VB Code:
    1. Private Sub Test()
    2.     Dim myRange As Range
    3.     Set myRange = ActiveDocument.Range
    4.     myRange.WholeStory
    5. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    hi .. i tried doing that but now it gives me all the number of words (even when its not selected), i placed it at the top like this. Played around with it and kept Selection.Range but still didn't work??

    'To count words
    Dim myRange As Range
    'Set myRange = Selection.Range
    Set myRange = ActiveDocument.Range
    myRange.WholeStory
    Dim wordCount As Integer
    wordCount = myRange.ComputeStatistics(wdStatisticWords)

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Thats what WholeStory is, the entire document.I thought thats what you wanted? You said that if you dont select
    text you get an error. so I thought that you wanted all. If you want just the selected text then use
    wordCount = Selection.Range.ComputeStatistics(wdStatisticWords)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    sorry...what i meant is that when i don't select any text i get an overflow error when im trying to calculate number of decimal's by wordcount (overflow error) BUT when i select the text - it works fine. so i don't know if its because im dividing by zero that its giving me the overflow error (no text selected, for example, won't let me calculate "propComma = propChar(countComma, wordCount)"). Because when i put the below in to select word count its okay.

    VB Code:
    1. Dim myRange As Range
    2. Set myRange = Selection.Range
    3. wordCount = myRange.ComputeStatistics(wdStatisticWords)

    I'm not sure why the overflow error is coming - i tried to force the propChar function to return a Double - but its not working either (code pasted in last post) - hope this makes sense...??

  20. #20
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Oh, then we only need to detect if the count is equal to zero before dividing. Thats the error.
    VB Code:
    1. If wcount > 0 Then
    2.     propChar = char / wcount 'THIS IS THE PROBLEM LINE...
    3. Else
    4.     propChar = 0
    5. End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    Thanks Rob. I can't believe it was as simple as that - i feel so stupid!! I spent hours on this one last night - i knew the number/0 gives errors, so i was doing all sorts of weird statements (adding # at the end of one of the ints) etc.. Anyway im onto the next part (which thank god is the last part!! whoo-hoo!!). Although its Distribution of sentences!! :eek

  22. #22

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    OK this is the last bit i have to do - and i promise i'll stop harrassing you rob!! okay what i have to do in this part is "the distribution of the number of commas per sentence (i.e. how many sentences have 0 or 1 or more comm'as)" -- so we need to break down each sentence. i can do a sentence count okay, which gives me the number of sentences, but how to break it down and say sentence 1 = .., etc..not sure.
    I've put the program in a do while loop to iterate over each sentence and find out how many comma per sentence, it just gives me the total number of comma's..??
    VB Code:
    1. Dim sentenceCount As Integer
    2.         Dim newCount As Integer
    3.         sentenceCount = Selection.Sentences.count
    4.         newCount = sentenceCount - 1
    5.         Dim sText As String
    6.         myText = Selection.Text
    7.         Dim commaCount As Integer
    8.        
    9.          Do While sentenceCount > 0
    10.                commaCount = findChar(myText, ",")
    11.                   sentenceCount = sentenceCount - 1
    12.                Loop
    13.                     MsgBox "commas = " & commaCount

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    The issue is that ou need it to search just the sentence only and not the entire document.

    Can you post your updated findChar procedure?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    yep so now instead of counting the words in the selected text, i count the sentences and search through each sentence for a comma and display how many times a comma occured in each sentence (is 1 comma in first sentence, 0 in second etc) , i thought i'd use the same findChar function as before:
    VB Code:
    1. Private Function findChar(Text As String, myChar As String) As Integer 'Returns a Integer value
    2.  
    3.     Dim counter As Integer
    4.     Dim iPos As Integer
    5.  
    6.     counter = 0
    7.     iPos = 0
    8.     iPos = InStr(1, Text, myChar)
    9.     Do While iPos > 0
    10.         iPos = InStr(iPos, Text, myChar)
    11.         If iPos > 0 Then
    12.             iPos = iPos + 1
    13.             counter = counter + 1
    14.         End If
    15.     Loop
    16.    
    17.     findChar = counter
    18.    
    19. End Function
    And then call it from the main program as this:
    VB Code:
    1. 'Prints out sentences highlighted
    2.         Dim sentenceCount As Integer
    3.         Dim newCount As Integer
    4.        
    5.         'Gets number of all sentences selected
    6.         sentenceCount = Selection.Sentences.count
    7.         newCount = sentenceCount - 1
    8.        
    9.         'If sentenceCount > 0 Then
    10.        '       MsgBox "Sentences selected = " & newCount
    11.         '     Else
    12.         '     MsgBox "Sentences selected = 0"
    13.           '   End If
    14.           Do While sentenceCount > 0  'if sentence highlighted
    15.                commaCount = findChar(myText, ",")  'find comma
    16.                   sentenceCount = sentenceCount - 1 'suppose to iterate
    17.                Loop
    18.                     MsgBox "commas found in  sentence" & sentenceCount     & "equals " & commaCount  'but only comes out with total number of comma's in all sentences

    ??

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Searching string array

    Change the passed arguments from ByRef (default) to ByVal.
    VB Code:
    1. Private Function findChar(ByVal Text As String, ByVal myChar As String) As Integer 'Returns a Integer value
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: Searching string array

    no its not working...
    i think its to do with my do while loop (?) i put the comma search in the sub command instead of function - to test it...but that didnt work it out so i had:
    VB Code:
    1. Private Sub CommandButton2_Click()
    2.  
    3.     Dim myText As String
    4.     Dim counter As Integer
    5.     Dim iPos As Integer
    6.     Dim commaCount As Integer
    7.    
    8.    
    9.     myText = Selection.Text
    10.    
    11.     counter = 0
    12.     iPos = 0
    13.     iPos = InStr(1, myText, ",")
    14.     Do While iPos > 0
    15.         iPos = InStr(iPos, myText, ",")
    16.         If iPos > 0 Then iPos = iPos + 1
    17.         counter = counter + 1
    18.     Loop
    19.     counter = counter - 1
    20.    ' MsgBox counter
    21.    
    22.       'Prints out sentences highlighted
    23.         Dim sentenceCount As Integer
    24.         Dim newCount As Integer
    25.        
    26.         sentenceCount = Selection.Sentences.count
    27.         newCount = sentenceCount - 1
    28.        
    29.         'If sentenceCount > 0 Then
    30.       '       MsgBox "Sentences selected = " & newCount
    31.         '     Else
    32.         '     MsgBox "Sentences selected = 0"
    33.           '   End If
    34.           Do While sentenceCount > 0
    35.                commaCount = counter;
    36.                   sentenceCount = sentenceCount - 1
    37.                Loop
    38.                     MsgBox "commas found in " & sentenceCount & commaCount
    39.            
    40. End Sub
    so the output of the sentences (say i've selected them) below should be:
    Please God, let this
    be, over, very, soon.

    Sentence 1 = 1 comma
    Senctence 2 = 3 comma

    I think its the do while loop i havent written it properly or something?/

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Resolved]Searching string array

    Did you change the procedure signature for findchar?

    Also, the do loop is doing nothing more then counting down to zero? Or is sentenceCount and commaCount function names?


    VB Code:
    1. Do While sentenceCount > 0
    2.        commaCount = counter;
    3.        sentenceCount = sentenceCount - 1
    4. Loop
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  28. #28

    Thread Starter
    Member
    Join Date
    Apr 2005
    Posts
    49

    Re: [Resolved]Searching string array

    I did...but it didn't work. I kind of gave up and moved to the next steps . I ended up finishing the whole program last night, so left this part undone. Oh well....next time....im just glad its over....for now

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