Get value inserted at bookmarks.

Hi
I have a large collection of documents where the document name needs to be changed.
Inside the document is a bookmark: “titel” where the name is stored, and I need to get that value.
But I am struggling to work out how to get the value inserted at a specific bookmark with the GetContent.GetChildElements.

I have tried something like:

var files = Directory.GetFiles(@"C:\Users\job\Desktop\IFSBRC", "*.docx", SearchOption.AllDirectories);
foreach (string fileName in files)
{
   try
   {
      string path = Path.GetDirectoryName(fileName) + @"\";

      var document = DocumentModel.Load(fileName, new DocxLoadOptions { PreserveUnsupportedFeatures = true });
      document.Protection.StopProtection();

      var titel = document.Bookmarks["titel"].GetContent(false).GetChildElements(ElementType.valueInstertedAtBookmarks??);

      // ignore the rest...

      document.Save($"{path}{titel}.docx");
      Console.WriteLine($"{path}{titel}.docx");
   }
   catch (Exception e)
   {
      Console.WriteLine($"Kunne ikke åbne {fileName}");
   }
}

Hi Jonas,

Try this:

string titel = document.Bookmarks["titel"].GetContent(false).ToString();

Does this solve your issue?

Regards,
Mario

Hi Mario.
Thank you for responding.

I allready tried your suggestion, and it returns nothing.

Jonas

Please send us the document that reproduces your issue so that we can investigate it.

Sure thing.
Where do i send it ?

You can send it via email or support ticket, see the Contact page.

1 Like

The problem occurred because the “titel” bookmark in the documents is indeed empty, the targeted text is not inside of it but rather beside it.

In other words, the following screenshot shows one of the document’s “titel” bookmark (the bookmark’s start and end are in the same position):

image1

And the following screenshot shows how that same bookmark should look in order to have text within:

image2

So, in order to retrieve this text we could call Content.ToString on the parent Paragraph which contains both the bookmark and the text after it.

string titel = document.Bookmarks["titel"].Start.Parent.Content.ToString()
    .Replace("\t", " ")
    .Replace("\v", " ")
    .TrimEnd();
1 Like

That makes perfectly good sense now.
Thank you very much Mario.