none
gibberish text output in word output when generated using the template (.dotx) file RRS feed

  • Question

  • This is word interop related question.

    I have collection of RTF text and I need to generate the report in word by pasting this RTF content from collection in loop. I use word template (i.e. .dotx) file as a base template file so as to generate report in structured manner. In base template file, I have some placeholder texts which needs to be replaced with the RTF content. While replacing the placeholder text with RTF content I'm facing following two issues :

    1. maintaining the source formatting i.e. to apply format of RTF content (i.e. font, color, bold, etc.) in generated word report
    2. pasting of RTF text in proper order with proper content

    In order to maintain source formatting while copying the RTF content, I use

    Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting)

    This ensures that while I copy the RTF content from my collection, original source formatting (i.e. as per RTF text) gets applied. There are few other methods to paste the content i.e. Paste, PasteSpecial but they don't maintain the source formatting and upon pasting, it takes the font as the default font of the base word template file. Problem using the PasteAndFormat method is, generated output it bit gibberish when report is generated using template file while generated output is proper when not generated using template file.

    I have created the sample project (available at https://drive.google.com/open?id=1es1aBgewbJvQxmOAQF3FMhx3inu3keVy) which exactly reproduces the problem I face. In sample application, when you enter 1, it will generate the report without using the template file and that is the kind of final report I want. When you enter 2, it generates the report with gibberish text and I'm not able to figure out the reason for such output.

    Can anyone pls help me to figure out what is the problem with the sample application code and help me to generate the report with option 2 same as the one generated with option 1?


    Krunal C

    • Moved by CoolDadTx Monday, January 27, 2020 2:39 PM Office related
    Monday, January 27, 2020 2:21 PM

All replies

  • I looked at your project and saw the same problem.

    As far as I can tell, the problem occurs either because template.dotx already has some text in it, or is the wrong file type (I think it is actually a .dotm internally) or both.

    The text starts withJOB REFERENCE

    followed by a load of GUIDs or some such).

    If you create a new, empty Template.dotx in ConsoleApp1\Template , VS should copy that into the Debug\Template folder when you run the app, and everything seems to work. Saving as a template (.dotx, .dotm, .dot) *from Word* is always a bit tricky because Word changes the folder you chose as soon as you select a template file type. So you you have either to renavigate to the correct place or save to the standard template folder and copy the file in WIndows.

    If you actually need text/other material in the template, I suggest you start with a single character of text and see how that affects your code.


    Peter Jamieson

    Tuesday, February 4, 2020 8:02 PM