none
write-protection of bookmarks in Word RRS feed

  • Question

  • Hi,
    I'm writing a Word Add-In which creates bookmarks and fills in some text. Is there any way to prevent changes of these bookmarks by the user? Some kind  of read-only flag?

    Thansk & best regards
      Volker
    Friday, August 24, 2007 2:10 PM

Answers

  • Hi Volker,

     

    There is not a read-only flag of the bookmark. I researched a lot. It seems there was already some discussion about this on many forums and google groups. Ideas from them are dividing your document to sections. Put your bookmarks to sections which will be protected. Since your bookmarks are in the certain section which is protected, user cannot modify them. More information you can get on the following two links:

    http://wordtips.vitalnews.com/Pages/T1716_Protecting_Bookmarks.html

    http://msdn2.microsoft.com/en-us/library/ms178793(VS.80).aspx

    Also, I find another trick can prevent your bookmarks from being modified. In Application_WindowSelectionChange event, you can have a test if user’s selection is in the bookmark we want to protect. If yes, we protect the whole document, and if no, we unprotect the whole document. Sample I write are as follows:

    Code Snippet

    private Word.Bookmark bm = null;

     

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                this.Application.WindowSelectionChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(Application_WindowSelectionChange);                  

     

                Word.Range rg = this.Application.ActiveDocument.Range(ref missing, ref missing);

                rg.Text = "This is the bookmark where you cannot modify";

                object range = rg;

                bm = this.Application.ActiveDocument.Bookmarks.Add("test", ref range);

     

                this.Application.ActiveDocument.Content.InsertAfter("This is the area you can modify");

            }

     

            void Application_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)

            {

                int selstart = Sel.Start;

                int selend = Sel.End;

                int bmstart = bm.Range.Start;

                int bmend = bm.Range.End;

     

                if(!(selstart>bmend || selend< bmstart))

                {

                    this.Application.ActiveDocument.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, ref missing, ref missing, ref missing, ref missing);

                }

                else

                {

                    this.Application.ActiveDocument.Unprotect(ref missing);

                }

            }

     

     

    Hope this can help! J

     

    Thanks

    Ji

     

    Thursday, August 30, 2007 9:06 AM
    Moderator

All replies

  • Hi Volker,

     

    There is not a read-only flag of the bookmark. I researched a lot. It seems there was already some discussion about this on many forums and google groups. Ideas from them are dividing your document to sections. Put your bookmarks to sections which will be protected. Since your bookmarks are in the certain section which is protected, user cannot modify them. More information you can get on the following two links:

    http://wordtips.vitalnews.com/Pages/T1716_Protecting_Bookmarks.html

    http://msdn2.microsoft.com/en-us/library/ms178793(VS.80).aspx

    Also, I find another trick can prevent your bookmarks from being modified. In Application_WindowSelectionChange event, you can have a test if user’s selection is in the bookmark we want to protect. If yes, we protect the whole document, and if no, we unprotect the whole document. Sample I write are as follows:

    Code Snippet

    private Word.Bookmark bm = null;

     

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                this.Application.WindowSelectionChange += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventHandler(Application_WindowSelectionChange);                  

     

                Word.Range rg = this.Application.ActiveDocument.Range(ref missing, ref missing);

                rg.Text = "This is the bookmark where you cannot modify";

                object range = rg;

                bm = this.Application.ActiveDocument.Bookmarks.Add("test", ref range);

     

                this.Application.ActiveDocument.Content.InsertAfter("This is the area you can modify");

            }

     

            void Application_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection Sel)

            {

                int selstart = Sel.Start;

                int selend = Sel.End;

                int bmstart = bm.Range.Start;

                int bmend = bm.Range.End;

     

                if(!(selstart>bmend || selend< bmstart))

                {

                    this.Application.ActiveDocument.Protect(Microsoft.Office.Interop.Word.WdProtectionType.wdAllowOnlyReading, ref missing, ref missing, ref missing, ref missing);

                }

                else

                {

                    this.Application.ActiveDocument.Unprotect(ref missing);

                }

            }

     

     

    Hope this can help! J

     

    Thanks

    Ji

     

    Thursday, August 30, 2007 9:06 AM
    Moderator
  • Thank you very much - that SelectionChange-Event hint is cool!
    I'm using the SelectionChange-Event anyway for testing if one of my bookmarks is selected.

    Thanks & best regards
      Volker


    Thursday, August 30, 2007 9:40 AM
  • Did the above solution met your requirement?
    I am developing a similar product and I found  that the technique below seems to be more appropriate, via use VisualStudio 2008.
    protect the entire document and keep it protected but use the following API: Bookmark.Range.Editors(..)

    Thursday, July 30, 2009 8:19 AM