Quantcast
Channel: ProZ.com Translation Forums
Viewing all articles
Browse latest Browse all 3915

From Ms Word table to TMX file | @Hans

$
0
0
Forum: CAT Tools Technical Help
Topic: From Ms Word table to TMX file
Poster: Samuel Murray
Post title: @Hans

You may be able to write a single macro that does everything, but I would not be able to do that. If I were to do this, some steps would be manual.

The very first thing to do, is to use find/replace to remove or convert any characters that can break the XML. For example, replace & with &amp;, or replace < with &lt;.

Then, you have to add a column to the left, and then populate the column with numbers starting from 1. I have a macro that I can't remember where I got it from, that does that (you type "1" in the first cell and then run the macro, and it adds numbers to the cells below it):

Sub AddNumbersToTable()
Dim RowNum As Long
Dim ColNum As Long
Dim iStartNum As Integer
Dim J As Integer
If Selection.Information(wdWithInTable) Then
RowNum = Selection.Cells(1).RowIndex
ColNum = Selection.Cells(1).ColumnIndex
iStartNum = Val(Selection.Cells(1).Range.Text)
If iStartNum 0 Then
iStartNum = iStartNum + 1
For J = RowNum + 1 To ActiveDocument.Tables(1).Rows.Count
ActiveDocument.Tables(1).Cell(J, ColNum).Range.Text = iStartNum
iStartNum = iStartNum + 1
Next
Else
MsgBox "Cell doesn't contain a non-zero starting number."
Exit Sub
End If
Else
MsgBox "Not in table"
End If
End Sub

The next step is to convert the table to tabbed text. You can do that manually, but I use a macro for that:

Sub TablesConvert_to_tab()
For Each aTable In ActiveDocument.Tables
aTable.ConvertToText wdSeparateByTabs, True
Next aTable
End Sub

(this macro processes all tables in the file, though)

Then, you have to make sure that there is one blank line at the top of the text, and one blank line underneath the text.

And then you just record a find/replace macro:

Sub atable2tmx()
'
' atable2tmx Macro
' Macro recorded 8/20/2022
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l([0-9]@)^t"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^t"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = "^l"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
Selection.TypeText Text:="###"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "###"
.Replacement.Text = _
"^p^p"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdStory
Selection.TypeText Text:="###"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "###"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "^l"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

You can also create a macro that calls other macros, so you can automate everything in separate macros and then call them all from a single macro that you run once.

Viewing all articles
Browse latest Browse all 3915

Trending Articles