Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Iterate all text in a word document and replace with the new ones

$
0
0

Hello,

I am trying to iterate through all texts in a document (paragraphs, tables, header-footer, table of contents, textboxs), and translate them and replace the texts with the translated texts. I don't want to search and replace.

I tried some codes, they worked for some documents but in a document like in the picture below, the texts are coming word by word, and word by word translation doesn't mean any thing, I have to find to whole paragraph and replace the whole text with the translated text without loosing the format. How can I write a code to fully cover all situations?

Thanks,

    Sub ReplaceText4()

        Using doc As WordprocessingDocument = WordprocessingDocument.Open(ListBox1.Items(0), True)
            Dim document = doc.MainDocumentPart.Document

            For Each Text2 In document.Descendants(Of Text)() ' <<< Here
                'If Text2.Text.Contains("text-to-replace") Then
                Text2.Text = Text2.Text.Replace(Text2.Text, "My New Text")
                'End If
            Next
        End Using

    End Sub


Viewing all articles
Browse latest Browse all 1288

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>