I always get an error message. "Object reference not set to an instance of an object"
Can anyone how to solve this?
Here is my code.
Many thanks for your replying.
private void CreateNewWordFile(string sourceFileName,string destinationFileName,string stratBookmarkName,string endBookmarkName)
{
// Declaring the object variables we will need later
object varFileName = fileName;
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
// Activate the document
varDoc.Activate();
string test = varDoc.Bookmarks.Count.ToString();
//BOOK MARK FOR START OF SELECTION
object oBookmarkStart = "bookmark1";
object oRngoBookMarkStart = varDoc.Bookmarks.get_Item(ref oBookmarkStart).Range.Start;
//BOOK MARK FOR END OF SELECTION
object oBookmarkEnd = "bookmark2";
object oRngoBookMarkEnd = varDoc.Bookmarks.get_Item(ref oBookmarkEnd).Range.Start;
//SETTING THE RANGE ON THE BOOKMARK BETWEEN TWO BOOKMARKS
Word.Range rngBKMarkSelection = varDoc.Range(ref oRngoBookMarkStart, ref oRngoBookMarkEnd);
//SELECTING THE TEXT
rngBKMarkSelection.Select();
rngBKMarkSelection.Copy();
CreateNewDocument(destinationFileName);
InsertTableIntoNewDocument(destinationFileName);
InsertDataIntoTable(destinationFileName, rngBKMarkSelection);
//CLOSING THE FILE
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
//QUITTING THE APPLICATION
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
}
private void InsertDataIntoTable(string destinationFileName, Word.Range rngBKMarkSelection)
{
try
{
// Declaring the object variables we will need later
object varFileName = destinationFileName;
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing);
// Activate the document
varDoc.Activate();
//error code here
object oUpperHeadingLevel = "1";
object oLowerHeadingLevel = "1";
object oTOCTableID = varDoc.Tables[1].ID.ToString();
varDoc.TablesOfContents.Add(rngBKMarkSelection, ref varTrueValue, ref oUpperHeadingLevel,
ref oLowerHeadingLevel, ref varMissing, ref oTOCTableID, ref varTrueValue,
ref varTrueValue, ref varMissing, ref varTrueValue, ref varTrueValue, ref varTrueValue);
//error code here
//THE LOCATION WHERE THE FILE NEEDS TO BE SAVED
object oSaveAsFile = destinationFileName;
varDoc.SaveAs(
ref oSaveAsFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
//CLOSING THE FILE
varDoc.Close(ref varFalseValue, ref varMissing, ref varMissing);
//QUITTING THE APPLICATION
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
}
catch (Exception varE)
{
MessageBox.Show("Error:\n" + varE.Message, "Error message");
}
}