Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Find and replace using values calculated from the found string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
benkasminbullock@gmail.com - 14 Dec 2007 05:11 GMT
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) = "0"
widenumbers(1) = "1"
widenumbers(2) = "2"
widenumbers(3) = "3"
widenumbers(4) = "4"
widenumbers(5) = "5"
widenumbers(6) = "6"
widenumbers(7) = "7"
widenumbers(8) = "8"
widenumbers(9) = "9"
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
' 記録日 2007/12/14 記録者 Ben Bullock
'
For i = 1 To 12

   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
       .Text = "平成([0-9]{1,})年" & IntToWide(i) & "月([0-9]{1,})日"
       .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
Ben Bullock - 14 Dec 2007 13:10 GMT
I managed to solve the problem, as follows:

Dim edate As String
  With Selection.Find
      .Text = "平成([0-90-9]{1,})年([0-90-9]{1,})月([0-90-9]{1,})日"
...
      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.
benkasminbullock@gmail.com - 14 Dec 2007 13:59 GMT
> I managed to solve the problem, as follows:

P.S. I have put the whole macro here if anyone is interested:
http://linuxtnt.wordpress.com/2007/12/14/microsoft-word-macro-to-convert-japanes
e-dates-to-english/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.