none
Remove Macro by C# RRS feed

Answers

  • As far as I know, there is no way to remove the password from a VBA project except through the UI. There's no way to provide a password using the VBE Automation interface.

    About the only thing I can imagine is to use SendKeys in concert with the CommandBars command to execute the menu+dialog. Something like

        SendKeys ("^{TAB} {TAB}test{TAB}test{ENTER}")
        ActiveDocument.VBProject.VBE.CommandBars(_
           "Tools").Controls(5).Execute

    But you have to remember that SendKeys is only going to work if the focus is IN the VB Editor window, and even then it wouldn't be reliable. And you have to know the password for each and every project.

    (Note: my example is for setting a password, not for entering one. It's just to give you a general idea of the only approach I know of that could work.)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Ho Duy Loi Thursday, September 29, 2011 8:21 AM
    Thursday, September 29, 2011 7:15 AM
    Moderator
  • I hope this helps

    I used Console Project.

    If you read below post, you may have detailed information. Thanks.

    http://csharp.net-informations.com/excel/csharp-open-excel.htm

                Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(@"c:\test\test.xlsm", Type.Missing, Type.Missing,6, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ",", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                xlApp.Visible = true;
             
    
    
                Microsoft.Vbe.Interop.VBComponents com = xlWorkBook.VBProject.VBComponents;
    
                foreach (Microsoft.Vbe.Interop.VBComponent c in com)
                {
                    if (c.Type == Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule)
                    {
                        com.Remove(c);
                    }
                }
    

     


    http://vsto.tistory.com
    Monday, September 19, 2011 2:23 AM

All replies

  • I hope this helps

     

                Microsoft.Vbe.Interop.VBComponents com = Globals.ThisWorkbook.VBProject.VBComponents;
    
                foreach (Microsoft.Vbe.Interop.VBComponent c in com)
                {
                    if (c.Type == Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule)
                    {
                        com.Remove(c);
                    }
                }
    


    http://vsto.tistory.com
    Saturday, September 17, 2011 1:37 AM
  • hi VSTO_Beginner

    thank you for your help.

    when I input a file excel, word... I want to test macro in them, if yes, I want remove those macros. So, how can I do it?

    and

    Microsoft.Vbe.Interop.VBComponents com = Globals.ThisWorkbook.VBProject.VBComponents;
    when I test. I was recived Error The name 'Globals' does not exist in the current context
    so how can I use "Globals"?

    thanks you very much :)

    Saturday, September 17, 2011 3:07 PM
  • Hello Ho Duy Loi

    You've posted in the VSTO forum, so VSTO_Beginner assumed that you're working with the VSTO technology. Apparently, this is not the case since your project does not recognize Globals.ThisWorkbook.

    I'm therefore going to move your question to the more appropriate General Office Developer forum.

    As you've added an additional criterium to your question - "I want to test macro in them" - we definitely need to know which version of Office you're targeting. 

    We also need to undestand the scenario. Why do you need to detect this? What's supposed to happen with the documents once any macros have been removed?


    Cindy Meister, VSTO/Word MVP
    Saturday, September 17, 2011 4:24 PM
    Moderator
  • hello Cindy Meister

    thanks for your reply

    I use MS office 2003 and 2007. I want to delete macro because I will send mail that file and I don't want recipients see the code in the file. So before I send mail, I want to delete all macro in them. This is my exercises from my teacher.

    and I using VS 2008 to programing(window form/C#). please help me.

    thanks so much.



    • Edited by Ho Duy Loi Saturday, September 17, 2011 4:50 PM
    Saturday, September 17, 2011 4:48 PM
  • I hope this helps

    I used Console Project.

    If you read below post, you may have detailed information. Thanks.

    http://csharp.net-informations.com/excel/csharp-open-excel.htm

                Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(@"c:\test\test.xlsm", Type.Missing, Type.Missing,6, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ",", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                xlApp.Visible = true;
             
    
    
                Microsoft.Vbe.Interop.VBComponents com = xlWorkBook.VBProject.VBComponents;
    
                foreach (Microsoft.Vbe.Interop.VBComponent c in com)
                {
                    if (c.Type == Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule)
                    {
                        com.Remove(c);
                    }
                }
    

     


    http://vsto.tistory.com
    Monday, September 19, 2011 2:23 AM
  • Hi VSTO_Beginner.

    your code is greate to delete macro in excel file. But It's not run if macro has password. How can I delete macro in excel file if It has password protected.

    thank you!

    Wednesday, September 28, 2011 2:50 PM
  • As far as I know, there is no way to remove the password from a VBA project except through the UI. There's no way to provide a password using the VBE Automation interface.

    About the only thing I can imagine is to use SendKeys in concert with the CommandBars command to execute the menu+dialog. Something like

        SendKeys ("^{TAB} {TAB}test{TAB}test{ENTER}")
        ActiveDocument.VBProject.VBE.CommandBars(_
           "Tools").Controls(5).Execute

    But you have to remember that SendKeys is only going to work if the focus is IN the VB Editor window, and even then it wouldn't be reliable. And you have to know the password for each and every project.

    (Note: my example is for setting a password, not for entering one. It's just to give you a general idea of the only approach I know of that could work.)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Ho Duy Loi Thursday, September 29, 2011 8:21 AM
    Thursday, September 29, 2011 7:15 AM
    Moderator
  • Hi Cindy Meister.

    How can I use SendKeys ("^{TAB} {TAB}test{TAB}test{ENTER}")
        ActiveDocument.VBProject.VBE.CommandBars(_
           "Tools").Controls(5).Execute

    in my project If I know password?

    thank you very much :)


    • Edited by Ho Duy Loi Thursday, September 29, 2011 8:31 AM not yet complete
    Thursday, September 29, 2011 8:22 AM