How to Insert text in the end of the document

前端 未结 2 859
遇见更好的自我
遇见更好的自我 2020-12-17 04:42

I am trying to insert text into the word document. But whenever i execute my code the text enter by me in the text box is always added in the beginning. I am unable to inser

相关标签:
2条回答
  • 2020-12-17 04:54

    You have to use append method to add the text within the same line rather than entering in next line Ex:

    File.AppendAllText(@"c:\path\file.txt", textBox1.Text);
    

    This might help

    0 讨论(0)
  • 2020-12-17 04:55

    Line 1 and Line 2 in the below code helps me. The following code works fine.

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (textBox1.Text != "")
            {
                Microsoft.Office.Interop.Word._Application oWord;
                object oMissing = Type.Missing;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = false;
                oWord.Documents.Open(filePath);
                oWord.ActiveDocument.Characters.Last.Select();  // Line 1
                oWord.Selection.Collapse();                     // Line 2
                oWord.Selection.TypeText(textBox1.Text);
                oWord.ActiveDocument.Save();
                oWord.Quit();
                MessageBox.Show("The text is inserted.");
                textBox1.Text = "";
            }
            else
            {
                MessageBox.Show("Please give some text in the text box");
            }
        }
        catch(Exception)
        {
            MessageBox.Show("Please right click on the window and provide the path");
        }
    }
    
    0 讨论(0)
提交回复
热议问题