Find and replace using values calculated from the found string

B

benkasminbullock

I am trying to write a program which can find and translate Japanese
dates, in the form

Heisei 19 nen 12 gatsu 14 nichi

into Western dates.

So far I have made the routine at the end of this message.

My problem is that I want to do some work on the strings in the find
and then make a replace string using the calculated values. Firstly
the months in wide format need to be replaced with words. I overcame
that problem by using a loop from one to twelve and repeatedly formed
the "find" string and its replacement using an array. Secondly the
years need to be replaced with western years. For example, Heisei 19
is 2007. It is just a matter of subtracting 1988 from the year, so I
guess I could use another loop to loop over each year. However, my
question is, what is the "smart" way to do this? That is, rather than
using these loops, how could I find the string, calculate the month
and the year, and then replace it, rather than using two loops?

I have tried using a loop of the form
Do While .Execute()
Selection.insertBefore

or

Selection.insertAfter

only to get into an infinite loop resulting in a crash.

Thanks for any advice.

=========================== Function follows (contains Japanese
characters)

Function IntToWide(i As Integer)
Dim widenumbers(0 To 9) As String
Dim j As Integer
widenumbers(0) = "$B#0(B"
widenumbers(1) = "$B#1(B"
widenumbers(2) = "$B#2(B"
widenumbers(3) = "$B#3(B"
widenumbers(4) = "$B#4(B"
widenumbers(5) = "$B#5(B"
widenumbers(6) = "$B#6(B"
widenumbers(7) = "$B#7(B"
widenumbers(8) = "$B#8(B"
widenumbers(9) = "$B#9(B"
j = i
Do While j > 0
IntToWide = widenumbers(j Mod 10) & IntToWide
j = j \ 10
Loop
End Function


Sub TranslateDate()
Dim myMonth(1 To 12) As String
Dim i As Integer
myMonth(1) = "January"
myMonth(2) = "February"
myMonth(3) = "March"
myMonth(4) = "April"
myMonth(5) = "May"
myMonth(6) = "June"
myMonth(7) = "July"
myMonth(8) = "August"
myMonth(9) = "September"
myMonth(10) = "October"
myMonth(11) = "November"
myMonth(12) = "December"
'
' Macro1 Macro
' $B5-O?F|(B 2007/12/14 $B5-O?<T(B Ben Bullock
'
For i = 1 To 12

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "$BJ?@.(B([$B#0(B-$B#9(B]{1,})$BG/(B" & IntToWide(i) & "$B7n(B([$B#0(B-$B#9(B]{1,})$BF|(B"
.Replacement.Text = myMonth(i) & " \2 \1"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Next i
End Sub
 
B

Ben Bullock

I managed to solve the problem, as follows:

Dim edate As String
With Selection.Find
.Text = "$BJ?@.(B([$B#0(B-$B#9(B0-9]{1,})$BG/(B([$B#0(B-$B#9(B0-9]{1,})$B7n(B([$B#0(B-$B#9(B0-9]{1,})$BF|(B"
...
Do While .Execute
edate = JDateToEnglish(Selection.Text)
Selection.Text = edate
Loop
End With

The trick was to use Do While Execute.

I got this trick from a web page but I can't remember what web page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top