none
Why does Word doc lose formatting? using bookmarks cannot use contentcontrols RRS feed

  • Question

  • Hi

    I Have a word document thats a template for a report.  I put bookmark areas where the text needs to go.  but formatting is always lost on other parts of the doc.  for example:

                              Title of report

    this is centered?  then the bookmark comes after, its a paragraph that ...MUST be left justified.

    text here is left justified romans 12pt plain

                              Report Details 1

    this is centered, bold, roman 14pt bold like the other headers, it never changes.

    each time, well 50% of the time with the same exact code (asp.net mvc) it will put the text left justified, more often lately its centered, plain, 12pt.  it only carries over the 'centered' part not the bold nor 14pt.  i clear it out, put text, left justify, 12pt save, save again save again.  run, centered again.  its like word is malfunctioning.

    The GOAL is to use interop contentcontrols to put the data in there, but no example found on the internet actually compiles or if it does it gets runtime errors;  I find a few people asking the same thing over the past 8 years or so, none of them have a working solution posted online.  

    For this bad solution with bookmarks, one possible idea is to make a screen capture with the text that needs to stay unchanged, make it into a jpg image then put that onto the page?  This is duct tape and wd-40.  what is so broken here that makes Word not hold its formatting?

    Wednesday, July 8, 2015 1:50 PM

Answers

  • Hi Bradley

    Really, I encourage you to concentrate on ONE topic per thread. I realize you're frustrated and feel a need to vent, but it doesn't help to mix topics...

    I posted a brief answer to this concern in the other thread you have running. In a nutshell, Word controls formatting by its STYLES. And you should use styles to apply formatting, not directly format text with a font or paragraph formatting. Word has basically two style types: paragraph and character.

    You need to analyze your formatting requirements and create a style definition for each requirement. If your project will always be generating the same kind of document it's useful to start with a template and create the new documents from that. The template can contain the entire set of styles you need, as well as boiler-plate text, your content controls/bookmarks, etc. If you can't start with a template, then your code needs to create the styles in the document.

    RE asp.net mvc: I don't recall seeing this, before, but with so many posts I may have missed it. You need to realize that Word is not designed to run remotely or server-side and this is not supported by Microsoft. It can work, but it's not guaranteed. The Open XML file format was designed specifically for these kinds of scenarios.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 8, 2015 1:59 PM
    Moderator

All replies

  • Hi Bradley

    Really, I encourage you to concentrate on ONE topic per thread. I realize you're frustrated and feel a need to vent, but it doesn't help to mix topics...

    I posted a brief answer to this concern in the other thread you have running. In a nutshell, Word controls formatting by its STYLES. And you should use styles to apply formatting, not directly format text with a font or paragraph formatting. Word has basically two style types: paragraph and character.

    You need to analyze your formatting requirements and create a style definition for each requirement. If your project will always be generating the same kind of document it's useful to start with a template and create the new documents from that. The template can contain the entire set of styles you need, as well as boiler-plate text, your content controls/bookmarks, etc. If you can't start with a template, then your code needs to create the styles in the document.

    RE asp.net mvc: I don't recall seeing this, before, but with so many posts I may have missed it. You need to realize that Word is not designed to run remotely or server-side and this is not supported by Microsoft. It can work, but it's not guaranteed. The Open XML file format was designed specifically for these kinds of scenarios.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 8, 2015 1:59 PM
    Moderator
  • Cindy

    yes i am applying styles in there, they just get overwritten somehow, i set them, in the style by modifying it, saving it.  run and the text is skewed, formatting lost.

    the code runs c# but its thru the asp app as opposed to a winform.  

    no example of openxml actually runs to modify the contentcontrols in the doc.  i have tried alot of them.  i have contacted 3 different authors of articles on openxml, only one has replied saying it was so long ago he doesnt remember how to make it work;  another author on contentcontrols says he doesnt know c#, go search on forums to find out. an expert claiming to know xml, word and contentcontrols says sign a contract and pay us $$$ or go search on msdn forums for the answer.

    Wednesday, July 8, 2015 2:50 PM
  • Hi Bradley

    Now you're really confusing me!!! This message is in the Word for Developers forum and we've been discussing the Word object model (APIs), not Open XML. Also your code samples have all been API, not Open XML. You need to be a lot more specific about what it is you're working on/with or we're all going to go nuts...


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, July 8, 2015 3:33 PM
    Moderator
  • Cindy,

    i was hoping for some kind of sanity in the Word document being used for the reference 'template' in the project.  i am doing 2 parallel tasks, using openxml and the API hoping one will start working.

    right now bookmarks almost work.  the c# code puts in the variables to the report. but the format gets fouled up and thats a show stopper.  Maybe someone used sw to write bookmarks and could say oh, yes, you have to set this bit to 5 or set this variable to 'airplane' and then Words formatting will work.

    right now? im trying to make the openxml part of the code access contentcontrols, and it will use the same word document.  i have to accept that its possible to write into contentcontrols with openXml, just have yet to figure out how.  

    I think you answered this Word format problem, use styles;  but i set these up and my guess now is the code to write bookmarks steps on some format bits.  Im open to any ideas and will put comments into which ever thread you think is best.

    thanks again for your patience

    Wednesday, July 8, 2015 7:28 PM
  • Hi Bradley

    Let's put this discussion on (temporary) hold and continue with the content Controls in the Open XML SDK forum - see where that takes us :-)


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 9, 2015 3:31 PM
    Moderator