Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAxObject and Find function in Excel
QtWS25 Last Chance

QAxObject and Find function in Excel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 792 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oda412
    wrote on last edited by
    #1

    Good day!
    I'm trying to find in Excel the cell wich contains text "{z8}" and then get it's row number:

    #include <QAxObject>
    #include <QGuiApplication>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QAxObject* mExcel=new QAxObject("Excel.Application");
        mExcel->setProperty("Visible", false);
        QAxObject *workbooks = mExcel->querySubObject( "Workbooks" );
        QAxObject *workbook = workbooks->querySubObject( "Open(const QString&)", QString("C:\\Users\\Olga\\Desktop\\test.xlsx") );
        QAxObject *mSheets = workbook->querySubObject( "Worksheets" );
        QAxObject *StatSheet = mSheets->querySubObject( "Item(const QVariant&)", QVariant("Sheet1") );
        QAxObject* cells = StatSheet->querySubObject("Cells()");
    
        QVariant What="{z8}";
        QVariant After="ActiveCell";
        QVariant LookIn=-4123;
        QVariant LookAt=2; //xlPart
        QVariant SearchOrder=1; //xlByRows
        QVariant SearchDirection=1;
        QVariant MatchCase=false;
        QVariant SearchFormat=false;
    
        QVariantList list_ = { What, After, LookIn, LookAt, SearchOrder, SearchDirection,MatchCase,SearchFormat};
        QAxObject* cell=cells->querySubObject("Find(const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&)",list_);
        cell->dynamicCall("Activate()");
        int cr= cell-> property("Row").toInt();
        qDebug() << "The row number of cell is : " << cr;
    
        workbook->dynamicCall("Close()");
        workbook->dynamicCall("Quit()");
        return 0;
    }
    

    But there is an error: "QAxBase: Error calling IDispatch member Find: Exception thrown by server Code : -2147352571"

    Could you help me, please?

    Best regards,
    Olga

    JonBJ 1 Reply Last reply
    0
    • O Oda412

      Good day!
      I'm trying to find in Excel the cell wich contains text "{z8}" and then get it's row number:

      #include <QAxObject>
      #include <QGuiApplication>
      #include <QApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QAxObject* mExcel=new QAxObject("Excel.Application");
          mExcel->setProperty("Visible", false);
          QAxObject *workbooks = mExcel->querySubObject( "Workbooks" );
          QAxObject *workbook = workbooks->querySubObject( "Open(const QString&)", QString("C:\\Users\\Olga\\Desktop\\test.xlsx") );
          QAxObject *mSheets = workbook->querySubObject( "Worksheets" );
          QAxObject *StatSheet = mSheets->querySubObject( "Item(const QVariant&)", QVariant("Sheet1") );
          QAxObject* cells = StatSheet->querySubObject("Cells()");
      
          QVariant What="{z8}";
          QVariant After="ActiveCell";
          QVariant LookIn=-4123;
          QVariant LookAt=2; //xlPart
          QVariant SearchOrder=1; //xlByRows
          QVariant SearchDirection=1;
          QVariant MatchCase=false;
          QVariant SearchFormat=false;
      
          QVariantList list_ = { What, After, LookIn, LookAt, SearchOrder, SearchDirection,MatchCase,SearchFormat};
          QAxObject* cell=cells->querySubObject("Find(const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&,const QVariant&)",list_);
          cell->dynamicCall("Activate()");
          int cr= cell-> property("Row").toInt();
          qDebug() << "The row number of cell is : " << cr;
      
          workbook->dynamicCall("Close()");
          workbook->dynamicCall("Quit()");
          return 0;
      }
      

      But there is an error: "QAxBase: Error calling IDispatch member Find: Exception thrown by server Code : -2147352571"

      Could you help me, please?

      Best regards,
      Olga

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Oda412
      Excel OLE Automation is not good at providing any helpful error messages. I would guess there is something wrong in the parameters you pass (assuming the method signature is correct). But we do not know what they mean/should be. For example, LookIn=-4123 or SearchFormat=false might be wrong.

      Have you tried/tested this? I think if you "record macro" and do the find you get to see the generated VBA for this?

      O 1 Reply Last reply
      1
      • JonBJ JonB

        @Oda412
        Excel OLE Automation is not good at providing any helpful error messages. I would guess there is something wrong in the parameters you pass (assuming the method signature is correct). But we do not know what they mean/should be. For example, LookIn=-4123 or SearchFormat=false might be wrong.

        Have you tried/tested this? I think if you "record macro" and do the find you get to see the generated VBA for this?

        O Offline
        O Offline
        Oda412
        wrote on last edited by
        #3

        @JonB yes, I've recorded a macro in VBA.
        The working macro code in VBA is:

        Sub macro1()
        
        ' == selecting all cells in the worksheet ==
            
           Cells.Select
            
        ' == find function ==
            
            Cells.Find( _
            What:="{z8}", _
            After:=ActiveCell, _
            LookIn:=xlFormulas, _
            LookAt:=xlPart, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False, _
            SearchFormat:=False).Activate
                
        ' == displaying the row number ==
            
            MsgBox ActiveCell.Row
        
        End Sub
        
        JonBJ 1 Reply Last reply
        0
        • O Oda412

          @JonB yes, I've recorded a macro in VBA.
          The working macro code in VBA is:

          Sub macro1()
          
          ' == selecting all cells in the worksheet ==
              
             Cells.Select
              
          ' == find function ==
              
              Cells.Find( _
              What:="{z8}", _
              After:=ActiveCell, _
              LookIn:=xlFormulas, _
              LookAt:=xlPart, _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlNext, _
              MatchCase:=False, _
              SearchFormat:=False).Activate
                  
          ' == displaying the row number ==
              
              MsgBox ActiveCell.Row
          
          End Sub
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Oda412 said in QAxObject and Find function in Excel:

          After:=ActiveCell

          You may be optimistic thinking that would map to your

          QVariant After="ActiveCell";
          

          That is a string, ActiveCell should be a VBA object I think.

          Try simplifying your case. Try whatever it is to pass a "null/empty" variant for that one to let it default to searching from the top, while you get it going.

          O 1 Reply Last reply
          1
          • JonBJ JonB

            @Oda412 said in QAxObject and Find function in Excel:

            After:=ActiveCell

            You may be optimistic thinking that would map to your

            QVariant After="ActiveCell";
            

            That is a string, ActiveCell should be a VBA object I think.

            Try simplifying your case. Try whatever it is to pass a "null/empty" variant for that one to let it default to searching from the top, while you get it going.

            O Offline
            O Offline
            Oda412
            wrote on last edited by
            #5

            @JonB thank you for your help!
            The solution is follows:

            #include "widget.h"
            #include <QAxObject>
            #include <QApplication>
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                QAxObject* mExcel=new QAxObject("Excel.Application");
                mExcel->setProperty("Visible", false);
                QAxObject *workbooks = mExcel->querySubObject( "Workbooks" );
                QAxObject *workbook = workbooks->querySubObject( "Open(const QString&)", QString("C:\\Users\\Olga\\Desktop\\test.xlsx") );
                QAxObject *mSheets = workbook->querySubObject( "Worksheets" );
                QAxObject *StatSheet = mSheets->querySubObject( "Item(const QVariant&)", QVariant("Sheet1") );
                QAxObject* cells = StatSheet->querySubObject("Cells()");
                QVariant What="{z8}";
            
                QAxObject* cell=cells->querySubObject("Find(const QVariant&)",What);
                cell->dynamicCall("Activate()");
                int cr= cell-> property("Row").toInt();
                qDebug() << "Number of row is " << cr;
            
                workbook->dynamicCall("Close()");
                workbook->dynamicCall("Quit()");
                return 0;
            }
            

            Best regards,
            Olga

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved