0

At the moment I'm working with Word.dotx files that hold several bookmarks which are being altered by a c# program.

For a Rebranding project I need to add several new bookmark fields and my predecessor code does reference to the Text Form Field Legacy Control inside Office Word 2010.

I create a new Text Form Field with Field Settings Bookmark pointed to TestBookmark1. I'm already aware of a certain bug that the bookmarkname of a text form field can contain max 20 chars.

When I run the testcode, the existing bookmarks are replaced perfectly while it crashes on the new bookmarks. The exception I receive here is "The range cannot be deleted"

The code that is used for replacing the bookmark goes as follows:

  public void ReplaceBookmark(string bookmarkName, string text)
  {
      try
      {
          var bookmarks = GetProperty("Bookmarks", _wordDoc); //worddoc is the Word.Document equivalent in late binding
          var exists = InvokeMember("Exists",
              bookmarks,
              new object[]
              {
                  bookmarkName
              }) != null && (bool)InvokeMember("Exists",
                  bookmarks,
                  new object[]
                  {
                      bookmarkName
                  });
          if (!exists)
              return;
          var bookmark = InvokeMember("Item",
              bookmarks,
              new object[]
              {
                  bookmarkName
              });

          var range = GetProperty("Range", bookmark);
          SetProperty("Text", range, text);

          InvokeMember("Add",
              bookmarks,
              new[]
              {
                  bookmarkName, range
              });
      }
      catch
      {
          CloseWord(false);
          throw;
      }
  }

The exception get's thrown at SetProperty("Text", range, text);

  private static void SetProperty(string propertyName, object instance, object value)
  {
      if (instance == null)
          return;
      var type = instance.GetType();
      type.InvokeMember(propertyName,
          BindingFlags.SetProperty,
          null,
          instance,
          new[]
          {
              value
          });
  }

When going deeper here it falls on the type.InvokeMember function.

I already saw a likewise solution found Here, But this example uses the Early binding principle that I for company reasons cannot use.

This leaves me with the following questions:

  • Am i adding the bookmarks incorrectly, or am i simply forgetting something?
  • Why do i get the "Range cannot be Deleted Exception"?
  • When i catch this specific error, is there another way to replace the bookmark?

Thanks in advance

Community
  • 1
  • 1
Schuere
  • 1,579
  • 19
  • 33

1 Answers1

0

I found it, probably another Office scam...

when adding a new Text Form Field you have the option to add the properties. In the Field Settings you can set the bookmark.

This however doesn't complete the bookmark thingy.

After setting the Text Form Field properties you still need to go to

Insert Tab => Tools Group => Bookmark => Select the correct bookmark (standard correctly highlighted) and press Add.

It's and sounds stupid, but I clearly didn't do the last steps.

greets

Schuere
  • 1,579
  • 19
  • 33
  • The reason for this is that, by default, the bookmark name is INSIDE the field code. You can see this by viewing the field codes (Alt+F9). That works fine for the form field functionality. But when you try to use the bookmark name and delete the Range it's not possible because it's INSIDE the form field code. That's why selecting the entire form field and applying the bookmark name to the field worked. You should also be able to accomplish your purpose by working with the FormField object instead of the Bookmark object. – Cindy Meister Dec 15 '15 at 18:04