none
pasting objects into word RRS feed

  • Question

  • Hi all, I'm developing a windows forms application that pastes tables from other word documents and excel charts into a single word document. I'm handling this in a loop that pastes a table or chart, then pastes some text about the table/chart, then repeat for all the objects I need to paste. The problem is when I open the word document all of the tables/charts are grouped at the top, and all the text is grouped at the bottom. So instead of getting chart then text then chart then text, I have 4 charts followed by 4 paragraphs. Does anyone know why this might be, or how I can better control how things are getting pasted?

    Any help is appreciated!


    Victory is mine!!
    Tuesday, August 30, 2011 6:40 PM

Answers

  • Hi smtraber

    I agree with Rich, but can perhaps offer a few insights into the problem. It would also be very important to know the VERSION of Word involved. I'd also like to know whether "grouped at the top" means they're side-by-side, overlapped, or one below the other.

    When working with Word, practically everything depends on the RANGE given as a "target" for what you want to do. Most developers who are unfamiliar with Word use Selection / Selection.Range - if they specify a Range at all.

    When graphics with textwrap formatting (members of the Shapes collection) are brought into a document, in most recent versions of Word an Option controls whether they are done so with textwrap formatting (as Shapes) or not (InlineShapes). In the case of a Shape, it's related to a RANGE through its Anchor property and will position itself relative to that Range.

    My best guess, based on your description, would be that the objects may be coming in with textwrap formatting and are positioning themselves relative to the current selection - the paragraph at the top of the document. "Square" text wrap would be the default setting for the afore-mentioned option. If any of these objects, or the total of the objects, is across the width of the page this will force the text to display below the objects because there's no room for it anywhere else.

    Most probably, the best thing would be for you to save the user's option setting, change the setting to default everything coming in as InlineShapes, insert things one-by-one, then change that option setting back. (Application.Options.PictureWrapType)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Wednesday, September 7, 2011 3:58 AM
    Wednesday, August 31, 2011 8:31 AM
    Moderator
  • Hi smtraber,

    Thanks for your post.

    Rich and Cindy have given you helpful suggestions. Based on my understanding, you could use selection.Insertfile method to inserts all or part of the specified file. Use InlineShapes.AddOLEObject Method to insert excel charts into word document.

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.insertfile(office.11).aspx (Selection.InsertFile Method)

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshapes.addoleobject(v=Office.11).aspx (InlineShapes.AddOLEObject Method)

    Below code inserted 13.docx and Book1.xlsx to 11.docx. You could set the inserted range according to your requirement. It works fine with Office2010.

     private void button1_Click(object sender, EventArgs e)
      {
    
       Microsoft.Office.Interop.Word.Application word_app = new Microsoft.Office.Interop.Word.Application();
    
       // Make Word visible (optional).
       word_app.Visible = true;
    
       // Create the Word document.
       Microsoft.Office.Interop.Word.Document oDoc = word_app.Documents.Open("D:\\11.docx");
       Microsoft.Office.Interop.Word.Paragraph para=oDoc.Paragraphs.Add();
       para.Range.InsertFile("D:\\13.docx");
       Microsoft.Office.Interop.Word.Paragraph para2 = oDoc.Paragraphs.Add();
       para2.Range.InlineShapes.AddOLEObject("Excel.Sheet.14","D:\\Book1.xlsx");
      } 
    

    If I misunderstood, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    • Edited by Liliane Teng Wednesday, September 7, 2011 3:54 AM
    • Marked as answer by Liliane Teng Wednesday, September 7, 2011 3:58 AM
    Thursday, September 1, 2011 9:37 AM

All replies

  • It sounds like it could be a couple things; one being floating shapes versus inline shapes and the other being how you are inserting the objects if you are using a range command. This is only speculation and it would be most helpful if you could post the section of code that contains this loop.

    Also include the version of Word you are using.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, August 30, 2011 8:23 PM
  • Hi smtraber

    I agree with Rich, but can perhaps offer a few insights into the problem. It would also be very important to know the VERSION of Word involved. I'd also like to know whether "grouped at the top" means they're side-by-side, overlapped, or one below the other.

    When working with Word, practically everything depends on the RANGE given as a "target" for what you want to do. Most developers who are unfamiliar with Word use Selection / Selection.Range - if they specify a Range at all.

    When graphics with textwrap formatting (members of the Shapes collection) are brought into a document, in most recent versions of Word an Option controls whether they are done so with textwrap formatting (as Shapes) or not (InlineShapes). In the case of a Shape, it's related to a RANGE through its Anchor property and will position itself relative to that Range.

    My best guess, based on your description, would be that the objects may be coming in with textwrap formatting and are positioning themselves relative to the current selection - the paragraph at the top of the document. "Square" text wrap would be the default setting for the afore-mentioned option. If any of these objects, or the total of the objects, is across the width of the page this will force the text to display below the objects because there's no room for it anywhere else.

    Most probably, the best thing would be for you to save the user's option setting, change the setting to default everything coming in as InlineShapes, insert things one-by-one, then change that option setting back. (Application.Options.PictureWrapType)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Liliane Teng Wednesday, September 7, 2011 3:58 AM
    Wednesday, August 31, 2011 8:31 AM
    Moderator
  • Hi smtraber,

    Thanks for your post.

    Rich and Cindy have given you helpful suggestions. Based on my understanding, you could use selection.Insertfile method to inserts all or part of the specified file. Use InlineShapes.AddOLEObject Method to insert excel charts into word document.

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.insertfile(office.11).aspx (Selection.InsertFile Method)

    http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshapes.addoleobject(v=Office.11).aspx (InlineShapes.AddOLEObject Method)

    Below code inserted 13.docx and Book1.xlsx to 11.docx. You could set the inserted range according to your requirement. It works fine with Office2010.

     private void button1_Click(object sender, EventArgs e)
      {
    
       Microsoft.Office.Interop.Word.Application word_app = new Microsoft.Office.Interop.Word.Application();
    
       // Make Word visible (optional).
       word_app.Visible = true;
    
       // Create the Word document.
       Microsoft.Office.Interop.Word.Document oDoc = word_app.Documents.Open("D:\\11.docx");
       Microsoft.Office.Interop.Word.Paragraph para=oDoc.Paragraphs.Add();
       para.Range.InsertFile("D:\\13.docx");
       Microsoft.Office.Interop.Word.Paragraph para2 = oDoc.Paragraphs.Add();
       para2.Range.InlineShapes.AddOLEObject("Excel.Sheet.14","D:\\Book1.xlsx");
      } 
    

    If I misunderstood, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

     

    • Edited by Liliane Teng Wednesday, September 7, 2011 3:54 AM
    • Marked as answer by Liliane Teng Wednesday, September 7, 2011 3:58 AM
    Thursday, September 1, 2011 9:37 AM