Thread: OPEN an Excel file with MACROS DISABLED

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    OPEN an Excel file with MACROS DISABLED

    Does anyone know how to OPEN an Excel file with MACROS DISABLED using C++?

    - The Excel file contains VBA macros.
    Last edited by highlowjack; 11-02-2007 at 12:48 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well assuming the reason for posting here is to open the file with something other than Excel, then the whole macro problem goes away.

    You're going to need a lot of code to open an excel file, and even begin to interpret embedded VBA macros when using your own code as opposed to opening it with excel.

    Or are you talking about launching the file within excel, but making sure excel itself runs with "macros disabled". In which case, what is your C++ question?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    CORRECT: launching the file within excel, but making sure excel itself runs with "macros disabled".

    I just want to OPEN an Excel file located on the windows desktop with MACROS DISABLED (using C++).

    The Excel file contains VBA macros - I do not want to interpret the VBA code - just open the excel file with macros DISABLED.
    Last edited by highlowjack; 11-02-2007 at 12:01 PM.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Meh. The only way C++ could help you out here would be that you find out where and how Microsoft Excel keeps its settings. And if there is any setting which shows whether macros are enabled or disabled, the program can change the setting to off, then shellexecute the xls file and then maybe after 10 secs restore the setting. Can't help you out here, I'm using OpenOffice.

    As far as I remember Excel used to ask if you want macros to be enabled whenever you opened an xls file with macros.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Below (In RED) is the Excel VBA CODE to open an Excel file with MACROS DISABLED.

    I need to use C++ to open the Excel file with MACROS DISABLED

    Application.EnableEvents = False
    Workbooks.Open Filename:="Target Excel File Path"
    Application.EnableEvents = True

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure you CAN do that (trivially) with C++. There's probably a way to use COM objects and connect to Excel, and tell it to open a file [once you have a COM connection, the "commands" that you use to open a file would probably be similar to the VB script method, but you'll have to figure out how to use a COM object to connect to Excel - and I have no idea how that works - or even if it's remotely the right idea, but some googling of COM + open file + excel or similar might give you an idea if that's doable or not]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Give the following code a try to see if it works....

    Code:
    #include <afx.h>
    #include <iostream>
    using namespace std;
    #import "c:\Program Files\Common Files\Microsoft Shared\Office10\mso.dll"
    #import "c:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.olb"
    #import "C:\Program Files\Microsoft Office\Office10\excel.exe" \
        rename("DialogBox", "ExcelDialogBox") rename("RGB", "ExcelRGB")
    
    int main(void)
    {
        CoInitialize(NULL);
        Excel::_ApplicationPtr App("Excel.Application");
        try
        {
            App->PutVisible(0,TRUE);
            Excel::WorkbooksPtr WorkBooks = App->GetWorkbooks();
            _variant_t varNull;
            varNull.vt = VT_NULL;
    		App->EnableEvents = FALSE;
            Excel::_WorkbookPtr WorkBook = WorkBooks->Open("c:\\Test.xls", varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull, varNull);
    		App->EnableEvents = TRUE;
        }
        catch (_com_error &ex)
        {
            char szTemp[1024]= {0};
            CString csErrorMessage = "Error Message:\n";
            sprintf(szTemp, "Code = %08lx\n", ex.Error());
            csErrorMessage += szTemp;
            sprintf(szTemp, "Code Description = %s\n", ex.ErrorMessage());
            csErrorMessage += szTemp;
            sprintf(szTemp, "Source = %s\n", ex.Source());
            csErrorMessage += szTemp;
            sprintf(szTemp, "Source Description = %s\n", ex.Description());
            csErrorMessage += szTemp;
            cout << csErrorMessage << endl;
        }
        CoUninitialize();
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Replies: 12
    Last Post: 03-10-2005, 07:48 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM