1

I have a Qt application working with Excel, and I want to add a worksheet to a document. The simpliest solution is just call

QAxObject *sheets = workbook->querySubObject("Worksheets");
sheets->dynamicCall("Add()");

But this way you'll add a sheet BEFORE the last existing sheet, but I want to place it AFTER last sheet. Generated documentation will say you:

IDispatch* Add (QVariant Before, QVariant After, QVariant Count, QVariant Type) [slot]

Connect a signal to this slot:

    QObject::connect(sender, SIGNAL(someSignal(QVariant, QVariant, QVariant, QVariant)), object, SLOT(Add(QVariant, QVariant, QVariant, QVariant)));
Or call the function directly:

    QVariantList params = ...
    QAxObject * result = object->querySubObject("Add(QVariant, QVariant, QVariant, QVariant)", params);

But how should params be like? As I can see, "after" is second param, however I don't need "before" at all. What should I specify as params?

2 Answers2

2

You have to specify the last and the new sheet, otherwise if BEFORE and AFTER were both omitted, the new sheet will be inserted before the active sheet by default.

You can do something like this to insert after the last sheet:

// set of sheets
QAxObject* sheets = workbook->querySubObject( "Worksheets" );

// Sheets number
int intCount = sheets->property("Count").toInt();

// Capture last sheet and add new sheet
QAxObject* lastSheet = sheets->querySubObject("Item(int)", intCount);
sheets->dynamicCall("Add(QVariant)", lastSheet->asVariant());

// Capture the new sheet and move to after last sheet
QAxObject* newSheet = sheets->querySubObject("Item(int)", intCount);
lastSheet->dynamicCall("Move(QVariant)", newSheet->asVariant());

Regards,

  Valdir.
valdyr
  • 21
  • 3
0

I fell appound the same issue and solved it the same way. could not manage to make following syntax work :

sheets->dynamicCall("Add(After:=QVariant)", lastSheet->asVariant());

Neither :

sheets->dynamicCall("Add(Null, QVariant)", lastSheet->asVariant());

Nor :

sheets->dynamicCall("Add(QVariant, QVariant)", QVariant(), lastSheet->asVariant());
user2019716
  • 587
  • 4
  • 13