简体   繁体   中英

How can I get back a previously created Excel ListObject?

I am creating a ListObject in Excel using VSTO as follows:

ListObject lo = ws_vsto.Controls.AddListObject(range, "MyList");

(The range variable is a previously defined range.)

If I then loop through the worksheet Controls collection I can find that ListObject.

However, if I save the workbook and reopen it, the Controls collection is empty. How can I get this ListObject back after re-opening so that I can continue working with it?

EDIT

I've got a bit further:

        var wb = Globals.ThisAddIn.Application.ActiveWorkbook;
        var wb_vsto = wb.GetVstoObject();


        foreach (Excel.Worksheet ws in wb.Worksheets)
        {
            var wsv = ws.GetVstoObject();
            foreach (Excel.ListObject l in ws.ListObjects)
            {
                MessageBox.Show(l.Name);
                var lo = wsv.Controls.AddListObject(l);
                Excel.Range range = lo.Range;
                range.Activate();
           }
        }

When I get to the var lo = line, I have a ListObject added to the Controls collection and available for use. However, it's DataSource property holds null. Is there an easy way to get the original data source back?

I then thought about rebuilding the data source from the information in the range. The range.Activate() line selects the list in Excel (so I know it has the right thing). However, I can't for the life of me work out how to get the data out of that range and get the address of the range. The MSDN documentation talks about the Address property but this doesn't appear to actually exist. (MSDN documentation for VSTO seems ropey at best).

I made the following changes to the initial code. You need to get a VSTO Listobject back from the Factory.

        var wb = Globals.ThisAddIn.Application.ActiveWorkbook;
        var wb_vsto = wb.GetVstoObject();


        foreach (Excel.Worksheet ws in wb.Worksheets)
        {
            var wsv = ws.GetVstoObject();
            foreach (Excel.ListObject l in ws.ListObjects)
            {
                MessageBox.Show(l.Name);
                //var lo = wsv.Controls.AddListObject(l);
                Microsoft.Office.Tools.Excel.ListObject lo = 
                          Globals.Factory.GetVstoObject(l);
                // I can now get at the datasource if neede
                var ds = lo.DataSource;
                // In my case the datasource was DataTable.
                DataTable t = (DataTable)d;

                if (t.Rows.Count > 0)
                {
                    foreach (DataRow r in t.Rows)
                    {
                    // Access row data.
                    }
                }
                //Excel.Range range = lo.Range;
                //range.Activate();
           }
        }

Have you tried? Microsoft.Office.Tools.Excel.ListObject lo= Microsoft.Office.Tools.Excel.ListObject.GetVstoObject(l) (it's C# I'm not sure in VB)

From MSDN GetVstoObject , be sure to read the remarks.

About your first question, you created the listOject with the code

ListObject lo = ws_vsto.Controls.AddListObject(range, "MyList");

To recover the object after save/reope the workbook, try this line of code:

ListObject lo = Globals.Factory.GetVstoObject(Worksheet.ListObjects["MyList"]);

You simply can't get back a ListObject control that you created dynmically during run-time. this is from the MSDN documentation .

By default, dynamically created list objects are not persisted in the worksheet as host controls when the worksheet is closed.

The only way I can get back my ListObject is to create them directly in the sheets of my template during design-time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM