Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I am working on .xlsx file on windows in VC++ MFC and trying to create table in its worksheets. I am using XLS2007::ListObjects::add function for creating table but function fails every time. Please have a look what I am doing:

What I have tried:

XLS2007::ListObjects listobj =    oWorkSheet2007.GetListObjects();
	
XLS2007::ListObject Tblobj = listobj.Add(1,COleVariant(L"A1:J1008"),COleVariant((short)(0)),1,COleVariant(L"A1"),COleVariant(L"TableStyleMedium1"));

Am I doing it right it or Is there any thing else do I need to change in it? please help.
Posted
Updated 30-Aug-21 20:12pm

1 solution

As per documentation, a ListObject.Add function requires to define a sheet in which the list is added.

For example:
VB.NET
Set objListObject = ActiveWorkbook.Worksheets(1).ListObjects.Add(SourceType:= xlSrcExternal, _ 
Source:= Array(strServerName, strListName, strListGUID), LinkSource:=True, _ 
XlListObjectHasHeaders:=xlGuess, Destination:=Range("A1")), 
TableStyleName:=xlGuess, Destination:=Range("A10")) 


See:
VBA - ListObjects.Add method (Excel) | Microsoft Docs[^]
VSTO - How to: Add ListObject controls to worksheets - Visual Studio (Windows) | Microsoft Docs[^]
 
Share this answer
 
Comments
Aashish_stellar 3-Sep-21 4:54am    
Thanks for your help. As mentioned above I am working in VC++ using excel object model on windows. I think the problem is in its parameters which I am passing in XLS2007::ListObjects::Add() function. Please help if you can.
Maciej Los 3-Sep-21 8:43am    
As i mentioned in the answer: you're trying to add ListObject to collection of them, but you have to add ListObject to specific worksheet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900