0

I am facing a issue while unprotecting the VBA Project of the protected excel.

IntPtr hWnd = FindWindow(null, ES.oXL.VBE.MainWindow.Caption);//Find window if (hWnd != IntPtr.Zero) { bool ret = BringWindowToTop(hWnd); //Bring VBE to top. } SendKeys.SendWait("%{F11}%TE" + strPassword + "~~%{F11}"); SendKeys.SendWait("{ENTER}"); SendKeys.SendWait("xyz"); SendKeys.SendWait("{ENTER}"); 

I Wrote a code like this to unprotect the VBA Project. It works fine when debugging application.

We use com object model. when i applied the same dll in the dll hive, call the function i am getting below error

System.ComponentModel.Win32Exception: Access is denied at System.Windows.Forms.SendKeys.SendInput(Byte[] oldKeyboardState, Queue previousEvents) 

Any help on this why its working?

4
  • may I ask why do you want to unprotect the VB project at runtime? Commented Mar 7, 2013 at 14:02
  • We have a requirement to add a new macro to the excel at run time Commented Mar 7, 2013 at 14:38
  • honestly this is not what you want to do. You should just open the Excel file, unprotect it manually in the Visual Basic Editor Project Explorer, and add the new code before saving the macro workbook and making it available to the users. Commented Mar 7, 2013 at 14:58
  • 1
    You might want to use APIs as shown HERE Commented Apr 24, 2013 at 9:56

1 Answer 1

1

What kind of environment did you deploy into?

It seems like the assembly is not running in a full trust security context which is why certain API calls are not a allowed

From the documentation:

The call to the Shell function requires full trust (SecurityException class).

UPDATE

Based on the OP's comments it appears that indeed the assembly being hosted in a COM+ container is not granted FullTrust.

In order to work around this you might consider deploying a separate assembly into the GAC on the target machine with FullTrust and enable the AllowPartiallyTrustedCallers option on this assembly. This assembly would have to be the one making the calls to SendKeys on your other COM+ hosted assembly.

But in the end, as others have pointed out this may not be the right solution. In general SendKeys should be used very sparingly and there are probably other better ways to accomplish your goals..

Sign up to request clarification or add additional context in comments.

4 Comments

and that leaves aside the issue of whether anyone could really rely on SendKeys to unprotect the VB Project in a productiion environment anyway - or even whether it should be done!
We are using classic asp as front end. We will deploy our dll objects into com+ Applications
what version of Excel are you trying to use and what are you using it for - is it reporting or calculation?
we are using office 2010 , we are using it for both reporting and calculation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.