Does anyone know how to OPEN an Excel file with MACROS DISABLED using C++?
- The Excel file contains VBA macros.
Printable View
Does anyone know how to OPEN an Excel file with MACROS DISABLED using C++?
- The Excel file contains VBA macros.
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?
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.
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.
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
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
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;
}