Insert Text From File causes loss of text styles

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?

January 19th, 2012 9:35pm

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.
Free Windows Admin Tool Kit Click here and download it now
January 20th, 2012 3:04pm

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.

 

January 20th, 2012 5:50pm

Setting Style based on to (no style) in the Modify Style dialog box for your custom styles should help.   Im not sure what you mean when you write that paragraphs didnt have a style set anymore. Since each paragraphs in a Word document has a style assigned to it, your statement is unclear.
Free Windows Admin Tool Kit Click here and download it now
January 20th, 2012 6:39pm

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.

 

January 20th, 2012 6:59pm

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 its worth, to change the Style based on setting in code, set the BaseStyle property to the empty string.
Free Windows Admin Tool Kit Click here and download it now
January 21st, 2012 12:54am

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.

 

January 21st, 2012 1:05am

OK, Im glad it seems to be working now. Thank you for the follow-up.   Its hard to tell why it didnt work correctly from the start. One possibility is that document defaults were somehow interfering with the settings.
Free Windows Admin Tool Kit Click here and download it now
January 21st, 2012 2:31pm

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

July 3rd, 2015 5:37am

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.

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2015 11:52am

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.

July 3rd, 2015 4:26pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics