我正在尝试将文本插入到 Word 文档中。但是每当我执行我的代码时,我在文本框中输入的文本总是在开头添加。我无法在文档末尾插入文本。我Range也无法解决这个问题。
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");
}
}
Run Code Online (Sandbox Code Playgroud)