Help word automation with c# urgent please

Jul 21 2009 2:22 PM
Hello I'm trying to create a doc from a template I can access bookmarks an everything is ok except when there is a bookmark inside a textbox in the template, I can't acces that nor the bookmarks in the header and footer from c# the reason that a bookmark is in a textbox is because I need that text to not move from the page just stay there always, anyway here is the class I'm Using to access word.
  1. using System;  
  2. using System.ComponentModel;  
  3. using Microsoft.Office.Interop.Word;  
  4. using Microsoft.Office.Core;  
  5. using Microsoft.Office.Tools.Word;  
  6. using Microsoft.Office.Tools;  
  7.   
  8. namespace Oficios  
  9. {  
  10.     public class CCWordApp  
  11.     {  
  12.         private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic; // a reference to Word application  
  13.         private Microsoft.Office.Interop.Word.Document oDoc;                    // a reference to the document  
  14.   
  15.         public CCWordApp()  
  16.         {  
  17.             // activate the interface with the COM object of Microsoft Word  
  18.             oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();  
  19.         }  
  20.   
  21.         // Open a file (the file must exists) and activate it  
  22.         public void Open(string strFileName)  
  23.         {  
  24.             object fileName = strFileName;  
  25.             object readOnly = false;  
  26.             object isVisible = true;  
  27.             object missing = System.Reflection.Missing.Value;  
  28.   
  29.             oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,  
  30.                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,  
  31.                 ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);  
  32.   
  33.             oDoc.Activate();  
  34.         }  
  35.   
  36.         // Open a new document  
  37.         public void Open()  
  38.         {  
  39.             object missing = System.Reflection.Missing.Value;  
  40.             oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);  
  41.   
  42.             oDoc.Activate();  
  43.         }  
  44.   
  45.         public void Quit()  
  46.         {  
  47.             object missing = System.Reflection.Missing.Value;  
  48.             oWordApplic.Application.Quit(ref missing, ref missing, ref missing);  
  49.         }  
  50.   
  51.         public void Save()  
  52.         {  
  53.             oDoc.Save();  
  54.         }  
  55.   
  56.         public void SaveAs(string strFileName)  
  57.         {  
  58.             object missing = System.Reflection.Missing.Value;  
  59.             object fileName = strFileName;  
  60.   
  61.             oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,  
  62.                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);  
  63.         }  
  64.   
  65.         // Save the document in HTML format  
  66.         public void SaveAsHtml(string strFileName)  
  67.         {  
  68.             object missing = System.Reflection.Missing.Value;  
  69.             object fileName = strFileName;  
  70.             object Format = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;  
  71.             oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,  
  72.                 ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);  
  73.         }  
  74.   
  75.         public void InsertText(string strText)  
  76.         {  
  77.             oWordApplic.Selection.TypeText(strText);  
  78.         }  
  79.   
  80.         public void InsertImage(string nameImg)  
  81.         {  
  82.   
  83.         }  
  84.   
  85.         public void InsertLineBreak()  
  86.         {  
  87.             oWordApplic.Selection.TypeParagraph();  
  88.         }  
  89.   
  90.         public void InsertLineBreak(int nline)  
  91.         {  
  92.             for (int i = 0; i < nline; i++)  
  93.                 oWordApplic.Selection.TypeParagraph();  
  94.         }  
  95.   
  96.         // Change the paragraph alignement  
  97.         public void SetAlignment(string strType)  
  98.         {  
  99.             switch (strType)  
  100.             {  
  101.                 case "Center":  
  102.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;  
  103.                     break;  
  104.                 case "Left":  
  105.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;  
  106.                     break;  
  107.                 case "Right":  
  108.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;  
  109.                     break;  
  110.                 case "Justify":  
  111.                     oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;  
  112.                     break;  
  113.             }  
  114.   
  115.         }  
  116.   
  117.   
  118.         // if you use thif function to change the font you should call it again with   
  119.         // no parameter in order to set the font without a particular format  
  120.         public void SetFont(string strType)  
  121.         {  
  122.             switch (strType)  
  123.             {  
  124.                 case "Bold":  
  125.                     oWordApplic.Selection.Font.Bold = 1;  
  126.                     break;  
  127.                 case "Italic":  
  128.                     oWordApplic.Selection.Font.Italic = 1;  
  129.                     break;  
  130.                 case "Underlined":  
  131.                     oWordApplic.Selection.Font.Subscript = 0;  
  132.                     break;  
  133.             }  
  134.   
  135.         }  
  136.   
  137.         // disable all the style   
  138.         public void SetFont()  
  139.         {  
  140.             oWordApplic.Selection.Font.Bold = 0;  
  141.             oWordApplic.Selection.Font.Italic = 0;  
  142.             oWordApplic.Selection.Font.Subscript = 0;  
  143.   
  144.         }  
  145.   
  146.         public void SetFontName(string strType)  
  147.         {  
  148.             oWordApplic.Selection.Font.Name = strType;  
  149.   
  150.         }  
  151.   
  152.         public void SetFontSize(int nSize)  
  153.         {  
  154.             oWordApplic.Selection.Font.Size = nSize;  
  155.   
  156.         }  
  157.   
  158.         public void InsertPagebreak()  
  159.         {  
  160.             // VB : Selection.InsertBreak Type:=wdPageBreak  
  161.             object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;  
  162.             oWordApplic.Selection.InsertBreak(ref pBreak);  
  163.         }  
  164.   
  165.         // Go to a predefined bookmark, if the bookmark doesn't exists the application will raise an error  
  166.   
  167.         public void GotoBookMark(string strBookMarkName)  
  168.         {  
  169.             // VB :  Selection.GoTo What:=wdGoToBookmark, Name:="nome"  
  170.             //if( oWordApplic.Selection.Bookmarks.Exists(strBookMarkName)){  
  171.             object missing = System.Reflection.Missing.Value;  
  172.   
  173.   
  174.   
  175.             object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;  
  176.             object NameBookMark = strBookMarkName;  
  177.             oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);  
  178.             //oWordApplic.ActiveDocument.Bookmarks.Exists(strBookMarkName);  
  179.             //            Range cosa = oWordApplic.ActiveDocument.Bookmarks.get_Item(strBookMarkName).Range.Select();  
  180.   
  181.             // }  
  182.         }  
  183.   
  184.         public void GotoFooter(string footer, string header, string strBookMarkName)  
  185.         {  
  186.             // VB :  Selection.GoTo What:=wdGoToBookmark, Name:="nome"  
  187.             //if( oWordApplic.Selection.Bookmarks.Exists(strBookMarkName)){  
  188.             object missing = System.Reflection.Missing.Value;  
  189.   
  190.   
  191.   
  192.             object Bookmark2 = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;  
  193.             object NameBookMark = strBookMarkName;  
  194.             // oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);  
  195.             //oWordApplic.ActiveDocument.Bookmarks.Exists(strBookMarkName);  
  196.             //            Range cosa = oWordApplic.ActiveDocument.Bookmarks.get_Item(strBookMarkName).Range.Select();  
  197.   
  198.             // }  
  199.   
  200.   
  201.             Microsoft.Office.Interop.Word.Document dc = oWordApplic.ActiveDocument;  
  202.             foreach (Microsoft.Office.Interop.Word.Section wordSection in dc.Sections)  
  203.             {  
  204.   
  205.                 object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;  
  206.                 wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = header;  
  207.                 wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = footer;  
  208.                 wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = header;  
  209.                 wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = footer;  
  210.   
  211.                 //wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Bookmarks.get_Item(ref headersupder).Range.Text= header;  
  212.                 //wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Bookmarks.get_Item(ref headersupder).Range.Text = header;  
  213.             }  
  214.         }  
  215.   
  216.         public void Try(string str)  
  217.         {  
  218.   
  219.             object Name = str;  
  220.         }  
  221.   
  222.         public void GoToTheEnd()  
  223.         {  
  224.             // VB :  Selection.EndKey Unit:=wdStory  
  225.             object missing = System.Reflection.Missing.Value;  
  226.             object unit;  
  227.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  228.             oWordApplic.Selection.EndKey(ref unit, ref missing);  
  229.   
  230.         }  
  231.         public void GoToTheBeginning()  
  232.         {  
  233.             // VB : Selection.HomeKey Unit:=wdStory  
  234.             object missing = System.Reflection.Missing.Value;  
  235.             object unit;  
  236.             unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;  
  237.             oWordApplic.Selection.HomeKey(ref unit, ref missing);  
  238.   
  239.         }  
  240.   
  241.         public void GoToTheTable(int ntable)  
  242.         {  
  243.             //  Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""  
  244.             //    Selection.Find.ClearFormatting  
  245.             //    With Selection.Find  
  246.             //        .Text = ""  
  247.             //        .Replacement.Text = ""  
  248.             //        .Forward = True  
  249.             //        .Wrap = wdFindContinue  
  250.             //        .Format = False  
  251.             //        .MatchCase = False  
  252.             //        .MatchWholeWord = False  
  253.             //        .MatchWildcards = False  
  254.             //        .MatchSoundsLike = False  
  255.             //        .MatchAllWordForms = False  
  256.             //    End With  
  257.   
  258.             object missing = System.Reflection.Missing.Value;  
  259.             object what;  
  260.             what = Microsoft.Office.Interop.Word.WdUnits.wdTable;  
  261.             object which;  
  262.             which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;  
  263.             object count;  
  264.             count = 1;  
  265.             oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);  
  266.             oWordApplic.Selection.Find.ClearFormatting();  
  267.   
  268.             oWordApplic.Selection.Text = "";  
  269.   
  270.   
  271.         }  
  272.   
  273.         public void GoToRightCell()  
  274.         {  
  275.             // Selection.MoveRight Unit:=wdCell  
  276.   
  277.             object missing = System.Reflection.Missing.Value;  
  278.             object direction;  
  279.             direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;  
  280.             oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);  
  281.         }  
  282.   
  283.         public void GoToLeftCell()  
  284.         {  
  285.             // Selection.MoveRight Unit:=wdCell  
  286.   
  287.             object missing = System.Reflection.Missing.Value;  
  288.             object direction;  
  289.             direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;  
  290.             oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);  
  291.         }  
  292.   
  293.         public void GoToDownCell()  
  294.         {  
  295.             // Selection.MoveRight Unit:=wdCell  
  296.   
  297.             object missing = System.Reflection.Missing.Value;  
  298.             object direction;  
  299.             direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;  
  300.             oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);  
  301.         }  
  302.   
  303.         public void GoToUpCell()  
  304.         {  
  305.             // Selection.MoveRight Unit:=wdCell  
  306.   
  307.             object missing = System.Reflection.Missing.Value;  
  308.             object direction;  
  309.             direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;  
  310.             oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);  
  311.         }  
  312.     }  
  313. }  
I can go to bookmarks in tables just ok but when I call the method doc.GoToBookmark(smth) where smth is insidea the header, footer or the textbox vs2008 throws an exception that it couldn't find the bookmark.

Any ideas what can I do.

Thanks a lot.


Answers (3)