#1
|
|||
|
|||
ActiveDocument.Words(i).Characters.Last.Text statment not working
I'm working on a text mining program to detect instances of hiatus, which is when the last letter of a word and the first letter of the next word both start with vowels. Eventually I'd like to use and/or logic to detect any vowel combinations, but I was having difficulty with the program and simplified things to figure out what was wrong. My If statement is never triggering because the ActiveDocument.Words(i).Characters.Last.Text statement doesn't seem to work. I can find vowels as the first letter of the word using ActiveDocument.Words(j).Characters.First.Text with no problems, but for some reason it can't detect them at the end of words. Here is a simplified version of the code I've been using to troubleshoot. Its only trying to look for the letter "e" at the end of words, but the if statement won't trigger. Thanks in advance for the help!
Sub hiatus() 'this bit selects the document and sets up a way to cycle through words Dim doc As Word.Document Dim i As Integer Dim j As Integer Dim hiatus As Integer 'initialize counters must start on 1st and 2nd word i = 1 j = 2 hiatus = 0 Set doc = Word.ActiveDocument 'loop through words For i = 1 To (doc.words.Count - 1) 'this bit searches for if the word meets certain criteria If ActiveDocument.Words(i).Characters.Last.Text = "e" Then hiatus = hiatus + 1 End If 'increment counters j = j + 1 Next i doc.Range.InsertAfter "hiatus =" & (CStr(hiatus)) End Sub |
#2
|
||||
|
||||
The problem you're having is mainly due to each 'word' including the trailing space, if any. Even so, there's a simpler way:
Code:
Sub Demo() Application.ScreenUpdating = False Dim StrOut As String With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "<[!^13 ]@[aeiouy] [aeiouy][! ]@>" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found StrOut = StrOut & vbCr & .Text .Collapse wdCollapseEnd .Find.Execute Loop End With ActiveDocument.Range.InsertAfter vbCr & Chr(12) & "Hiatus List" & StrOut Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Awesome thanks! It worked really well for the English texts. I'm also doing a few in Greek. Usually the only changes that need to be made between the two are replacing the English letters with ChrW(###) statements for the Greek letters I need. VBA can't handle the Greek directly so I've been using ChrW and the unicode numbers of the Greek characters. Just subbing in ChrW(###) for the letters doesn't seem to be working in this case. Any insights? I'm super impressed with your code by the way. I'm an industrial engineering student so my programs are still pretty clunky.
|
#4
|
||||
|
||||
You could use something like:
Code:
Sub Demo() Application.ScreenUpdating = False Dim StrFnd As String, StrOut As String StrFnd = ChrW(&H3AC) & "-" & ChrW(&H3AF) & ChrW(&H3B1) & ChrW(&H3B5) & _ ChrW(&H3B7) & ChrW(&H3B9) & ChrW(&H3BF) & ChrW(&H3C5) & ChrW(&H3C9) & _ ChrW(&H3CC) & "-" & ChrW(&H3CE) & ChrW(&H1F00) & "-" & ChrW(&H1F07) & _ ChrW(&H1F10) & "-" & ChrW(&H1F15) & ChrW(&H1F20) & "-" & ChrW(&H1F27) & _ ChrW(&H1F30) & "-" & ChrW(&H1F37) & ChrW(&H1F40) & "-" & ChrW(&H1F45) & _ ChrW(&H1F50) & "-" & ChrW(&H1F57) & ChrW(&H1F60) & "-" & ChrW(&H1F67) & _ ChrW(&H1F70) & "-" & ChrW(&H1F87) & ChrW(&H1F90) & "-" & ChrW(&H1F97) & _ ChrW(&H1FA0) & "-" & ChrW(&H1FA7) & ChrW(&H1FB0) & "-" & ChrW(&H1FB7) & _ ChrW(&H1FC2) & "-" & ChrW(&H1FC7) & ChrW(&H1FD0) & "-" & ChrW(&H1FD7) & _ ChrW(&H1FE0) & "-" & ChrW(&H1FE3) & ChrW(&H1FE6) & "-" & ChrW(&H1FE7) & _ ChrW(&H1FF2) & "-" & ChrW(&H1FF7) With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "<[!^13 ]@" & StrFnd & " " & StrFnd & "[! ]@>" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found StrOut = StrOut & vbCr & .Text .Collapse wdCollapseEnd .Find.Execute Loop End With ActiveDocument.Range.InsertAfter vbCr & Chr(12) & "Hiatus List" & StrOut Application.ScreenUpdating = True End Sub If you want to see what StrFnd contains, insert: .InsertAfter StrFnd before: With .Find and run the macro in an empty document.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
ActiveDocument.ContentControls(ID) not working | Tejas.T | Word VBA | 3 | 03-09-2015 06:50 PM |
How show characters instead words (status bar) | BrunoChirelli | Word | 2 | 02-19-2015 12:03 PM |
wordapp.ActiveDocument.SaveAs Not Working | KSReynolds | Mail Merge | 1 | 07-18-2014 04:03 PM |
Color words ending with special characters | Singh_Edm | Word | 2 | 01-20-2014 12:51 AM |
Array into ComboBox + Macro-Text into ActiveDocument | Vivi | Word VBA | 1 | 01-27-2010 07:03 AM |