0

I have a form that asks questions and you write an answer in a text box. When you hit 'next' or 'add' it should save the input and cycle like this until you hit save. For whatever reason, it only saves the one line and not the subsequent lines. Here's the code I have.

private void add_Click(object sender, EventArgs e)
    {

        Paragraph question = document.AddSection().AddParagraph();
        question.AppendText(questions.Text + "  " + answer.Text);

        document.SaveToFile(teamMember.Text + ".doc", FileFormat.Doc);

    }

    private void finish_Click(object sender, EventArgs e)
    {            
        document.SaveToFile(teamMember.Text + ".doc", FileFormat.Doc);
    }
TomG103
  • 311
  • 2
  • 26

1 Answers1

2

Not sure if I understand you correctly. From your code, I found that each time you were adding a new section, this will cause each of the question and the answer to be added to a new section of the document. If this is the problem, you can try the following code:

if (doc.Sections.Count == 0)
{
    //If the document is null, then add a new section
    Section sec = doc.AddSection();
    Paragraph para = sec.AddParagraph();
    para.AppendText("this is the first para");
}
else
{
    //Else add the text to the last paragraph of the document
    Paragraph paranew = doc.Sections[0].Paragraphs[doc.Sections[0].Paragraphs.Count - 1];
    paranew.AppendText(" " + "this is new para");
}

Hope it helps.

Bozhidar Stoyneff
  • 3,576
  • 1
  • 18
  • 28
Dheeraj Malik
  • 703
  • 1
  • 4
  • 8
  • This works. The only issue now is having the follow on text move to the next line. I've tried using AddSection(), but that doesn't seem to be the answer. – TomG103 Sep 25 '17 at 18:31
  • You can try appending a line break to the paragraph, like this: paragraph.AppendBreak(BreakType.LineBreak); – Dheeraj Malik Sep 26 '17 at 02:13
  • I decided to not use Spire. It ended up being more code than just using Interop.Word and this way I won't have the red text from their demo on all of my documents. – TomG103 Sep 28 '17 at 14:24