none
Office interop updating bookmarks RRS feed

  • Question

  • I am trying to update bookmarks with some text. Booksmarks.Exists("bookmarkname") always returns false though both the document and bookmark exists.

    Any help would be appreciated.

    • Moved by CoolDadTx Wednesday, November 5, 2014 2:42 PM Office related
    Wednesday, November 5, 2014 1:17 PM

Answers

  • Hi Raj Krish,

    Based on the description, you can't find the specific bookmark based on its name. Which version of Word application are you using? 

    As far as I test, the code works for me well. I suggest that you check whether the bookmark to ensure it exists with correct name like figure below(Insert->Bookmark):

    And here is the test code:

     public void Main()
            {
                Microsoft.Office.Interop.Word.Application wordApp=new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document wordDoc;
    
                object fileName = @"C:\Users\UserName\Desktop\Test.docx";
                object readOnly = false;
                object isVisible = true;
    
                object oMissing = System.Reflection.Missing.Value;
    
                wordDoc=wordApp.Documents.Open(fileName, ref oMissing, readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                wordDoc.Activate();
    
                if (wordDoc.Bookmarks.Exists("EmployeeName"))
                {
                    object oBookmark = "EmployeeName";
                    wordDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = "me";
                }
                wordApp.Documents.Save(ref oMissing, ref oMissing);
                wordApp.Application.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 3:11 AM
    Moderator

All replies

  • I have tried versions office interop versions 12,14 and 15.
    Wednesday, November 5, 2014 1:34 PM
  • Are you using something like that?:

    Microsoft.Office.Interop.Word.document doc;
    doc.Bookmarks.Exists(bookmarkName) 


    Wednesday, November 5, 2014 1:44 PM
  • Please check the document if it is active or not ? 
    Wednesday, November 5, 2014 1:58 PM
  • Please check the document if it is active or not ? 

    Great qhdoanh.That's important too!

    If ActiveDocument.Bookmarks.Exists("start") = True Then
        ActiveDocument.Bookmarks("start").Delete
    End If
    

    http://msdn.microsoft.com/en-us/library/office/aa171995(v=office.11).aspx

    Wednesday, November 5, 2014 2:25 PM
  • Microsoft.Office.Interop.Word.Application wordApp
    Microsoft.Office.Interop.Word.Document wordDoc

    object fileName = "E:\\Employee.docx";
    object readOnly = false;
    object isVisible = true;

    object oMissing = System.Reflection.Missing.Value;

    wordApp.Documents.Open(fileName, ref oMissing, readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    wordDoc.Activate();

                    if (wordDoc.Bookmarks.Exists("EmployeeName"))
                    {
                        object oBookmark = "EmployeeName";
                        wordDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = "me";
                    }

                    wordApp.Documents.Save(ref oMissing, ref oMissing);
                    wordApp.Application.Quit(ref oMissing, ref oMissing, ref oMissing);

    Thursday, November 6, 2014 5:45 AM
  • Hi Raj Krish,

    Based on the description, you can't find the specific bookmark based on its name. Which version of Word application are you using? 

    As far as I test, the code works for me well. I suggest that you check whether the bookmark to ensure it exists with correct name like figure below(Insert->Bookmark):

    And here is the test code:

     public void Main()
            {
                Microsoft.Office.Interop.Word.Application wordApp=new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document wordDoc;
    
                object fileName = @"C:\Users\UserName\Desktop\Test.docx";
                object readOnly = false;
                object isVisible = true;
    
                object oMissing = System.Reflection.Missing.Value;
    
                wordDoc=wordApp.Documents.Open(fileName, ref oMissing, readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                wordDoc.Activate();
    
                if (wordDoc.Bookmarks.Exists("EmployeeName"))
                {
                    object oBookmark = "EmployeeName";
                    wordDoc.Bookmarks.get_Item(ref oBookmark).Range.Text = "me";
                }
                wordApp.Documents.Save(ref oMissing, ref oMissing);
                wordApp.Application.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 11, 2014 3:11 AM
    Moderator