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.
- using System;
- using System.ComponentModel;
- using Microsoft.Office.Interop.Word;
- using Microsoft.Office.Core;
- using Microsoft.Office.Tools.Word;
- using Microsoft.Office.Tools;
-
- namespace Oficios
- {
- public class CCWordApp
- {
- private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic;
- private Microsoft.Office.Interop.Word.Document oDoc;
-
- public CCWordApp()
- {
-
- oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
- }
-
-
- public void Open(string strFileName)
- {
- object fileName = strFileName;
- object readOnly = false;
- object isVisible = true;
- object missing = System.Reflection.Missing.Value;
-
- oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
- ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
- ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
-
- oDoc.Activate();
- }
-
-
- public void Open()
- {
- object missing = System.Reflection.Missing.Value;
- oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
-
- oDoc.Activate();
- }
-
- public void Quit()
- {
- object missing = System.Reflection.Missing.Value;
- oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
- }
-
- public void Save()
- {
- oDoc.Save();
- }
-
- public void SaveAs(string strFileName)
- {
- object missing = System.Reflection.Missing.Value;
- object fileName = strFileName;
-
- oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
- ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
- }
-
-
- public void SaveAsHtml(string strFileName)
- {
- object missing = System.Reflection.Missing.Value;
- object fileName = strFileName;
- object Format = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
- oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
- ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
- }
-
- public void InsertText(string strText)
- {
- oWordApplic.Selection.TypeText(strText);
- }
-
- public void InsertImage(string nameImg)
- {
-
- }
-
- public void InsertLineBreak()
- {
- oWordApplic.Selection.TypeParagraph();
- }
-
- public void InsertLineBreak(int nline)
- {
- for (int i = 0; i < nline; i++)
- oWordApplic.Selection.TypeParagraph();
- }
-
-
- public void SetAlignment(string strType)
- {
- switch (strType)
- {
- case "Center":
- oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
- break;
- case "Left":
- oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
- break;
- case "Right":
- oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
- break;
- case "Justify":
- oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
- break;
- }
-
- }
-
-
-
-
- public void SetFont(string strType)
- {
- switch (strType)
- {
- case "Bold":
- oWordApplic.Selection.Font.Bold = 1;
- break;
- case "Italic":
- oWordApplic.Selection.Font.Italic = 1;
- break;
- case "Underlined":
- oWordApplic.Selection.Font.Subscript = 0;
- break;
- }
-
- }
-
-
- public void SetFont()
- {
- oWordApplic.Selection.Font.Bold = 0;
- oWordApplic.Selection.Font.Italic = 0;
- oWordApplic.Selection.Font.Subscript = 0;
-
- }
-
- public void SetFontName(string strType)
- {
- oWordApplic.Selection.Font.Name = strType;
-
- }
-
- public void SetFontSize(int nSize)
- {
- oWordApplic.Selection.Font.Size = nSize;
-
- }
-
- public void InsertPagebreak()
- {
-
- object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
- oWordApplic.Selection.InsertBreak(ref pBreak);
- }
-
-
-
- public void GotoBookMark(string strBookMarkName)
- {
-
-
- object missing = System.Reflection.Missing.Value;
-
-
-
- object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
- object NameBookMark = strBookMarkName;
- oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
-
-
-
-
- }
-
- public void GotoFooter(string footer, string header, string strBookMarkName)
- {
-
-
- object missing = System.Reflection.Missing.Value;
-
-
-
- object Bookmark2 = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
- object NameBookMark = strBookMarkName;
-
-
-
-
-
-
-
- Microsoft.Office.Interop.Word.Document dc = oWordApplic.ActiveDocument;
- foreach (Microsoft.Office.Interop.Word.Section wordSection in dc.Sections)
- {
-
- object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
- wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = header;
- wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = footer;
- wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = header;
- wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = footer;
-
-
-
- }
- }
-
- public void Try(string str)
- {
-
- object Name = str;
- }
-
- public void GoToTheEnd()
- {
-
- object missing = System.Reflection.Missing.Value;
- object unit;
- unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
- oWordApplic.Selection.EndKey(ref unit, ref missing);
-
- }
- public void GoToTheBeginning()
- {
-
- object missing = System.Reflection.Missing.Value;
- object unit;
- unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
- oWordApplic.Selection.HomeKey(ref unit, ref missing);
-
- }
-
- public void GoToTheTable(int ntable)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- object missing = System.Reflection.Missing.Value;
- object what;
- what = Microsoft.Office.Interop.Word.WdUnits.wdTable;
- object which;
- which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
- object count;
- count = 1;
- oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
- oWordApplic.Selection.Find.ClearFormatting();
-
- oWordApplic.Selection.Text = "";
-
-
- }
-
- public void GoToRightCell()
- {
-
-
- object missing = System.Reflection.Missing.Value;
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
- oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
- }
-
- public void GoToLeftCell()
- {
-
-
- object missing = System.Reflection.Missing.Value;
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
- oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
- }
-
- public void GoToDownCell()
- {
-
-
- object missing = System.Reflection.Missing.Value;
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
- oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
- }
-
- public void GoToUpCell()
- {
-
-
- object missing = System.Reflection.Missing.Value;
- object direction;
- direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
- oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
- }
- }
- }
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.