CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2022
    Posts
    5

    Post Opening .xlsx file in Excel programmatically

    Hi all.

    I need help for a tiny .exe program, but I am not proficient in C++ (to say the least).

    This program needs to receive the full path to an excel file (.xlsx) in the command line (argv[1]).
    The file is password protected.
    I need to launch Excel, opening this file in read only mode, leave Excel open and exit.
    That's it.

    The purpose is to let some selected users open and read the file without releasing the password to anyone.
    (the password would be handed over and over again without any possible control, and at a certain point everyone would know it. on the contrary, IT can insure that the .exe program is distributed only to known authorized users)

    This can be done with .net calls (I have done it in labview) but I do no know enough in C++ to do it.

    Could anyone share a few lines of code that I can compile in Visual Studio?

    Thanks in advance.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Opening .xlsx file in Excel programmatically

    Quote Originally Posted by militar View Post
    Hi all.

    I need help for a tiny .exe program, but I am not proficient in C++ (to say the least).

    This program needs to receive the full path to an excel file (.xlsx) in the command line (argv[1]).
    The file is password protected.
    I need to launch Excel, opening this file in read only mode, leave Excel open and exit.
    That's it.

    The purpose is to let some selected users open and read the file without releasing the password to anyone.
    (the password would be handed over and over again without any possible control, and at a certain point everyone would know it. on the contrary, IT can insure that the .exe program is distributed only to known authorized users)

    This can be done with .net calls (I have done it in labview) but I do no know enough in C++ to do it.

    Could anyone share a few lines of code that I can compile in Visual Studio?

    Thanks in advance.
    You could start here
    https://learn.microsoft.com/en-us/wi...createprocessa

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Opening .xlsx file in Excel programmatically

    and where is the password going to be stored required to open the Excel file? If it's hard-coded into the program, then anyone can examine the file and obtain it. And what about password changes?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Nov 2022
    Posts
    5

    Re: Opening .xlsx file in Excel programmatically

    Hi.
    Password will initially be hardcoded - changes are not expected frequently - but I can add later on an ini file with password (obviously encoded),
    Only selected users can have the exe program and PC policy insures that nothing can be installed or run without IT authorizing it.

  5. #5
    Join Date
    Nov 2022
    Posts
    5

    Re: Opening .xlsx file in Excel programmatically

    Hi.

    I looked at the page you suggest, but the CreateProcess basically opens a shell window.
    Unfortunately, a password protected excel file cannot be opened from a direct command like .....\excel.exe file.xlsx

  6. #6
    Join Date
    Nov 2022
    Posts
    5

    Re: Opening .xlsx file in Excel programmatically


  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Opening .xlsx file in Excel programmatically

    Quote Originally Posted by militar View Post
    Yes, you'll probably need to use an Excel Automation to open the workbook protected with password.
    How to automate Excel from C++ without using MFC or #import
    How to automate Excel from MFC and Visual C++ 2005 or Visual C++ .NET to fill or obtain data in a range using arrays
    Victor Nijegorodov

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Opening .xlsx file in Excel programmatically

    As Victor already stated, we can do the magic by using Excel Automation. Here is an example:
    Code:
    void CExcelRunnerApp::OpenWorkbook(const CStringW& strFile,
        const CStringW& strPassword,
        BOOL bReadOnly /*= TRUE*/)
    {
        try
        {
            // create Excel application instance
            CApplication app;
            if (!app.CreateDispatch(L"Excel.Application"))
            {
                AfxMessageBox(_T("Cannot create Excel app instance"));
                return;
            }
    
            // get workbooks collection
            CWorkbooks wbks = app.get_Workbooks();
    
            // open the workbook
            COleVariant ovPassword(strPassword);
            COleVariant ovReadOnly((short)bReadOnly);
            COleVariant ovOptional(DISP_E_PARAMNOTFOUND, VT_ERROR);
            wbks.Open(strFile, ovOptional, ovReadOnly, ovOptional, ovPassword,
                ovOptional, ovOptional, ovOptional, ovOptional, ovOptional,
                ovOptional, ovOptional, ovOptional, ovOptional, ovOptional);
    
            // show Excel to the user
            app.put_Visible(TRUE);
        }
        catch (COleDispatchException* pExc)
        {
            DisplayExceptonErrorMessage(pExc);
            pExc->Delete();
        }
    }
    Notes:
    • I have attached a Visual Studio 2015 solution. After building the project, launch excelrunner.exe from command line, providing the full path and file name of the Excel file and the password, e.g. "excelrunner.exe c:\work\test\Book1.xlsx zuzububu". If everything goes well, it launch the Excel password-protected workbook in read-only mode, then exists.
    • CApplication and CWorkbooks are wrapper classes generated by MFC Class Wizard from a TypeLib (Microsoft Excel <xx.x> Object Library).
    • The generated #import directives must be commented/deleted to avoid a bunch of errors.
    • Both CApplication and CWorkbooks classes have a lot of methods (very similar with VB(.NET) or VBA automation stuff). I have deleted most of them for a "cleaner" and more intuitive demo.
    • Of curse, it is possible to achieve the same goal without using MFC, but for someone who isn't a "OLE/COM-guru" guy, that can be an overekill.

      Download:
    ExcelRunner.zip
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Nov 2022
    Posts
    5

    Re: Opening .xlsx file in Excel programmatically

    Hello Ovidiu.

    This is exactly what I need, I cannot thank you enough.
    Many many thanks,
    I could never get to do it, I realize that I do not have enough knowledge in windows programming.
    (after all, I have been programming "machinery", not windows applications)

    BTW, nice blog. Excellent reference for programming.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured