Question

I've created a MS Word document with some bookmarks I want to replace at run-time with some text fetched from a database.
Originally this application was using OLE Automation (Word) to do these kind of things but then I had decided to find some alternative.
I've downloaded and used the Open Office SDK (last release) and implemented a simple project.

// Opens a MS Word document 
XComponent document = OpenDocument("Test.docx", "_blank", true);
XNameAccess xna = ((XBookmarksSupplier) document).getBookmarks();
string[] documentBookMarkKeys = xna.getElementNames();

if (documentBookMarkKeys.Length > 0) {
    uno.Any currentBookmark = xna.getByName("bookmark1");
    XTextContent bookmarkContent = currentBookmark.Value as XTextContent;

    if (bookmarkContent != null) {
        XTextRange xFound = bookmarkContent.getAnchor();
        xFound.setString("Some text here!");
    }
}

This simple routine I've put together trying to grab some code found on the Internet seems to work fine, but it doesn't keep the formatting of those bookmarks. I don't know if there are any other alternatives to achieve the same result. Am I doing something wrong?

I am using Open Office 3.4 via UNO.

Was it helpful?

Solution 2

I simply deleted and recreated bookmarks and everything seem to work fine now.

OTHER TIPS

If your aim is to keep the document in Microsoft Word then you may need to adjust your approach. My understanding is that OpenOffice will not keep all the meta data associated with the original document i.e. in my experience data fields from an MS Word doc will copy across just the text and the link to the original datasource/datafield will be lost. My guess is that the same could happen for bookmarks and that just because the 2 products support bookmarks it may be that using OO to manipulate Word will cause the underlying link to be lost. You could prove this by trying this manually i.e. amend your .docx using OO just to see if when you save the amended document that you get the required result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top