Jump to content

Hello,

 

The code below should create a word document and then generate a table of contents. it should only have one item called "INTRODUCTION", but i keep getting an error saying "an unhandled exception of type "system.runtime.interopservicesCOMException" Occured.

 

Could you please tell me what is wrong :(

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using Word = Microsoft.Office.Interop.Word;using System.Reflection;namespace WindowsFormsApplication27{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            object oMissing = System.Reflection.Missing.Value;            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */            //Start Word and create a new document.            Word.Application oWord = new Word.Application();            Word.Document oDoc = new Word.Document();            oWord.Visible = true;            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,                ref oMissing, ref oMissing);            object oTrue = true;            object oFalce = false;            object styleHeading2 = "Heading 2";            object styleHeading3 = "Heading 3";            oWord.Selection.Range.set_Style(ref styleHeading2);            oWord.Selection.Range.set_Style(ref styleHeading3);            oWord.Selection.Paragraphs.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel2;            oWord.Selection.Paragraphs.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevel3;            oWord.Selection.Paragraphs.OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText;            object oBookMarkTOC = "Bookmark_TOC";            Word.Range rngTOC = oDoc.Bookmarks.get_Item(ref oBookMarkTOC).Range;  //<=========================================== The error happens on this line            rngTOC.Select();            object oUpperHeadingLevel = "1";            object oLowerHeadingLevel = "3";            object oTOCTableID = "TableOfContents";            oDoc.TablesOfContents.Add(rngTOC, ref oTrue, ref oUpperHeadingLevel, ref oLowerHeadingLevel, ref oMissing, ref oTOCTableID, ref oTrue, ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);            //******************TABLE OF CONENTES END**************            Word.Paragraph oIntro;            oIntro = oDoc.Content.Paragraphs.Add(ref oMissing);            oIntro.Range.Text = "INTRODUCTION";            oIntro.Range.set_Style(ref styleHeading2);            oIntro.Range.Font.Bold = 1;            oIntro.Range.Font.Name = "Arial";            oIntro.Range.Font.Size = 12;            oIntro.Format.SpaceAfter = 15;                oIntro.Range.InsertParagraphAfter();            Word.Paragraph oPara1;            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);            oPara1.Range.Text = "This is the paragraph after the Introduction Title.";            oPara1.Range.set_Style(ref styleHeading2);            oPara1.Range.Font.Bold = 0;            oPara1.Range.Font.Name = "Arial";            oPara1.Range.Font.Size = 12;            oPara1.Format.SpaceAfter = 15;                oPara1.Range.InsertParagraphAfter();            //Update table of contents            oDoc.TablesOfContents[1].Update();        }    }}
Link to comment
https://linustechtips.com/topic/220479-help-with-visual-c-error/
Share on other sites

Link to post
Share on other sites

object oBookMarkTOC = "Bookmark_TOC";Word.Range rngTOC = oDoc.Bookmarks.get_Item(ref oBookMarkTOC).Range;

It probably throws an exception because the object is not the excepted value. You are trying to get a Range of just one object which cannot be done.

// TODO: Update signature to include PC buid.

Link to comment
https://linustechtips.com/topic/220479-help-with-visual-c-error/#findComment-3025062
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×