1

my application collects data, transforms it and then saves everything to a Word-document. This is the code which gets the text and saves it to the word document:

     Dim DOCXPath as string="C:\\Test"
     SelectedIndex="Atlantic City"
     Using wordDocument As WordprocessingDocument = WordprocessingDocument.Create(DOCXPath, WordprocessingDocumentType.Document)
        ' Add a main document part.
        Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
        mainPart.Document = New Document()
        Dim body As Body = mainPart.Document.AppendChild(New Body())
        Dim para As Paragraph = body.AppendChild(New Paragraph())
        Dim run As Run = para.AppendChild(New Run())

        Dim justification As Justification = New Justification()
        justification.Val = JustificationValues.Center
        Dim paragraphProperties1 As ParagraphProperties = New ParagraphProperties
        paragraphProperties1.Append(justification)
        Dim paragraph As Paragraph = New Paragraph
        paragraph.ParagraphProperties = paragraphProperties1

        ''Loop through the rows
        For Each ActualRow As DataRow In DataTablePublication.Rows

            'Find the rows matching the Index
            If ActualRow.Item(RefDocPubIndex).ToString = SelectedIndex Then
                'Write bold
                Dim runProperties As RunProperties = run.AppendChild(New RunProperties(New Bold()))
                run.AppendChild(New Text(HeaderCreator(ActualRow)))
                run.AppendChild(New Paragraph)

                'Write bold
                run.AppendChild(New RunProperties(New Bold()))
                run.AppendChild(New Text(TextCreator1(ActualRow)))
                run.AppendChild(New Paragraph)

                run.AppendChild(New Text(TextCreator2(ActualRow)))
                run.AppendChild(New Paragraph)
            End If
        Next
        wordDocument.Close()
    End Using

The function TextCreator2 returns for example the following string:

"Atlantic City had 1 million inhabitants" + Environment.NewLine + "That is wrong!"

The part about the new line is the part which gets somehow "deleted" during the writing to the .docx-file. Can anyone explain me why and how I can solve this?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Cliff
  • 61
  • 1
  • 10
  • 1
    Suggest you look at https://stackoverflow.com/questions/2871887/inserting-newlines-in-word-using-openxml (AFAICS you are just inserted a newline character into a Word "run" 0 that won't work, at most you will see white space when you open the document. You need to insert the OOXML markup for a "break". – jonsson Mar 07 '23 at 14:17
  • @jonsson: Your link suggestion is a good duplicate: [Inserting newlines in Word using OpenXML](https://stackoverflow.com/questions/2871887/inserting-newlines-in-word-using-openxml) – kjhughes Mar 07 '23 at 14:37
  • For the solution given by the linked topic I have a "problem". My function returns the whole phrase. Where statement is instantly followed by the correct answer. So I tried inserting the `` tag in every possible manner (``, ``, `< w:br />`) but nothing worked. Just to be assured: my OutputText would be: `OutputText="Atlantic City is flat.That is not true.` or do I need to write something else? So that the ``-tag is not directly in the text? – Cliff Mar 07 '23 at 14:43
  • 1
    The thing is that you can't just put a in the middle of a piece of text. You have to terminate the text first and restart it after, as per the suggestion by BrainSlugs83. My guess is that that suggestion is a bit of a kludge abd so may not work in all circumstances. – jonsson Mar 07 '23 at 15:29

0 Answers0