locked
Insert Text From File causes loss of text styles RRS feed

  • Question

  • This problem has been occuring for me for years, and going back multiple versions of Word  2003, 2007 and 2010.   It occurs both programatically using Word's VBA to open a .DOC file and insert the contents of another .DOC file.  And also just opening a .DOC file and using Word's Insert -> Text From File.

    It does not happen with every document and so far can't pin it down to any thing specific.  Also, it happens with paragraphs and text that are set to regular fonts (i.e. not using Word Styles) and also when creating custom Word Styles and setting the paragraphs to those styles in the doucment that is to be inserted at the end of an existing document. 

    All the fonts do exist and all docs were created the same way on the same computer.  Nothing fancy just plain text.

    What happens is when the 2nd document is inserted at the end of the open document.  Sometimes entire paragraphs change to a different font, font color and/or font size.   In tests where I set custom Word Styles.   And the same custom Word Style existed in both documents with the same settings - the paragraphs don't have the style setting anymore in the inserted text.

    Most of the time it works, but many times it doesn't.   And when it doesn't work, its usually only portions of the document that loose the formatting.

    Any idea why this happens and how to prevent it?

    Thursday, January 19, 2012 9:35 PM

Answers

  • The rule is that the inserted document adjusts to the formatting of the target document. Styles that are unique to the inserted document will be preserved.
     
    However, note that if a style in the inserted document is based on another style, which is present in the target, style changes may still occur.

    Stefan Blom, Microsoft Word MVP
    • Proposed as answer by Cindy Meister MVP Friday, January 20, 2012 3:23 PM
    • Marked as answer by Bruce Song Thursday, February 2, 2012 5:32 AM
    Friday, January 20, 2012 3:04 PM

All replies

  • The rule is that the inserted document adjusts to the formatting of the target document. Styles that are unique to the inserted document will be preserved.
     
    However, note that if a style in the inserted document is based on another style, which is present in the target, style changes may still occur.

    Stefan Blom, Microsoft Word MVP
    • Proposed as answer by Cindy Meister MVP Friday, January 20, 2012 3:23 PM
    • Marked as answer by Bruce Song Thursday, February 2, 2012 5:32 AM
    Friday, January 20, 2012 3:04 PM
  • That's exactly the problem I'm trying to resolve.  What specifically are the rules that must be followed to prevent those style changes?

    I have customers using our Word vba product that assembles documents and some of them have to do manual reformatting of their assembled documents to cleanup these Word glitches.

    Both documents use identical and standard fonts (Times New Roman and Arial).  And in my tests using Word styles, I inserted identical custom styles into both the source and target document before inserting one into the other.

    For example, the source document is plain text using Times New Roman and Arial.   The inserted document's body text is in Times New Roman and Arial.   But after inserting, the body text of the inserted document changes to "Trebuchet".   This is using in-line fonts (not Word Styles).

    In another case using Word Styles, I inserted idential Quick Word Styles in Calabria 10pt into both the source doc and the doc to be inserted.   Then I set all the body paragraphs in the document to be inserted to that custom style.  After inserting, the Word Styles existed in the source doc, but the pargraphs that were set to the style didn't have a style set anymore (and were changed to Helvetica).

    So how exactly do you prevent this?   I need a programatic way of setting up the templates and styles that ensures the paragraphs won't loose their formatting.

     


    Ian Lauder
    Friday, January 20, 2012 5:50 PM
  • Setting “Style based on” to “(no style)” in the Modify Style dialog box for your custom styles should help.
     
    I’m not sure what you mean when you write that paragraphs “didn’t have a style set anymore.” Since each paragraphs in a Word document has a style assigned to it, your statement is unclear.

    Stefan Blom, Microsoft Word MVP
    Friday, January 20, 2012 6:39 PM
  • I meant that when I had a paragraph of text that had a custom Word Style applied to it in the document to be inserted.

    Once the insert completed and you clicked that inserted paragraph and looked in the Word Styles menu, the paragraph would not have a Word Style associated with it anymore.  Whereas in the original document that was being inserted, you would see the Word Style highlighted in the Word Style Menu.

    But I think we are onto something finally.  What you mentioned about setting the based on style to "(no style)".   

    My last test with this using Word styles that was failing had my custom Word styles set to "Normal" as the based on style.  And even though the "Normal" style is defined identically in both the original and inserted documents it was failing with inserted paragraphs loosing their Styles (and subsequently the font type changed from Calibria to Helvetica).

    I just redid that test by only changing the based on style from Normal to "(no style)"  and now those paragraphs set to that custom Word style are retaining their style once inserted.

    When I am adding new styles programatically via VBA, I'm am specifically not setting the "based on" style, but now that I look at my inserted styles, its defaulting everything to using "Normal" as the default. So, I'll revisit that.

    If that's it, then that's the piece I've been looking for.  Thanks.

     


    Ian Lauder
    Friday, January 20, 2012 6:59 PM
  • If you are not seeing a style name in the Styles pane, try looking in the Apply Styles pane (Ctrl+Shift+S) instead. It should tell you the name of the style at the insertion point. (The only way to learn the names of all styles at the insertion point--paragraph, character, list, and table styles--is by looking in the old Style dialog box, which you can display if you assign a keyboard shortcut to the FormatStyle built-in command.)
     
    For what it’s worth, to change the “Style based on” setting in code, set the BaseStyle property to the empty string.

    Stefan Blom, Microsoft Word MVP
    Saturday, January 21, 2012 12:54 AM
  • It wasn't a problem with Style names disappearing from the Quick Styles or Styles list.   Those are always there, and their settings are always the same.

    What was happening is that the actual paragraphs of text lost the style settings.   The custom Style still exists, its just that the paragraph wasn't set to that style anymore.

    You would have to manually select the various paragraphs that lost their styles and then set them back to the Style after inserting the document.  What's thrown me for a loop for a long time is that this has been a sporadic problem which worked most of the time so I never could figure out how to consistently reproduce it to figure out how to fix it.

    I've already done a test manually setting the Based on to "(no style)" before doing the insert -> text from file.  And it did start working properly.  Still not sure why - since the Based on style was "Normal", and the Normal style was defined identically in both documents.

    And I've already updated my VBA code to set the Based on style to "".   My first test programmatically assembling about 5 documents together worked, so I think I understand what happened and now that I know about the  Base style trick.   That should take care of the problem.

    I've got a lot of documents to update and code to test, so I'll consider this closed for now unless something else crops up.

    Thanks.

     


    Ian Lauder
    Saturday, January 21, 2012 1:05 AM
  • OK, I’m glad it seems to be working now. Thank you for the follow-up.
     
    It’s hard to tell why it didn’t work correctly from the start. One possibility is that document defaults were somehow interfering with the settings.

    Stefan Blom, Microsoft Word MVP
    Saturday, January 21, 2012 2:31 PM
  • Hello,

    I am experiencing the same problem, only that I can't solve it with the solution mentioned. It works with some styles, but I have a style with bullets, and that style does not adhere to the settings in the target document. It keeps the settings from the inserted document.

    In the Style pane it is selected as normally. In the "Show style details"-pane (Shift+F1) I don't have the correct English name for it :-)), it shows several direct formatting settings, all in the paragraph format section and specifically "indent" and "Tab" part.

    If I press Ctrl+Q to reset the paragraph formatting the paragraph corrects itself and adheres to the target documents setting.

    I appreciate any input on this.

    Peeter

    Friday, July 3, 2015 9:35 AM
  • Hi pemok

    As this discussion was finished some time ago you're more likely to get a useful discussion by posting a new question and referencing this discussion (include a link).

    Also, I notice that the discussion is not about code, so doesn't belong in the Developer (MSDN) forum. I'll therefore be moving it to the IT Pro forum on the TechNet site.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 3, 2015 3:50 PM
  • There might be a conflict between indentation settings in the Paragraph dialog box and those of the bulleted style. You could just redefine the indentation after pasting.

    Like Cindy, I suggest that you create a new thread. You could also link to an example document so that we could have a look.


    Stefan Blom, Microsoft Word MVP

    Friday, July 3, 2015 8:24 PM
  • I will continue here, as I don't know how to answer Stefan in the other thread. And as a matter of fact it is code based although you can recreate the scenario manually. I have prepared 2 sample documents - the target and the source, but I don't know how to enclose them. I don't have any "file space" on the "internet". I could email them.

    To recapitulate my issue: I have different style settings in the different target documents. The common "thing" is the style name. The thing I want to achieve is that when inserting a source file into the target document, I want the styles of the target document to be the ones that "rule".

    My problem is that after inserting a file that has text with applied styles and with the same style names as in the target document, they don't fully adopt the settings of the target document. After the insertion of the source document into the target document, the paragraphs in the target document with the applied style "Punktlista", keep their settings. If you press Shift+F1 to view the settings of a "Punktlista"-paragraph, you will se that there now are some Direct formatting applied. Pressing Ctrl+Q will fix the problem.

    I would like that the target style will rule when inserting a file with the same style name applied.

    Thanks,
    Peeter

    Saturday, July 4, 2015 11:40 AM
  • I'm not sure which other thread you are referring to... A link would be helpful.

    By default, Word uses the style definition of the target document if there is a conflict. This is usually not a problem, assuming that you leave the paste options set to the defaults in Word Options. But, as I wrote, it may not work properly when bulleted and numbered styles are involved.

    Using your sign-in to this forum you already have a OneDrive account. But you can send the files to [e-mail address removed], and I'll have a look. Be sure to include brief information on the issue in the message and also a link to this thread.


    Stefan Blom, Microsoft Word MVP

    Saturday, July 4, 2015 1:46 PM
  • Stefan,

    Sorry about the thread-thing, I thought this was the Developer forum. I will e-mail you my documents.

    Thank you,
    Peeter

    Sunday, July 5, 2015 6:46 AM
  • I have received your documents. If you mean that the vertical spacing between numbered paragraphs is incorrect in the target document, then the explanation is as follows: The spacing is incorrect in the source as well. You can see that by clicking in the paragraph and pressing Ctrl+Q. In other words, the fix is to remove direct formatting in the source file before inserting it into the target.

    To be specific, the direct formatting is Line Spacing set to 1.5 lines in one of the bulleted paragraphs.


    Stefan Blom, Microsoft Word MVP

    Sunday, July 5, 2015 2:37 PM
  • Thank you Stefan,

    sorry for not explaining more precisely. Firstly it is a mistake that the spacing in the source document isn't according to the style. But isn't it strange that if you examine the settings via Shift+F1, it doesn't inform you of any direct formatting?!

    Now to the problem, what I want is that the tabs and the right indent shall apply in the target document. If you press shift+F1 after inserting the sorce into the target, it doesn't apply the settings, but indeed stated that there are direct formatting applied. That is the problem.

    Best regards,
    Peeter

    Monday, July 6, 2015 8:22 AM
  • Thank you Stefan,

    sorry for not explaining more precisely. Firstly it is a mistake that the spacing in the source document isn't according to the style. But isn't it strange that if you examine the settings via Shift+F1, it doesn't inform you of any direct formatting?!

       

    Sorry, there was an error in my previous response (now corrected). The direct formatting added is Line Spacing 1.5 (while the rest of the text is set to "Single"). If you select the option (in the Styles pane) to show the source of formatting, you'll see that if you click in the affected paragraph.

    Numbering must be set up via Home tab | Multilevel List. See http://shaunakelly.com/word/numbering/numbering20072010.html.


    Stefan Blom, Microsoft Word MVP

    Monday, July 6, 2015 1:56 PM
  • Hello again, My problem is that the tab setting and right indent doesn't following the style. Any help is appreciated. /Peeter
    Tuesday, July 21, 2015 1:59 PM
  • Read the article that Stefan linked. These are set in the list numbering attached to styles.

    Charles Kenyon Madison, WI

    Tuesday, July 21, 2015 10:38 PM
  • As Charles has pointed out, it is crucial that you set up numbering correctly. Indentation must be specified in the Multilevel List dialog box. See http://shaunakelly.com/word/numbering/numbering20072010.html.


    Stefan Blom, Microsoft Word MVP

    Thursday, July 23, 2015 12:01 AM
  • Thank you for your replies,

    I have now tried the approach in the mentioned link, but unfortunately the result is the same. Although my documents hasn't a numbered list but a bulleted list I used the approach mentioned.

    Again: I have different tab settings in the source and target documents, and I want the target documents style tab settings to apply when I insert a source document with the same style name. But it doesn't.

    What can be the problem?

    Best regards,
    Peeter

    Friday, July 24, 2015 12:49 PM
  • Did you use the same Styles in both documents?

    See also:

    How to control bullets in Ribbon Versions of Word by Shauna Kelly


    Charles Kenyon Madison, WI

    Sunday, July 26, 2015 6:37 PM
  • I use the same style names and now I have also done as in "How to control bullets in Ribbon Versions of Word by Shauna Kelly".

    But it still doesn't work. I enclose a link to 2 documents - target and source -

    https://onedrive.live.com/?id=5E2141D59C66DE3A%21119&cid=5E2141D59C66DE3A&group=0<o:p></o:p>


    Maybe you can see some oddities there?

    Thanks,
    Peeter

    Monday, July 27, 2015 9:12 AM
  • The problem: when I insert the source document into the target document, the bulleted list doesn't follow the style in the target document. Any help is appreciated. /Peeter
    Thursday, July 30, 2015 8:14 AM
  • Does anyone have more input on this? Is it a confirmed bug?

    Peeter

    Wednesday, August 5, 2015 8:48 AM
  • Double check your style names. A space in the name in one document would make it a different style name. There is not a bug in that if the style names are the same and if the style is already in use in the target document, pasted text will adopt the style definition of the target document. Looking at your target document, it is blank. You need to actually use the style you want adopted in that document for it to override the style in the source document.

    Charles Kenyon Madison, WI


    Wednesday, August 5, 2015 11:20 AM
  • Thank you for your answer.

    The style names are identical in both documents. It is the style with a bulleted list that doesn't work. Other styles, work as intended.  And the target document is empty, because it is built up by inserting other documents into it. So, is it by definition not possible to have such an approach - an empty target document?

    When you insert the source document into the target document, do you get the same result?

    Thanks,
    Peeter

    Monday, August 10, 2015 6:58 AM
  • Bullets and numbering are different, to some extent. There, the procedures given by Shauna Kelly in the two articles below need to be followed:

    How to create numbered headings or outline numbering in Ribbon Versions of Word by Shauna Kelly

    How to control bullets in Ribbon Versions of Word by Shauna Kelly

    As for inserting source documents into target documents, I have no idea what rules are followed.

    Again, if the style is used in the target document first, the target document's style definition will overrule the source document's definition.

    Alternatively, you could use the Organizer (Import/Export Styles) to copy one or more style definitions from the target document's template into the target document after the paste.


    Charles Kenyon Madison, WI

    Wednesday, August 12, 2015 7:28 PM