jj2396
06-14-2012, 09:07 AM
Hi All,
New to VBA and really need help with this code. Any help would be much appreciated! :hi:
What I need the code to do is go find every instance of State & Zip code string within the body of a word document and insert a comma before the State. basically, this is part of a larger code which formats names and address.
I found snippets of codes which I modified the best I could but the code goes into an infinite loop when I run it and it inserts an infinite number of commas. the code eventually crashes and I have to close Word to recover from the crash.
This only happends when I use the "InsertBefore" method to insert the comma before the State. If I use the "InsertAfter" method, the code works great (no loop), but the comma is inserted after the state/zip code string which isn't what I want.
I haven't been able to solve this problem on my own.
In case anyone is wondering why the entire State/zip code string is included and not just the State, it's because a street could be called Victoria Street for instance or the name of a person could be Victoria and it would insert a comma before the street or the name of a person which isn't needed. Hence the search for the entire state/zipcode string before inserting the comma.
Here is the code
Sub Insert_comma()
Dim commaText As String
commaText = ","
Dim rTextStateZip As Range
Dim vFindStateZip(8) As String
Dim z As Long
' variable arrays for the different states with zipcodes as wildcards
vFindStateZip(0) = (" South Australia " & "???? ")
vFindStateZip(1) = (" Western Australia " & "???? ")
vFindStateZip(2) = (" Northern Territory " & "???? ")
vFindStateZip(3) = (" Tasmania " & "???? ")
vFindStateZip(4) = (" Victoria " & "???? ")
vFindStateZip(5) = (" New South Wales " & "???? ")
vFindStateZip(6) = (" Queensland " & "???? ")
vFindStateZip(7) = (" Australian Capital Territory " & "???? ")
For z = 0 To UBound(vFindStateZip)
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=vFindStateZip(z), _
MatchWildcards:=True, _
MatchWholeWord:=True, _
MatchCase:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rTextStateZip = Selection.Range 'The found text
With rTextStateZip 'Do what you want with the found text
Select Case z
Case Is = 0 'South Australia
rTextStateZip.InsertBefore (commaText)
Case Is = 1 'Western Australia
rTextStateZip.InsertBefore (commaText)
Case Is = 2 'Northern Territory
rTextStateZip.InsertBefore (commaText)
Case Is = 3 'Tasmania
rTextStateZip.InsertBefore (commaText)
Case Is = 4 'Victoria
rTextStateZip.InsertBefore (commaText)
Case Is = 5 'New South Wales
rTextStateZip.InsertBefore (commaText)
Case Is = 6 'Queensland
rTextStateZip.InsertBefore (commaText)
Case Is = 7 'Australian Capital Territory
rTextStateZip.InsertBefore (commaText)
End Select
End With
Loop 'and look for the next match
End With
End With
Next z
End Sub
New to VBA and really need help with this code. Any help would be much appreciated! :hi:
What I need the code to do is go find every instance of State & Zip code string within the body of a word document and insert a comma before the State. basically, this is part of a larger code which formats names and address.
I found snippets of codes which I modified the best I could but the code goes into an infinite loop when I run it and it inserts an infinite number of commas. the code eventually crashes and I have to close Word to recover from the crash.
This only happends when I use the "InsertBefore" method to insert the comma before the State. If I use the "InsertAfter" method, the code works great (no loop), but the comma is inserted after the state/zip code string which isn't what I want.
I haven't been able to solve this problem on my own.
In case anyone is wondering why the entire State/zip code string is included and not just the State, it's because a street could be called Victoria Street for instance or the name of a person could be Victoria and it would insert a comma before the street or the name of a person which isn't needed. Hence the search for the entire state/zipcode string before inserting the comma.
Here is the code
Sub Insert_comma()
Dim commaText As String
commaText = ","
Dim rTextStateZip As Range
Dim vFindStateZip(8) As String
Dim z As Long
' variable arrays for the different states with zipcodes as wildcards
vFindStateZip(0) = (" South Australia " & "???? ")
vFindStateZip(1) = (" Western Australia " & "???? ")
vFindStateZip(2) = (" Northern Territory " & "???? ")
vFindStateZip(3) = (" Tasmania " & "???? ")
vFindStateZip(4) = (" Victoria " & "???? ")
vFindStateZip(5) = (" New South Wales " & "???? ")
vFindStateZip(6) = (" Queensland " & "???? ")
vFindStateZip(7) = (" Australian Capital Territory " & "???? ")
For z = 0 To UBound(vFindStateZip)
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=vFindStateZip(z), _
MatchWildcards:=True, _
MatchWholeWord:=True, _
MatchCase:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set rTextStateZip = Selection.Range 'The found text
With rTextStateZip 'Do what you want with the found text
Select Case z
Case Is = 0 'South Australia
rTextStateZip.InsertBefore (commaText)
Case Is = 1 'Western Australia
rTextStateZip.InsertBefore (commaText)
Case Is = 2 'Northern Territory
rTextStateZip.InsertBefore (commaText)
Case Is = 3 'Tasmania
rTextStateZip.InsertBefore (commaText)
Case Is = 4 'Victoria
rTextStateZip.InsertBefore (commaText)
Case Is = 5 'New South Wales
rTextStateZip.InsertBefore (commaText)
Case Is = 6 'Queensland
rTextStateZip.InsertBefore (commaText)
Case Is = 7 'Australian Capital Territory
rTextStateZip.InsertBefore (commaText)
End Select
End With
Loop 'and look for the next match
End With
End With
Next z
End Sub