inserting selected page from one word document in another word document with c#

Posted by daemonkid on Stack Overflow See other posts from Stack Overflow or by daemonkid
Published on 2010-06-01T15:04:47Z Indexed on 2010/06/01 15:23 UTC
Read the original article Hit count: 329

Filed under:
|

I have a requirement to move selected pages from word DocumentA into another word DocumentB. So in the end DocumentB should have its own contents plus selected pages from DocumentA inserted at selected pages in DocumentB. The page number in DocumentB I will set thru properties.

This is the code I am using to just append contents of DocumentA to DocumentB.

  object missing = System.Reflection.Missing.Value;
  Word._Application wordApp = new Word.Application();
  Word._Document aDoc = new Word.Document();

  try
  {

     wordApp.Visible = false;
     object readOnly = false;
     object isVisible = false;

     aDoc = wordApp.Documents.Open(ref fPath1, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

     Word.Selection selection = wordApp.Selection;
     selection.InsertFile(fPath2, ref missing, ref missing, ref missing, ref missing);
     aDoc.Save();
     wordApp.Quit(ref missing, ref missing, ref missing);
   }
        catch(Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            wordApp = null;
            aDoc = null;
        }

However, I keep getting this exception 'object reference not set to instance of object' at the line 'selection.InsertFile...'

What is going wrong here?

And how do I insert contents of page 2 from DocumentA into page 3 of DocumentB?

Thanks for your time.

© Stack Overflow or respective owner

Related posts about c#

Related posts about word-automation