Monday, May 17, 2010

Word Macro to convert Trados Multiterm XML to CSV

First rename the .xml to .txt. Open the text file in Word. Alt + F11 and paste the following code:

Sub trados_xml_to_csv_with_tabs()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "\*\<\/term\>"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindAll
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Font.Bold = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAll
.Format = True
.MatchCase = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindAll
.Format = True
.MatchCase = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "
"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAll
.Format = True
.MatchCase = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAll
.Format = True
.MatchCase = False
.MatchWildcards = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.WholeStory
Selection.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=2, _
NumRows:=551, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
.Style = "Table Grid"
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
True
End Sub