Key thing I learned was:
Someone posted what appears to be a vba homework question on the Word forum (url below). It looked like an interesting challenge to me so I started writing a procedure. It is to look at the first and last character in each word and if the character is a vowel, to change the color of the word.It seems to be an annoying thing that VB does not examine each structure individually, rather it treats them all like they were the same and simply counts the opening and closing statements for all blocks... when they do not balance, VB simply assumes the last block structure is at fault.... At least that had always been the case for me in Excel. I am not sure why in this case it did not say you had a For without Next...
I guess when you get this sort of error you should just ignore what it says specifically and just go through all your If, For, With .. etc… blocks.
https://www.msofficeforums.com/word/45 ... post152576
When I try to run it, though, I get an "End With without With" error. But there is a with, and only one.
Here is the code:
Code: Select all
Sub VowelsTest() ' Charles Kenyon ' https://www.msofficeforums.com/word/45285-need-help-vba-assignment.html#post152576 Dim iWordCount As Long Dim iSentenceCount As Long Dim i As Long ' counter Dim strChar As String Dim iMarker As Long Dim iCharacters As Long ' Let iSentenceCount = ActiveDocument.Sentences.Count Let iWordCount = ActiveDocument.Words.Count For i = 1 To iWordCount With ActiveDocument.Words(i) Let iMarker = 0 Let iCharacters = .Characters.Count Let strChar = .Characters(1) If VowelTest(strChar) = True Then iMarker = 1 ' First character is a vowel Let strChar = .Characters(iCharacters) If VowelTest(strChar) = True Then ' Last character is a vowel If iMarker = 1 Then ' Both first and last are vowels Let iMarker = 3 Else Let iMarker = 2 ' Only last character is a vowel End If Select Case iMarker Case 1 .Font.ColorIndex = wdYellow Case 2 .Font.ColorIndex = wdBlue Case 3 .Font.ColorIndex = wdGreen End Select End With Next i End Sub Private Function VowelTest(strChar As String) As Boolean ' True if strChar is a vowel in English VowelTest = False Select Case strChar Case "a", "A", "e", "E", "i", "I", "o", "O", "u", "U", "y", "Y" VowelTest = True End Select End Function