Home > Net >  how to insert a caption to a table in c# with office.interop.word library
how to insert a caption to a table in c# with office.interop.word library

Time:09-26

Hello to whoever that may read this Im working my way to become a C# developer. The first project assigned to me is writing a word document report automation (and extend it to the main app) i Find the office.interop.word library documentation from Microsoft docs rather intimidating.

so theres a CaptionLabels Interface and it has an Add method yet i dont know how to assign the created caption to the purposed Table and heres my code :

using Word = Microsoft.Office.Interop.Word;
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

            //Start Word and create a new document.
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            Object oTemplate = "C:\\Users\\a_shiGenerate word document report\\acidproTemplateDocx.docx";
            oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
            ref oMissing, ref oMissing);
          //some code in between here

            oWord.CaptionLabels.Add("some caption");
            object caption = oWord.CaptionLabels["some caption"];
            oWord.Selection.InsertCaption(ref caption, ref oMissing, ref oMissing, ref Tables[3], ref oMissing);
            /* in the line above Tables[3] is an unaccepted value but how to add this caption to this (any) particular table */
            oDoc.Tables.Add(wrdRng, 5, 10, ref oMissing, ref oMissing);
            oDoc.Paragraphs.SpaceBefore = 0.0f;
            oDoc.PageSetup.LeftMargin = 25.0f;
            oDoc.PageSetup.RightMargin = 29.0f;
            oDoc.Tables[3].Columns[2].Cells.PreferredWidth = 100;
            oDoc.Tables[3].Columns[1].Cells.PreferredWidth = 100;
            oDoc.Tables[3].Cell(1, 1).Range.Text = "hello";
            oDoc.Tables[3].Cell(1, 2).Range.Text = "arman";
            oDoc.Tables[3].Cell(1, 3).Range.Text = "this";
            oDoc.Tables[3].Cell(1, 4).Range.Text = "is  Image";
            oDoc.Tables[3].Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            oDoc.Tables[3].Cell(1, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

            oDoc.Tables[3].Cell(1, 1).Range.Font.Bold = 1;
            oDoc.Tables[3].Cell(1, 2).Range.Font.Bold = 1;
            oDoc.Tables[3].Cell(1, 3).Range.Font.Bold = 1;
            oDoc.Tables[3].Cell(1, 4).Range.Font.Bold = 1;
            oDoc.Tables[3].Range.Font.Size = 10;
            oDoc.Tables[3].Rows.Height = 2.0f;

so how can i assign captions to a certain table ?

any help/tips/howtos on working with this library would be much appreciated (the whole web has been searched and no useful reference found ).

CodePudding user response:

Well, still guessing, since you still did not define Tables… in my tests… that code will put the “Caption” as the FIRST paragraph in the document and the table as the last paragraph in the doc. However, the “caption” does appear to be assigned and is displayed as the first paragraph in the document. I am guessing you may want to move the caption down to the bottom of the doc where the table is.

If this is the case, then try the code below…

// set the range to the end of the document
wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();
wrdRng.InsertAfter("THE END.");
// insert the caption below “The End”…
oWord.CaptionLabels.Add("some caption");
object caption = oWord.CaptionLabels["some caption"];
wrdRng.InsertCaption(ref caption, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// insert the table after the caption…
wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.InsertParagraphAfter();
oDoc.Tables.Add(wrdRng, 5, 10, ref oMissing, ref oMissing);

Lastly, I highly recommend you put all the code that works with the Word Doc into a try/catch/finally statement to ensure all the COM objects your code creates get disposed of properly. Something like…

try {
   // all the code that works with the Word doc
}
catch (Exception ex) {
  // message box error ex.Message
}
finally {
   if (oDoc != null) {
      oDoc.Save();
      oDoc.Close();
      Marshal.ReleaseComObject(oDoc);
    }

    if (oWord != null) {
      oWord.Quit();
      Marshal.ReleaseComObject(oWord);
    }
}
  • Related