selection.InsertBefore("Hello, John!"); //This Generates an error and needs to be fixed.
Sure it do raise an "null reference exception", because you declare the variable "selection", but never set it, so it' null. => failure.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How would I insert text into a Rich Text Box using the Word object model?
Here is a code snippet:
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
namespace Plant_A_Tree
{
public partial class Plant_Tree_Main_Interface : Form
{
Selection selection;
public Plant_Tree_Main_Interface()
{
InitializeComponent();
}
private void Plant_Tree_Main_Interface_Load(object sender, EventArgs e)
{
}
private void btnInsert_Text_Click(object sender, EventArgs e)
{
selection = new Selection(); // This may generate an error.
selection.InsertBefore("Hello, John!"); //This Generates an error and needs to be fixed.
}
}
}
selection.InsertBefore("Hello, John!"); //This Generates an error and needs to be fixed.
Sure it do raise an "null reference exception", because you declare the variable "selection", but never set it, so it' null. => failure.