none
Range.Copy() throws an exception if the Range is just a Shape RRS feed

  • Question

  • Hi all;

    If I have a Range that is a Shape (not InlineShape) and call Range.Copy() on it I get:

    System.Runtime.InteropServices.COMException occurred
     HelpLink=wdmain11.chm#36966
     Message=Command failed
     Source=Microsoft Word
     ErrorCode=-2146824090
     StackTrace:
        at Microsoft.Office.Interop.Word.Range.Copy()
        at AutoTag2010.net.windward.autotag.word.WordFramework.SaveUsingClipboard(Range range, String tempFilename) in C:\src\Jenova\Dev\AutoTag\AutoTag2010\net\windward\autotag\word\WordFramework.cs:line 1195
     InnerException: 
    

    What can I do to avoid this exception if I need to copy just a Shape?

    thanks - dave

     


    The future of B.I. - Raw & Uncut
    Thursday, May 26, 2011 8:37 PM

Answers

  • Hi Dave

    Mmm, as I understand the object model, a Shape cannot be a Range. Shapes are in a different document "layer" and are anchored to a specific Range. You'd need to copy that Range to pick up the anchor (and thus the Shape). And the Shape object has no Copy method, itself.

    So I concur with Rich: if you want to copy only the Shape you'll need to select it, first.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Wednesday, June 1, 2011 12:40 PM
    • Marked as answer by DavidThi808 Wednesday, June 1, 2011 4:19 PM
    Friday, May 27, 2011 5:21 AM
    Moderator
  • Hi Dave,

    This works …

    Sub findShapes()

        Dim doc, doc2 As Word.Document

        Dim shp As Word.Shape

        Set doc = Word.ActiveDocument

        Set doc2 = Word.Documents.Add

        For Each shp In doc.Shapes

            shp.Select

            Selection.Copy

            Documents(doc2).Activate

            Selection.Paste

        Next shp

    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by DavidThi808 Wednesday, June 1, 2011 4:18 PM
    Friday, May 27, 2011 3:59 PM
  • Hi Dave,

    I tested Rich's code on my side, it works well.  I encountered the same problem with you, I agree with the opinion of Cindy and Tony, I also can't find the copy method of shape. We need get the selection and then copy and paste, below is the C# code which works well:

    using System;
    using Microsoft.Office.Interop.Word;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace CSWordCopyShape
    {
      class Program
      {
        static void Main(string[] args)
        {
          string baseDir = AppDomain.CurrentDomain.BaseDirectory;
    
          Word.Application app = new Word.Application();
          app.Visible = true;
          Word.Document doc = app.Documents.Open(baseDir + "DocWithShape.docx",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing);
          
          Word.Document docCopyTo = app.Documents.Open(baseDir + "DocShapesTo.docx",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing);
          try
          {
            foreach (Shape shp in doc.Shapes)
            {
              doc.Activate();
              shp.Select();
              Section sel = doc.Sections[1] as Section;
              if (sel != null)
              {
                sel.Range.Copy();
    
                docCopyTo.Activate();
                docCopyTo.Select();
                docCopyTo.Range(Type.Missing, Type.Missing).Paste();
              }
            }
          }
          catch (Exception ex)
          {
            throw ex;
          }
          finally
          {
            doc.Close();
            docCopyTo.Close();
            app.Quit();
          }
        }
      }
    }
    
    

    Hope this can give you the hint.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by DavidThi808 Wednesday, June 1, 2011 4:18 PM
    Wednesday, June 1, 2011 12:39 PM

All replies

  • Hi Dave,

    I've not tried this yet, but I'm guessing you might have to actually select it before issuing the copy command.

    As you know, an inline shape has a defined starting and ending location so a range command knows, without actually having to select it, what to pickup, but a non-linline shape is floating and thus the range.copy fails because it doesn't have actual starting/ending locations.

    Again, that's my untested guess.


    Kind Regards, Rich ... http://greatcirclelearning.com
    Thursday, May 26, 2011 11:32 PM
  • Hi Dave

    Mmm, as I understand the object model, a Shape cannot be a Range. Shapes are in a different document "layer" and are anchored to a specific Range. You'd need to copy that Range to pick up the anchor (and thus the Shape). And the Shape object has no Copy method, itself.

    So I concur with Rich: if you want to copy only the Shape you'll need to select it, first.


    Cindy Meister, VSTO/Word MVP
    • Proposed as answer by Bruce Song Wednesday, June 1, 2011 12:40 PM
    • Marked as answer by DavidThi808 Wednesday, June 1, 2011 4:19 PM
    Friday, May 27, 2011 5:21 AM
    Moderator
  • The Range is the anchor - a zero-length character. Its ShapeRange property gives you the Shape, but you still can't copy it, so, I, too, agree that selecting the range and then doing Selection.Copy is probably the easiest. One way round it, which I doubt you'll like, is to convert the shape to an inline shape, copy that, and undo the convert.
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    Friday, May 27, 2011 10:08 AM
  • Hi all;

    I do call Range.Select() just before Range.Copy() but I think you all are right that it's just the anchor and Word has trouble with that. We tried moving the Start or End out 1 and that works great if there  is a space or letter there - but it then pulls in that letter also.

    Any other ideas or is this just a limitation of the Word API? You can do a copy/paste of just an image in Word itself so it should be doable...

    thanks - dave


    The future of B.I. - Raw & Uncut
    Friday, May 27, 2011 2:49 PM
  • Hi Dave,

    This works …

    Sub findShapes()

        Dim doc, doc2 As Word.Document

        Dim shp As Word.Shape

        Set doc = Word.ActiveDocument

        Set doc2 = Word.Documents.Add

        For Each shp In doc.Shapes

            shp.Select

            Selection.Copy

            Documents(doc2).Activate

            Selection.Paste

        Next shp

    End Sub


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by DavidThi808 Wednesday, June 1, 2011 4:18 PM
    Friday, May 27, 2011 3:59 PM
  • Hi Dave,

    I tested Rich's code on my side, it works well.  I encountered the same problem with you, I agree with the opinion of Cindy and Tony, I also can't find the copy method of shape. We need get the selection and then copy and paste, below is the C# code which works well:

    using System;
    using Microsoft.Office.Interop.Word;
    using Word = Microsoft.Office.Interop.Word;
    
    namespace CSWordCopyShape
    {
      class Program
      {
        static void Main(string[] args)
        {
          string baseDir = AppDomain.CurrentDomain.BaseDirectory;
    
          Word.Application app = new Word.Application();
          app.Visible = true;
          Word.Document doc = app.Documents.Open(baseDir + "DocWithShape.docx",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing);
          
          Word.Document docCopyTo = app.Documents.Open(baseDir + "DocShapesTo.docx",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing);
          try
          {
            foreach (Shape shp in doc.Shapes)
            {
              doc.Activate();
              shp.Select();
              Section sel = doc.Sections[1] as Section;
              if (sel != null)
              {
                sel.Range.Copy();
    
                docCopyTo.Activate();
                docCopyTo.Select();
                docCopyTo.Range(Type.Missing, Type.Missing).Paste();
              }
            }
          }
          catch (Exception ex)
          {
            throw ex;
          }
          finally
          {
            doc.Close();
            docCopyTo.Close();
            app.Quit();
          }
        }
      }
    }
    
    

    Hope this can give you the hint.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by DavidThi808 Wednesday, June 1, 2011 4:18 PM
    Wednesday, June 1, 2011 12:39 PM
  • Hi all;

    Thank you - that was it. Very weird as the Shape.Anchor Start/End matched the Range Start/End but it still needs to be selected if it's the only thing.


    The future of B.I. - Raw & Uncut
    Wednesday, June 1, 2011 4:20 PM