Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to change the registry entry value of "AccessVBOM" to '1' when i access excel macro and later updated it to '0' once my job is completed.
I used the below code to set the value.
I can able to set the value to 1 and reset the value to 0 but automatically it is getting updated with the value 1.
C++
Void AccesstoVBAProject(BOOL bAccess)
{
	HKEY hKey;
	DWORD buffer = bAccess;
	unsigned long size = sizeof(DWORD);
	CLSID clsid;	
	int i = 0;
	BOOL flag = FALSE;
	CString version;
	CString subkey = "Software\\Microsoft\\Office\\";		
	for (i = EXCEL2003; i <= EXCEL2010; i++ )  
	{  
		if (SUCCEEDED(CLSIDFromProgID(szExcelProgID[i-1], &clsid)))  
		{ 
			switch(i)
			{
			case 1:		
				version = "11.0";			
				break;
			case 2:
				version = "12.0";			
				break;
			case 3:
				version = "14.0";			
				break;
			}		
			version = version + "\\Excel\\Security";
			subkey = subkey + version;
			break;
		}  
	} 		
	if(RegOpenKeyEx(HKEY_CURRENT_USER, (LPCSTR)subkey, 0, KEY_READ|KEY_SET_VALUE|KEY_CREATE_SUB_KEY|KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
	{
		if(RegSetValueEx(hKey, TEXT("AccessVBOM"), 0, REG_DWORD, (LPBYTE)&buffer, size) == ERROR_SUCCESS)
		{
			flag = TRUE;
		}
		RegCloseKey(hKey);
	}	
	return flag;
}
void GetMacroList()
{
AccesstoVBAProject(TRUE);
----
----
AccesstoVBAProject(FALSE);
}
Posted
Updated 7-Oct-12 21:23pm
v3
Comments
Sergey Alexandrovich Kryukov 8-Oct-12 3:01am    
Why? This is a really bad use of the registry, an abuse.
--SA
RS.Ratheesh 8-Oct-12 3:14am    
Because i have no other option to disable the default Office error message
Programmatic access to Visual Basic Project is not trusted.
RS.Ratheesh 8-Oct-12 4:03am    
Is there any API to enable Programmatic access to Visual Basic Project
Sergey Alexandrovich Kryukov 8-Oct-12 13:32pm    
First, it depends on what do you call "Visual Basic", but essentially, there are no such things, and should not be needed. Executable module is executable module. You should use some IPC -- please read on IPC for Windows. You try to interlock access to shared resources, that's it. The API is windows API, not "Visual Basic access".
--SA
Jochen Arndt 8-Oct-12 3:28am    
See here: http://blogs.msdn.com/b/cristib/archive/2012/02/29/vba-programmatically-enable-access-to-the-vba-object-model-using-macros.aspx

Changing the value requires a restart of Excel to come into effect.

1 solution

Hi.

It seems that the Office engine is keeping track of the last used value of the registry value and writing it on exit.

These are two different articles discussing the same (or similar behavior) I just found googling a bit:
MSDN Blogs > cristib > VBA - How to programmatically enable access to the VBA object model using macros
Quote:
It seems that the Office application will remember what was the value for this key when it first started. If while the application is running you attempt to modify the 'AccessVBOM' key by any method (macro, script, manually editing the key ...etc), except for going in 'Options' > 'Trust Center' > 'Trust Center Setting' > 'Macro Settings', your change will be discarded on exit. Since you need to exit the application and load it again for the setting to become effective, you cannot programmatically enable / disable access to VB object model.


RE: Enabling Turst access to Visual Basic Projects
Quote:
It is possible to Enable Trust to Visual Basic Project from VBA code.

It can be modifed through regedit.exe. It is called AccessVBOM, which can
also be modified through VBA code. However, closing Excel will set the
AccessVBOM value back
to the level that is indicated in the "Trust access to
Visual Basic Project" checkbox


A possible workaround might be delaying the last operation that sets it to zero, scheduling it to be run after a set amount of time or to wait for the Office engine to be unloaded. Otherwise, You may try to reset it to zero through the object model provided by the office engine (if available) rather than direct access to the registry value.

Regards,
Daniele.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900