none
The requested member of the collection does not exist. RRS feed

  • Question

  • How to solve this error = "The requested member of the collection does not exist."

    Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
                Document myWordDoc = new Document();
                object missing = System.Type.Missing;
                object path1 = txtOpen.Text;
                myWordDoc = myWordApp.Documents.Add(path1, missing, missing, missing);

                foreach (Microsoft.Office.Interop.Word.Window window in myWordDoc.Windows)
                {
                    foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes)
                    {
                        for (var i = 1; i <= pane.Pages.Count; i++)
                        {
                            var bits = pane.Pages[i].EnhMetaFileBits;
                            var target = txtOpen.Text;
                            try
                            {
                                using (var ms = new MemoryStream((byte[])(bits)))
                                {
                                    var image = System.Drawing.Image.FromStream(ms);
                                    var pngTarget = Path.ChangeExtension(target, "png");
                                    image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);
                                }
                            }
                            catch (System.Exception ex)
                            {
                                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                }
                myWordDoc.Close(Type.Missing, Type.Missing, Type.Missing);
                myWordApp.Quit(Type.Missing, Type.Missing, Type.Missing);

    • Moved by Kristin Xie Thursday, March 17, 2016 7:44 AM
    Wednesday, March 16, 2016 12:03 PM

Answers

  • Hi, Hitesh Maheshwari

    Unfortunately, I can't reproduce this issue, so I suggest that you could avoid this error when accessing a member of a collection, ensure that the member exists prior to accessing the collection member. If you are accessing the member by index number, you can use the Count property of the collection to determine if the member exists. The following example:
    if(pane.Pages.Count>0)
    {
        for (var i = 1; i <= pane.Pages.Count; i++)
        {
           var bits = pane.Pages[i].EnhMetaFileBits;
           ......
        }
    }

    In addition could you reproduce this issue by use an other Word file or unload your Word file on OneDrive.
    Monday, March 21, 2016 5:32 AM

All replies

  • Check if this modification helps:

        for( var i = 0; i < pane.Pages.Count; i++ )

    or use foreach.

    Wednesday, March 16, 2016 6:45 PM
  • Hi, Hitesh Maheshwari

    According to your sample code and description, I have made a sample with Word 2013 to try to reproduce this issue, unfortunately, I can't. So I suggest that you could set one breakpoint to debug, then make sure which line code cause exception. You could refer to below test code:
    using Word=Microsoft.Office.Interop.Word;
    using System.Drawing.Imaging;
    using System.Drawing;
    using System.IO;

    static void DemoReproduce() { Word.Application myWordApp = new Word.Application(); myWordApp.Visible = true; object missing = System.Type.Missing; object docPath = "D:\\esclate.docx"; Word.Document myWordDoc = myWordApp.Documents.Open(docPath); foreach (Microsoft.Office.Interop.Word.Window window in myWordDoc.Windows) { foreach (Microsoft.Office.Interop.Word.Pane pane in window.Panes) { for (var i = 1; i <= pane.Pages.Count; i++) { var bits = pane.Pages[i].EnhMetaFileBits; var target = docPath; try { using (var ms = new MemoryStream((byte[])(bits))) { var image = Image.FromStream(ms); var pngTarget = Path.ChangeExtension(target.ToString(), "png"); image.Save(pngTarget, ImageFormat.Png); } } catch (System.Exception ex) { Console.WriteLine(ex.Message); } } } } ((Word._Document)myWordDoc).Close(Type.Missing, Type.Missing, Type.Missing); ((Word._Application)myWordApp).Quit(Type.Missing, Type.Missing, Type.Missing); }


    Friday, March 18, 2016 9:22 AM
  •   var bits = pane.Pages[i].EnhMetaFileBits;

    Error in this code only


    hitesh modani

    Sunday, March 20, 2016 10:09 AM
  • Hi, Hitesh Maheshwari

    Unfortunately, I can't reproduce this issue, so I suggest that you could avoid this error when accessing a member of a collection, ensure that the member exists prior to accessing the collection member. If you are accessing the member by index number, you can use the Count property of the collection to determine if the member exists. The following example:
    if(pane.Pages.Count>0)
    {
        for (var i = 1; i <= pane.Pages.Count; i++)
        {
           var bits = pane.Pages[i].EnhMetaFileBits;
           ......
        }
    }

    In addition could you reproduce this issue by use an other Word file or unload your Word file on OneDrive.
    Monday, March 21, 2016 5:32 AM
  • But It's convert only one page after that it's giving error, i want to convert all pages...

    hitesh modani

    Tuesday, March 22, 2016 7:33 AM
  • Hi, Hitesh Maheshwari

    According to your description, I can't reproduced this issue. So I suggest that you could unload your Word file on OneDrive.
    Wednesday, March 23, 2016 8:34 AM