Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS-Word-Makro to translate the text in various textboxes

Status
Not open for further replies.

Allmend

Programmer
Feb 8, 2007
3
DE
I need a MS-Word-Makro to translate the text in various textboxes of a Word-Document from one language into another.

In the first Word-Document there are many textboxes with English words or sentences.

In the second Word-Document there is a dictionary, a spreadsheet with two columns.
Left column -Englsich texts, right column -German translations of the English texts.

The program has to compare the content of each textbox with the left column of the dictionary.

If the content of the textbox is found in the dictionary, the program has to replace the English text of the textbox with the according German Translation.

Thanks in advance for any help!
 
A literal word-for-word substitution is likely to simply result in incomprehensible gibberish as a result.

Just look at what Babelfish or other translations algorithms that are much more sophisticated than word-for-word substitution do.

Having said that, word-for-word substitution is a pretty simple thing to implement. Were you having difficulty with something specific that you have tried so far, or are you simply hoping that someone will do your work (homework?) for you?
 




Bottom Line:

Post the code that you are having trouble with, along with a CLEAR, CONCISE, COMPLETE explaination of the specific problem.

Skip,

[glasses] When a diminutive clarvoyant had disappeared from detention, headlines read...
Small Medium at Large[tongue]
 
IF the table contains sentences ONLY, then such a substitution MIGHT produce something meaningful.

If you also want to translate single words - don't!
The only way this would function, would be if you had a complete glossary, i.e. words with additional context information, AND the same contextual information in the textbox you wish to translate, which you don't, right?

Just as an example:

Auto-translate the word "disk" without extra info. You'll have the choice between
Diskette
Scheibe
Drehscheibe
Drehteller
Nummernscheibe
Lamelle
Teller
...
...
...

Which do you choose? And how do you determine which is the right to choose?

So let's assume you restrict yourself to entire sentences. In that case, you might want to pick a slightly different path:

Not a Word-lookup-table, but an Access database table.
Then you simply query the sentence
Code:
Dim con as ADODB.Connection, shp as Shape
Dim rs
...

If shp.Type = msoTextBox Then
   myText=shp.TextFrame.TextRange.Text
   [b]Set rs= con.execute("SELECT [DE] FROM [translations] WHERE [EN]='" & myText & "';")[/b]
   If not rs.EOF Then 'a translation exists
      shp.TextFrame.TextRange.Text=rs!DE
   End if
End If
...

Word tables are really good resource killers. Avoid extensive Word table "querying" where possible.
:)

Hope this helps.

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Hi to all and thanks for your help.

I already translated a huge Word-Document with many thousands of English textboxes into German.

I extracted the Text from the Word-Document in saving the Word-File as a plain-Text-file. Then I translated every Text-segment (sentences or words) into German.

I had to extract the text from the Word-Document because my CAT-Program (WordFast) can only work with a plain text document.

A CAT-Program divides the source-text into segments (usually sentences), so you can translate separate sentences. The CAT-Program then creates a so called Translation Memory, which is a Word-Table I mentioned.

Now I have a table with many thousands of English text-segments (left column) and according German translations (right column).

I have to replace in the original Word-Document the English texts in the text boxes through (already translated) German texts, that is all.

I know how to write such a Program in VB for Excel (using the command Var=cells(row,column).value ), but I have no idea how to do this in VB for Word.

How can I scan for the values in a MS-Word table, compare the value of the Var with the value in different textboxes in the source Word-Document and replace the text with the pre-translated German text if the according English text is found?

Thanks in advance.
 
You might be doing double-work here.
You are working with Wordfast?
If I'm not mistaken, WF can autotranslate 100%-matches just like Trados, can't it?
If so: why put your translation memory in a word table first then self-auto-translate using the word table instead of using WF's features?

What you are doing here is going around WF. You only used it to create the translation table.

Anyway, this is how you could do this:
(Given your translation table is the first (or only) table in your document)
Code:
Sub TranslateIt()
Dim transtable As Table, i As Integer, src As String, trg As String
Dim shp As Shape

Set transtable = ActiveDocument.Tables(1)

For i = 1 To transtable.Rows.Count 'skip first row if headers, else start at 0
    src = transtable.Cell(i, 1).Range.Text
    trg = transtable.Cell(i, 2).Range.Text
    src = Left(src, Len(src) - 2) 'cut off cell end marker
    trg = Left(trg, Len(trg) - 2) 'cut off cell end marker
    For Each shp In ActiveDocument.Shapes
        If shp.Type = msoTextBox Then
            With shp.TextFrame.TextRange.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = src
                .Replacement.Text = trg
                .MatchCase = True
                .MatchWholeWord = True
                .MatchWildcards = False
                .Format = False
                .Execute Replace:=wdReplaceAll
            End With
        End If
    Next shp
Next i

End Sub

Still i believe, if you are already using a CAT tool, you ought to actually USE it.
;-)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
MakeItSo (Programmer) wrote:

>If I'm not mistaken, WF can autotranslate 100%-matches just like Trados, can't it?

Well, that works with "normal" texts. Unfortunately, if you try this with TextBoxes, the program doesn't work properly. It is too slow and hangs up.

I do not have any experience in VB for Word, but I tried to do this programming in Excel, and it works.

Here is a simple Sub that replaces 5 texts in textboxes in Source.xls with the German texts in the table TM.xls:

--------

Sub TextReplace()

For N = 1 To 5
Windows("TM.xls").Activate

ENTxt = Cells(N, 1).Value
DETxt = Cells(N, 2).Value

Windows("Source.xls").Activate

For M = 1 To 5

TxBxNr = "Text Box " & M

ActiveSheet.Shapes(TxBxNr).Select

ENSegm = Selection.Characters.Text
If ENTxt = ENSegm Then

Selection.Characters.Text = DETxt
End If

Next M
Next N

End Sub
----------

I need a similar simple Sub for MS Word.

Thaks MakeItSo for your listing, I will try it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top