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
Forum Updated to NodeBB v4.3 + New Features

QAxObject and Find function in Excel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 804 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 29 Jun 2021, 12:51 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

    J 1 Reply Last reply 29 Jun 2021, 14:36
    0
    • O Oda412
      29 Jun 2021, 12:51

      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

      J Offline
      J Offline
      JonB
      wrote on 29 Jun 2021, 14:36 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 29 Jun 2021, 15:01
      1
      • J JonB
        29 Jun 2021, 14:36

        @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 29 Jun 2021, 15:01 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
        
        J 1 Reply Last reply 29 Jun 2021, 15:32
        0
        • O Oda412
          29 Jun 2021, 15:01

          @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
          
          J Offline
          J Offline
          JonB
          wrote on 29 Jun 2021, 15:32 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 30 Jun 2021, 07:10
          1
          • J JonB
            29 Jun 2021, 15:32

            @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 30 Jun 2021, 07:10 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

            5/5

            30 Jun 2021, 07:10

            • Login

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