VSTO - Appending one Word document to another fiasco

Eugene Lerner 1 Reputation point
2022-05-22T22:45:48.037+00:00

I explored two scenarios:

//Common steps:
Application wordApp = new Application( );
Document sourceDoc = wordApp.Documents.Open( "My main.doc" );
Document appendee = wordApp.Documents.Open( "My appendee.doc" );
Range sourceRange = sourceDoc.Content;
object end = sourceRange.End - 1;
Range newRange = sourceDoc.Range( ref end, ref end );

//Scenario 1: Attempt to paste at the range of the last character
newRange.FormattedText = appendee.Content.FormattedText;
//Problem: appendee text inserted at the first character "overlaying" two documents.

//Scenario 2: Attempt to insert a section
Section sec = newRange.Sections.Add( );
//Problem: System.Runtime.InteropServices.COMException: 'The specified range is not from the correct document or story.'

What am I doing wrong?

Please help.

Thank you,

Eugene

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,480 questions
0 comments No comments
{count} votes