locked
Creating an Excel Macro from Visual C# RRS feed

  • Question

  • Hello!
    I wanna create an Excel Macro to refresh data from database (I want to use this macro from excel), so I have written this code:
    xlModule = xlWorkBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
            xlModule.Name = "Module3";
    
            String macroCode = "' Makro 1\r\n" +
                    "Sub refreshData()\r\n" +
                    "   Application.StatusBar = \"Refreshing sheet SheetDataBase\" \r\n" +
                    "   ActiveWorkbook.Connections(\"Connection\").refresh \r\n" +
                    "   Application.StatusBar = \"\" \r\n" +
                    "End Sub";
            xlModule.CodeModule.AddFromString(macroCode);
    But after execution this code, I don't see any macros in my excel file. What have I done wrong?
    Wednesday, August 5, 2015 1:15 PM

Answers

  • Hello!
    I wanna create an Excel Macro to refresh data from database (I want to use this macro from excel), so I have written this code:
    xlModule = xlWorkBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
            xlModule.Name = "Module3";
    
            String macroCode = "' Makro 1\r\n" +
                    "Sub refreshData()\r\n" +
                    "   Application.StatusBar = \"Refreshing sheet SheetDataBase\" \r\n" +
                    "   ActiveWorkbook.Connections(\"Connection\").refresh \r\n" +
                    "   Application.StatusBar = \"\" \r\n" +
                    "End Sub";
            xlModule.CodeModule.AddFromString(macroCode);
    But after execution this code, I don't see any macros in my excel file. What have I done wrong?

    this may be a long shot but have you saved your file as an xlsm file. xlsx files do not allow macros in them
    Wednesday, August 5, 2015 1:43 PM

All replies

  • Hello!
    I wanna create an Excel Macro to refresh data from database (I want to use this macro from excel), so I have written this code:
    xlModule = xlWorkBook.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);
            xlModule.Name = "Module3";
    
            String macroCode = "' Makro 1\r\n" +
                    "Sub refreshData()\r\n" +
                    "   Application.StatusBar = \"Refreshing sheet SheetDataBase\" \r\n" +
                    "   ActiveWorkbook.Connections(\"Connection\").refresh \r\n" +
                    "   Application.StatusBar = \"\" \r\n" +
                    "End Sub";
            xlModule.CodeModule.AddFromString(macroCode);
    But after execution this code, I don't see any macros in my excel file. What have I done wrong?

    this may be a long shot but have you saved your file as an xlsm file. xlsx files do not allow macros in them
    Wednesday, August 5, 2015 1:43 PM
  • Fact, I saved my file as *.xlsx file. Now it works! Thank You :)
    Wednesday, August 5, 2015 1:52 PM