none
word.ActiveWindow.Selection.CopyAsPicture() does not work RRS feed

  • Question

  • hi 

       i want to make the data which i choose in the word to image. 

                var word = Globals.ThisAddIn.Application;
                Microsoft.Office.Interop.Word.Selection currentSelection = word.Selection;
                System.Windows.Clipboard.Clear();
                word.ActiveWindow.Selection.SetRange(word.ActiveDocument.Bookmarks["\\Page"].Range.Start,
     word.ActiveDocument.Bookmarks["\\Page"].Range.End - 1);
                word.ActiveWindow.Selection.CopyAsPicture();
                bool b = System.Windows.Clipboard.ContainsImage();// this returns false 
    is there any one can tell me that how can i get image from what i select?
    Monday, June 4, 2012 10:13 AM

Answers

  • Hi Tao636,

    I think it's hard to export it form the clipboard when you have mixed content in it. I think you can retrieve images from the InlineShapes collection of the Range of the document.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 6, 2012 7:33 AM
    Moderator

All replies

  • Hi Tao636,

    Thanks for posting in the MSDN Forum.

    As far as I know that

    word.ActiveWindow.Selection.SetRange(word.ActiveDocument.Bookmarks["\\Page"].Range.Start,word.ActiveDocument.Bookmarks["\\Page"].Range.End - 1);

    Will select the content of the current page. And it will throw exception if there only a image in the page. However the CopyAsPicture will work when the content include image and Clipboard.ContainsImage will return true only the content which has been copied into it only have image. So if the current page only have a image the snippet which has been quoted via me can't work, and if the content has not only image but also other things such as text the Clipboard.ContainsImage() will return false. The following snippet can return true. Please notice that only a image on the page.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using Microsoft.Office.Tools.Word;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Office = Microsoft.Office.Core;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace WordDocument14
    {
        public partial class ThisDocument
        {
            private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
                Word.Selection wdSelection = Application.Selection;
                Clipboard.Clear();
                //Application.ActiveWindow.Selection
                //    .SetRange(Application.ActiveDocument
                //    .Bookmarks["\\Page"].Range.Start, 
                //    Application.ActiveDocument.Bookmarks["\\Page"].Range.End - 1);
                // Only one image on the page.
                Application.ActiveWindow.Selection.SetRange(
                    Application.ActiveDocument.Range().Start,
                    Application.ActiveDocument.Range().End - 1);
                Application.ActiveWindow.Selection.CopyAsPicture();
                bool b = Clipboard.ContainsImage();
                if (b)
                    MessageBox.Show("YES");
                else
                    MessageBox.Show("NO");
            }
    
            private void ThisDocument_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisDocument_Startup);
                this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
            }
    
            #endregion
        }
    }

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, June 5, 2012 5:17 AM
    Moderator
  • Hi Tom, thank you for your reply!

    Frist,sorry for my pool English.

    What I want to do is each page  in the doc has a image. And I send these images to a listview control, just like a Navigation pane.Now I need to make images for pages.

    When I used

    word.ActiveWindow.Selection.Range.CopyAsPicture();

    How can I get the image from clipboard and insert it to the listview?


    Tuesday, June 5, 2012 8:03 AM
  • Hi Tao636,

    I think it's hard to export it form the clipboard when you have mixed content in it. I think you can retrieve images from the InlineShapes collection of the Range of the document.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, June 6, 2012 7:33 AM
    Moderator
  • Hi, did you try saving the clipboard to an image file?

    // load doc object m = Missing.Value; object isVisible = false; object filename = file; Word._Document newDoc = (Word._Document)oWordApp.Documents.Open(ref filename, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m); // convert doc to image newDoc.Content.CopyAsPicture(); string saveFile = Path.ChangeExtension(file, ".bmp"); ClipboardHelpers.SaveClipboardAsImage(saveFile, this.Handle); object saveChanges = false; newDoc.Close(ref saveChanges, ref m, ref m);



    • Proposed as answer by Lance.Roberts Wednesday, December 5, 2012 10:31 PM
    Wednesday, December 5, 2012 10:31 PM