PDA

View Full Version : Solved: Perform Action based on 1st letter of next word in text



CaptainJack
07-28-2008, 02:03 PM
I'm writing a Word macro (and I don't write Word macros often) that will insert some text at a certain point in my document based on the first letter of the next word after the cursor in the document.

For example: In my document I have this text:
"See also Compliance"

and I wand to insert "A-C:" in front of the word Compliance

So, after the insert the sentence would read "See also A-C:Compliance"

"So, just type it" you say... well, there are approximately 6000 of these little goodies in my doc so I need a macro.:banghead:

I can record most of the steps, however I'm at a loss as to how to write the code that will select the word "Compliance", stuff it into a variable and use it in a case statement that says basically:
If the first letter of the selected word is greater than or equal to A and less than or equal to C, insert "A-C:" before the selected word.

Any help would be greatly appreciated.:help

\/Ba41337Pwn
07-28-2008, 03:53 PM
Couldn't you just do a Replace command replacing Compliance with A-C:Compliance? :think:

Nelviticus
07-29-2008, 03:37 AM
This will do it for every word in your document that follows the phrase "see also":
Public Sub WordGrouping()

Dim ThisWord As String
Dim Word1 As String
Dim Word2 As String
Dim iWord As Integer

iWord = 1

While iWord < ActiveDocument.Words.Count

ThisWord = ActiveDocument.Words(iWord).Text

If UCase(Trim(Word1)) = "SEE" And UCase(Trim(Word2)) = "ALSO" Then
ActiveDocument.Words(iWord).Text = GroupWord(ThisWord)
' We add 5 here because 'A-C:' is treated as four (new) words
iWord = iWord + 5
Else
iWord = iWord + 1
End If

Word1 = Word2
Word2 = ThisWord

Wend

End Sub

Private Function GroupWord(KeyWord As String) As String

Dim GroupedWord As String

Select Case UCase(Left$(KeyWord, 1))
Case "A" To "C"
GroupedWord = "A-C:" + KeyWord
Case "D" To "F"
GroupedWord = "D-F:" + KeyWord
Case "G" To "I"
GroupedWord = "G-I:" + KeyWord
Case "J" To "L"
GroupedWord = "J-L:" + KeyWord
Case "M" To "O"
GroupedWord = "M-O:" + KeyWord
Case "P" To "R"
GroupedWord = "P-R:" + KeyWord
Case "S" To "U"
GroupedWord = "S-U:" + KeyWord
Case "V" To "Z"
GroupedWord = "V-Z:" + KeyWord
Case Else
GroupedWord = KeyWord
End Select

GroupWord = GroupedWord

End Function
That may not be quite what you want but maybe it'll get you started.

Regards

CaptainJack
07-29-2008, 05:45 AM
That may not be quite what you want but maybe it'll get you started.


Thanks, you're right... not exactly what was needed but got me on the right track.

Cheers!