开发者

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

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 no开发者_JAVA技巧t 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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜