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 / January 2006

Tip: Looking for answers? Try searching our database.

Translation Macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mido - 12 Jan 2006 23:36 GMT
Hello folks, first of all I would like to thank you for your effort and
great help.
I wrote a macro that I use to translate short texts into French.
I worked on the assumption that the first lines of the macro are
executed first, so I used the macro to search for exact phrase match in
the first lines of code and replace them with exact phrase match in
English.
Then I started using find and replace for single words. I did this to
avoid changing words at the beginning from Phrases where the meaning is
more than just the combination.
My code looked something like this:

Sub French()
'
' Translate phrases and words from English into French
' Translate phrases first then the single words
' Use blue for the translated words to verify the translation
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   Selection.Find.Replacement.Font.Color = wdColorBlue

Selection.Find.Execute Replace:=wdReplaceAll
   With Selection.Find
       .Text = "First game begins"
       .Replacement.Text = "Jeu commence"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchCase = False
       .MatchWholeWord = True
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
Selection.Find.Execute Replace:=wdReplaceAll
   With Selection.Find
       .Text = "Floral Cart"
       .Replacement.Text = "Chariot de fleurs"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchCase = False
       .MatchWholeWord = True
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
Selection.Find.Execute Replace:=wdReplaceAll
   With Selection.Find
       .Text = "Cart"
       .Replacement.Text = "Chariot"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchCase = False
       .MatchWholeWord = True
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
   End With
   Selection.Find.Execute Replace:=wdReplaceAll
End Sub
To trim the code I then excluded all the instances of  False such as
.MatchCase = False
unless it was absolutly necessary for the language issue. So my code
now looks something like this:

Selection.Find.Execute Replace:=wdReplaceAll
   With Selection.Find
       .Text = "First game begins"
       .Replacement.Text = "Jeu commence"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchWholeWord = True
   End With
Selection.Find.Execute Replace:=wdReplaceAll
   With Selection.Find
       .Text = "Floral Cart"
       .Replacement.Text = "Chariot de fleurs"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchWholeWord = True
   End With
Selection.Find.Execute Replace:=wdReplaceAll
   With Selection.Find
       .Text = "Cart"
       .Replacement.Text = "Chariot"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchWholeWord = True
   End With
Selection.Find.Execute Replace:=wdReplaceAll
The Macro seem to be working fine, but there are simply too many lines
wich makes it difficult to maintain and it consumes lots of ressources
as I have hundreds and hundreds of line. Is there is a simpler way to
serach and change a specific phrases, word  combination and then single
words? Should I keep all the statements with the False  value or am I
right in deleting them?
Thanks a lot for your help!
Dave Lett - 13 Jan 2006 20:17 GMT
Hi Mido,

You can shorten the routine some by not repeating all of the parameters for
the find/replace. Also, instead of creating a separate block for each
replace, you might consider creating two arrays and using that to cycle
through the find and replace words. Here's an example of eliminating the
repetition and using arrays for the find and replace.

Dim sFind As Variant
Dim sReplace As Variant
Dim iCount As Integer

sFind = Array("Test1", "Test2", "Test3", "Test4")
sReplace = Array("Replace1", "Replace2", "Replace3", "Replace4")
With Selection.Find
   .ClearFormatting
   .Forward = True
   .Wrap = wdFindContinue
   .Format = True
   .MatchCase = False
   .MatchWholeWord = True
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False
   .Replacement.ClearFormatting
   '''start the loop here so that
   '''you don't repeat all of the
   '''above commands
   For iCount = 0 To UBound(sFind)
       .Text = sFind(iCount)
       .Replacement.Text = sReplace(iCount)
       .Execute Replace:=wdReplaceAll
   Next iCount
End With
End Sub

HTH,

Dave

> Hello folks, first of all I would like to thank you for your effort and
> great help.
[quoted text clipped - 98 lines]
> right in deleting them?
> Thanks a lot for your help!
Mido - 13 Jan 2006 20:55 GMT
Hi Dave,
Thanks a lo for your help. As you might have found out that I am just
starting in this. I will try your solution and I really apprecciate it.
If you need any help with languages, do not hesitate!
> Hi Mido,
>
[quoted text clipped - 138 lines]
> > right in deleting them?
> > Thanks a lot for your help!
 
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.