none
VSTO Copy & Paste: You cannot put drawing objects a text box

    Question

  • Hello,

    i have a VSTO-addin for using in Word 2003. On a specific event this addin downloads a .doc-file, opens it in the background within a hidden instance of word and copies the content of the wdMainTextStory into the clipboard. Afterwards this instance gets closed and the copied content gets pasted into the original document.

    The content gets copied at the current cursor-position. While the cursor is on the MainTextStory or any other like header or footer this works perfect. But when i add a textbox to the document, place the cursor within the textbox and let the copy & paste happen i get the COMException: You cannot put drawing objects into a text box, callout, comment, footnote, or endnote.

    Even if the downloaded document is empty and thus the copied data only contains a line break "\r" i receive this error.

    The exceptions gets throwm by <targetRange>.paste()

    Here a code snippet:
    object objFalse = false;
    object objTrue = true;
    object objFormat = Word.WdOpenFormat.wdOpenFormatAuto;
    object missing = Type.Missing;
    
    
    //opening the downloaded document
    Word.Document moduleDoc = targetRange.Document.Application.Documents.Open(ref filename,
              ref objFalse,
              ref objTrue,
              ref objFalse,
              ref missing,
              ref missing,
              ref missing,
              ref missing,
              ref missing,
              ref objFormat,
              ref missing,
              ref objFalse,
              ref missing,
              ref missing,
              ref missing,
              ref missing);
    
    
    moduleDoc.StoryRanges[Word.WdStoryType.wdMainTextStory].Copy();
    moduleDoc.Saved = true;
    moduleDoc.Application.NormalTemplate.Saved = true;
    
    object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
    moduleDoc.Close(ref saveChanges, ref missing, ref missing);
    
    Word.Range selectedRange = doc.Application.Selection.Range.Duplicate;
    selectedRange.Paste();



    doc is my current VSTO-Document.
    moduleDoc is the downloaded VSTO-Document

    The very same code works well with Office 2007. Does someone have a clue why this is coming ?
    And i cant just copy plain text or so as the downloaded document may also contain images etc. They all must be copied into the original document.


    Thanks for the support.
    Kind regards,
    Oliver
    Monday, October 26, 2009 5:52 PM

Answers

  • Hi Oliver

    I'd like to stick to the first issue, before looking more closely at the second. FWIW what you're running into there has to do with how Word manages styles and direct formatting...

    Back to the original question:

    1. "Images" could be members of the Shapes collection or the InlineShapes collection. (Textboxes can only be Shapes). Word handles InlineShapes the same as text characters, so there would be no anchor. Of course, there's no way for me to know at this remove...

    2. Why it only behaves this way with Word 2003 and not 2007 I don't know, for sure. I do know that Word partially implements a new graphics engine in version 2007, it might have something to do with that.

    3. If there's no particular reason to use the Clipboard (and it seems in your scenario there is not) then it's often better to use the Range.FormattedText property to transfer things from one document into another. You can see if using that drops the VSTO Runtime Control (that ActiveX in the Shapes collection) - it will depend on exactly where and how it's anchored:
        selectedRange.FormattedText = moduleDoc.Content.FormattedText

    This will definitely drop the last paragraph mark of moduleDoc and will not "clutter" the user's Clipboard. It could perhaps also affect the other issue you're seeing (but may not resolve it). You might take a close look at whether all (or any) of the formatting you're losing/not losing is formatted with styles? Using character styles in "moduledoc" that are not present in "doc" would be your best bet for retaining the formatting.
    Cindy Meister, VSTO/Word MVP
    Tuesday, October 27, 2009 10:09 PM
  • Hi Oliver

    I'm glad to learn that you have the first part working :-)

    <<Now i only suffer from the second issue - the loose of formatting :/
    What i dont understand is why this issue only affects specific formattings - like italic and the font-color - while others are copied well. This issue also only exist for Word 2007. In Word 2003 the same code works very well when copying my downloaded content into a bullet point.>>

    Without having the documents and code at hand, it's very difficult for me to say what might be going on.

    Word uses a set of internal algorithms to decide what direct formatting it will drop when integrating text into another paragraph. Word was originally designed to efficiently produce professional documents. The underlying philosophy stipulates that such documents are highly structured, and that this structure is primarily controlled by the use of styles. Anything formatted with a style will be retained; formatting applied directly to the text (by using the "bold" button, for example) is not guaranteed to be retained. And if you bring text formatted with a style that also exists in the target document, the target document's style definition will overwrite the formatting of the in-coming text. This is all to maintain structural consistency within the document, without the person editing needing to go through and make lots of manual changes.

    So what is retained or dropped will depend firstly on how the formatting was applied in the source document and secondly on the algorithm Word is using to integrate the in-coming text.

    As mentioned in my last reply, you should define a style (character level) for each type of formatting your using in the source document. Since you apparently control this document, that shouldn't be an issue. Word has a number of tools, available through the Styles task pane, that can help you create new styles (from a selected example), then apply them to all text formatted with a specific combination (such as Normal + bold) throughout the document.
    Cindy Meister, VSTO/Word MVP
    Wednesday, October 28, 2009 11:00 AM
  • <<But you're right - i tried using styles and then the bold and font-color are copied correctly. Yet thats not a good solution for my needs as this would require that i need to create/use a style for each formatting i need. Thats not very comfortable.

    Is there another way i can use the formatting buttons (bold, italic...) instead of styles having the correct formatting after copying ?>>

    Not really, no. Styles are without question the best (perhaps only reliable) approach.

    Word does have quite a few built-in styles, some of which might meet your needs. It might pay to check out the list and see what ones already have the formatting you need, which would save you needing to define all of them.

    It might also make sense to sit down and figure out exactly what formatting combinations will be required, at any point. Create these styles in the document, then they'll be there when you need them, all ready in the Styles task pane. Or you can create buttons for them. It all depends, really, on exactly what you're doing (which still isn't clear to me, in the detail I'd need to have an inspiration on the design).
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by oliver_zx Wednesday, October 28, 2009 12:52 PM
    Wednesday, October 28, 2009 11:46 AM
  • Hi Oliver

    1. You misunderstood my question about using VSTO :-) Is it necessary that moduleDoc be a VSTO document? Or could this be a "plain" document? If it could be a plain document (no VSTO customization attached) then the problem you're seeing should go away.

    2. Again, my question wasn't formulated clearly enough for you. I'm asking if it's absolutely necessary for you to use Select All. What kinds of things are you wanting to transfer from moduleDoc to the target file? Or, in other words, what does moduleDoc contain (very exactly)?
    Cindy Meister, VSTO/Word MVP
    Tuesday, October 27, 2009 4:49 PM

All replies

  • Hi Oliver

    As we're dealing with VSTO documents I do know where the problem comes from:

    Even a supposedly empty VSTO document contains a Shape object: the ActiveX control embedded in the document by VSTO to support the VSTO technology (see this page).

    Shapes are managed in the Word document text flow by being "anchored" to a Range. But it's not possible to embed (anchor) one shape within another (a Text Box).

    That's why you're getting the error message. 

    So...

    1. Is it really necessary that "moduleDoc" is a VSTO document?
    2. Why are you doing a Select All, then copying? What, very specifically, are you bringing in from "moduleDoc"? 
    Cindy Meister, VSTO/Word MVP
    Tuesday, October 27, 2009 1:52 PM
  • Hi Cindy,

    thanks for the response. Im not bound to VSTO when opening/copying the content from the document (moduleDoc). Am i understand correct that this error would not appear when i would use Interop ?

    With Select-All you mean the last two code lines ? The purpose is to copy the content from the clipboard to the current cursor position - thats why the range of the selection is used.


    Thanks and best regards,
    Oliver
    Tuesday, October 27, 2009 4:28 PM
  • Hi Oliver

    1. You misunderstood my question about using VSTO :-) Is it necessary that moduleDoc be a VSTO document? Or could this be a "plain" document? If it could be a plain document (no VSTO customization attached) then the problem you're seeing should go away.

    2. Again, my question wasn't formulated clearly enough for you. I'm asking if it's absolutely necessary for you to use Select All. What kinds of things are you wanting to transfer from moduleDoc to the target file? Or, in other words, what does moduleDoc contain (very exactly)?
    Cindy Meister, VSTO/Word MVP
    Tuesday, October 27, 2009 4:49 PM
  • Hi again,

    i stepped over another issue. Yet im unsure if i should open a new thread for it as its related to copy'n'paste as well.

    See my scenario above only having Office 2007 here (may also apply on Office 2003, hadnt tested yet though). When my downloaded document contains some formatted text and i copy it into my current document all goes fine until i copy the content into a bullet-point of a list.

    When im doing this im losing some format-information like Italic-state and the Font-color.
    Other information are copied well: Bold, underlined, size, background-color.


    Maybe some ideas how comes ? Looks strange to me...
    I already played around with the Paste-options of Interop.Word, yet without any luck.


    Thanks again,
    Oliver
    Tuesday, October 27, 2009 4:51 PM
  • Hi Cindy,

    oh ok...well yeah currently the downloaded document is a VSTO document itself. Yet i could remove the customization before copying the data. That should do the trick, huh ?! I will try that tomorrow.

    Well and the moduleDoc only contain data in the MainTextStory. But the data itself can be of the kinds: text, images... Yet textboxes wont be in these modules so we wont try to copy textboxes into a textbox of the current document.

    Hope that answers your questions ?!

    And why does this only apply on Word 2003 ? When i use the same code on Word 2007. Does the implementation of 2007 cut off the anchor itself when beeing copied from a VSTO-customized document ? (just beeing curious)


    Thanks,
    Oliver
    Tuesday, October 27, 2009 5:13 PM
  • Hi Oliver

    I'd like to stick to the first issue, before looking more closely at the second. FWIW what you're running into there has to do with how Word manages styles and direct formatting...

    Back to the original question:

    1. "Images" could be members of the Shapes collection or the InlineShapes collection. (Textboxes can only be Shapes). Word handles InlineShapes the same as text characters, so there would be no anchor. Of course, there's no way for me to know at this remove...

    2. Why it only behaves this way with Word 2003 and not 2007 I don't know, for sure. I do know that Word partially implements a new graphics engine in version 2007, it might have something to do with that.

    3. If there's no particular reason to use the Clipboard (and it seems in your scenario there is not) then it's often better to use the Range.FormattedText property to transfer things from one document into another. You can see if using that drops the VSTO Runtime Control (that ActiveX in the Shapes collection) - it will depend on exactly where and how it's anchored:
        selectedRange.FormattedText = moduleDoc.Content.FormattedText

    This will definitely drop the last paragraph mark of moduleDoc and will not "clutter" the user's Clipboard. It could perhaps also affect the other issue you're seeing (but may not resolve it). You might take a close look at whether all (or any) of the formatting you're losing/not losing is formatted with styles? Using character styles in "moduledoc" that are not present in "doc" would be your best bet for retaining the formatting.
    Cindy Meister, VSTO/Word MVP
    Tuesday, October 27, 2009 10:09 PM
  • Hi Cindy,

    good news. After i removed the VSTO-customization from the download-document it does work.
    I also use the FormattedText Member now to copy the content instead of using the clipboard. Thanks for the hint!!!

    Now i only suffer from the second issue - the loose of formatting :/
    What i dont understand is why this issue only affects specific formattings - like italic and the font-color - while others are copied well. This issue also only exist for Word 2007. In Word 2003 the same code works very well when copying my downloaded content into a bullet point.



    Thanks alot,
    Oliver
    Wednesday, October 28, 2009 9:48 AM
  • Hi Oliver

    I'm glad to learn that you have the first part working :-)

    <<Now i only suffer from the second issue - the loose of formatting :/
    What i dont understand is why this issue only affects specific formattings - like italic and the font-color - while others are copied well. This issue also only exist for Word 2007. In Word 2003 the same code works very well when copying my downloaded content into a bullet point.>>

    Without having the documents and code at hand, it's very difficult for me to say what might be going on.

    Word uses a set of internal algorithms to decide what direct formatting it will drop when integrating text into another paragraph. Word was originally designed to efficiently produce professional documents. The underlying philosophy stipulates that such documents are highly structured, and that this structure is primarily controlled by the use of styles. Anything formatted with a style will be retained; formatting applied directly to the text (by using the "bold" button, for example) is not guaranteed to be retained. And if you bring text formatted with a style that also exists in the target document, the target document's style definition will overwrite the formatting of the in-coming text. This is all to maintain structural consistency within the document, without the person editing needing to go through and make lots of manual changes.

    So what is retained or dropped will depend firstly on how the formatting was applied in the source document and secondly on the algorithm Word is using to integrate the in-coming text.

    As mentioned in my last reply, you should define a style (character level) for each type of formatting your using in the source document. Since you apparently control this document, that shouldn't be an issue. Word has a number of tools, available through the Styles task pane, that can help you create new styles (from a selected example), then apply them to all text formatted with a specific combination (such as Normal + bold) throughout the document.
    Cindy Meister, VSTO/Word MVP
    Wednesday, October 28, 2009 11:00 AM
  • Hi Cindy,

    well the code is exactly the same like shown in my scenario-description within the first post. Yet i use the FormattedText instead to copy the text. The different is that the targetRange is now a range which is part of a list and is directly behind a bullet-point.

    To your styles question. In my tryouts im creating the downloaded document at the very same system and office which i use later to open the main document. So the style-set is the same in both documents (initial set as its all freshly installed).

    What im doing is to open the later downloaded document and added a line of text where i set a different formatting on the words.
    Later in my main-document this document gets downloaded, openened in the background and the content gets simply copied in the range behind the bulletpoint using the FormattedText Member.


    But you're right - i tried using styles and then the bold and font-color are copied correctly. Yet thats not a good solution for my needs as this would require that i need to create/use a style for each formatting i need. Thats not very comfortable.

    Is there another way i can use the formatting buttons (bold, italic...) instead of styles having the correct formatting after copying ?


    Thanks for your support Cindy.


    Kind regards,
    Oliver
    Wednesday, October 28, 2009 11:21 AM
  • <<But you're right - i tried using styles and then the bold and font-color are copied correctly. Yet thats not a good solution for my needs as this would require that i need to create/use a style for each formatting i need. Thats not very comfortable.

    Is there another way i can use the formatting buttons (bold, italic...) instead of styles having the correct formatting after copying ?>>

    Not really, no. Styles are without question the best (perhaps only reliable) approach.

    Word does have quite a few built-in styles, some of which might meet your needs. It might pay to check out the list and see what ones already have the formatting you need, which would save you needing to define all of them.

    It might also make sense to sit down and figure out exactly what formatting combinations will be required, at any point. Create these styles in the document, then they'll be there when you need them, all ready in the Styles task pane. Or you can create buttons for them. It all depends, really, on exactly what you're doing (which still isn't clear to me, in the detail I'd need to have an inspiration on the design).
    Cindy Meister, VSTO/Word MVP
    • Marked as answer by oliver_zx Wednesday, October 28, 2009 12:52 PM
    Wednesday, October 28, 2009 11:46 AM
  • Ok, thats a solution i can live with then. Now the users have to use styles instead of using the plain bold-buttons etc but thats ok.


    Thanks again and best regards,
    Oliver
    Wednesday, October 28, 2009 12:52 PM