views:

171

answers:

2

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.

A: 

Are you selecting text in Word and then running this? I imagine you'd get that exception if no text was selected in Word.

Laplace
No, Im following the steps out in this link. It doesnt say that you need to select text http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx
A: 

I have been doing abit of Visio interop and selection is an array, so it could be you have not added anything to the wordApp.Selection.

ywm