Translation Macro

M

Mido

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!
 
D

Dave Lett

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
 
M

Mido

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!
 

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