如何在文档末尾插入文本 [英] How to Insert text in the end of the document

查看:255
本文介绍了如何在文档末尾插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在word文档中插入文本.但是,每当我执行我的代码时,总是在开始时添加在文本框中输入的文本.我无法在文档末尾插入文本.我也无法使用Range来解决此问题.

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 insert the text in the end of the document. I am unable to fix this using Range also.

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.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");
        }
    }

推荐答案

以下代码中的第1行和第2行对我有帮助. 以下代码可以正常工作.

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");
    }
}

这篇关于如何在文档末尾插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆