none
Convert "Selection.CopyAsPicture()" to Image File?

    Question

  • Hello,

    I failed to find a solution for my problem.

    I want to save the Word Selection as an Image file.

    It successfully copies the data from word file, but i cannot get an image from clipboard using c#. However, when i close the app and normally paste it in doc file .... it works fine!

    I am using the following CODE!!

    Microsoft.Office.Interop.Word.Application app;
      Microsoft.Office.Interop.Word.Document oDoc;
    
      app = new Microsoft.Office.Interop.Word.Application();
      oDoc = app.Documents.Open("C:\\test\\test.docx");
    
      //Set the range for First Page
      oDoc.ActiveWindow.Selection.SetRange(oDoc.Bookmarks["\\Page"].Range.Start,
      oDoc.Bookmarks["\\Page"].Range.End - 1);
     
      app.Selection.CopyAsPicture();
    
      if (Clipboard.GetDataObject() != null) 
      {
      IDataObject data = Clipboard.GetDataObject();
    
      if (data.GetDataPresent(DataFormats.Bitmap, true)) // Return False
      {
       Image image = (System.Drawing.Image)data.GetData(DataFormats.Bitmap, true);
       image.Save( "C:\\image.jpg");
    
      }
      else if (data.GetDataPresent(DataFormats.MetafilePict)) // Return TRUE but failed to convert as image
      {
       Stream myStream = (Stream)data.GetData(DataFormats.MetafilePict);
       Image m = Metafile.FromStream(myStream);
       
      }
      
      }
    


     

    I am using Studio 2010 and Office 2007.

     

    Could you please help me out!!

     

    N M A

     

    Wednesday, March 02, 2011 9:21 AM

All replies