GilesB
11-18-2008, 01:47 PM
Hi All,
Hoping someone can point me in the right direction on a couple of things. I'm trying to write a macro that when run will find words of a particular pattern in my document and then paste the list to a new document. I've pretty much got it working with the code below, but the search patterns I can use in Selection.Find.Text and using the Like operator seem to be very limited:
Sub ListAcronyms()
Dim strAcronym As String
Dim strDictAcron As New Scripting.Dictionary
Dim str As Variant
Dim strTemp As String
Application.ScreenUpdating = False 'Dont update the screen
'Start from the top of the document, this could cause problems if acronymn list has laready been added.
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowHiddenText = False
'Find a possible acronym
Do
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting 'Clear previous search
.Text = "<[A-Z]*>"
.Replacement.Text = ""
.Forward = True
.Wrap = Word.wdFindStop
.Format = False
.MatchCase = True
.MatchWildcards = True
.MatchWholeWord = True
.Execute
End With
If Selection.Find.Found Then
strAcronym = Selection.Text
strTemp = Selection.Text
strTemp = Right(strTemp,(Len(strTemp)-1))
If strTemp Like "*[A-Z]*" Then
strDictAcron.add strAcronym, strAcronym
End If
End If
Loop Until Not Selection.Find.Found
If strDictAcron.Count > 0 Then
Dim newDoc As Document
Set newDoc = Documents.Add
Selection.HomeKey Unit:=wdStory
'Type the text
For Each str In strDictAcron
Selection.TypeText Text:=str & vbCr
Next str
newDoc.Content.Sort SortOrder:=wdSortOrderAscending
End If
End Sub
For example one of the searches I might want to implement would be to find words that don't start with an uppercase letter but do have uppercase letters elsewhere in the word.
Is there a better way to achieve what I'm trying to achieve?
Can I use complex regular expressions in the code I'm using now or would I need to take a different approach?
Also another question, what exactly does the Word Constant wdFindStop mean? And is there a constant I could use to ensure the selection ends with the next white space character? For example with the aobve code A.B.C gets selected as A.B
All help will be much appreciated, thanks.
Giles
Hoping someone can point me in the right direction on a couple of things. I'm trying to write a macro that when run will find words of a particular pattern in my document and then paste the list to a new document. I've pretty much got it working with the code below, but the search patterns I can use in Selection.Find.Text and using the Like operator seem to be very limited:
Sub ListAcronyms()
Dim strAcronym As String
Dim strDictAcron As New Scripting.Dictionary
Dim str As Variant
Dim strTemp As String
Application.ScreenUpdating = False 'Dont update the screen
'Start from the top of the document, this could cause problems if acronymn list has laready been added.
Selection.HomeKey Unit:=wdStory
ActiveWindow.View.ShowHiddenText = False
'Find a possible acronym
Do
Selection.Find.ClearFormatting
With Selection.Find
.ClearFormatting 'Clear previous search
.Text = "<[A-Z]*>"
.Replacement.Text = ""
.Forward = True
.Wrap = Word.wdFindStop
.Format = False
.MatchCase = True
.MatchWildcards = True
.MatchWholeWord = True
.Execute
End With
If Selection.Find.Found Then
strAcronym = Selection.Text
strTemp = Selection.Text
strTemp = Right(strTemp,(Len(strTemp)-1))
If strTemp Like "*[A-Z]*" Then
strDictAcron.add strAcronym, strAcronym
End If
End If
Loop Until Not Selection.Find.Found
If strDictAcron.Count > 0 Then
Dim newDoc As Document
Set newDoc = Documents.Add
Selection.HomeKey Unit:=wdStory
'Type the text
For Each str In strDictAcron
Selection.TypeText Text:=str & vbCr
Next str
newDoc.Content.Sort SortOrder:=wdSortOrderAscending
End If
End Sub
For example one of the searches I might want to implement would be to find words that don't start with an uppercase letter but do have uppercase letters elsewhere in the word.
Is there a better way to achieve what I'm trying to achieve?
Can I use complex regular expressions in the code I'm using now or would I need to take a different approach?
Also another question, what exactly does the Word Constant wdFindStop mean? And is there a constant I could use to ensure the selection ends with the next white space character? For example with the aobve code A.B.C gets selected as A.B
All help will be much appreciated, thanks.
Giles