コード例 #1
0
 private void RunMacro(string MacroName, string SuccessMessage)
 {
     if (!isMacroInjected())
     {
         InjectMacro();
     }
     try
     {
         if (ExcelUtils.RunMacro(MacroName))
         {
             MessageBox.Show(SuccessMessage);
         }
         else
         {
             MessageBox.Show(string.Format("{0} could not be executed.", MacroName));
         }
     }
     catch
     {
         MessageBox.Show(string.Format("{0} could not be executed succesfully", MacroName));
     }
 }
コード例 #2
0
        private void InjectMacro()
        {
            var          assembly = Assembly.GetExecutingAssembly();
            StreamReader _textStreamReader;

            try
            {
                assembly          = Assembly.GetExecutingAssembly();
                _textStreamReader = new StreamReader(assembly.GetManifestResourceStream("ExcelAddInForMacro.Resources.Macro.txt"));
                var macroText         = _textStreamReader.ReadToEnd();
                var firstCodeModule   = ExcelUtils.GetFirstCodeModule(Globals.ThisAddIn.Application.ActiveWorkbook);
                var newStandardModule = firstCodeModule != null ? firstCodeModule : Globals.ThisAddIn.Application.ActiveWorkbook.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule);
                var codeModule        = newStandardModule.CodeModule;
                if (codeModule.CountOfLines > 0)
                {
                    codeModule.DeleteLines(1, codeModule.CountOfLines - 1);
                }
                codeModule.AddFromString(macroText);
                //Globals.ThisAddIn.Application.ActiveWorkbook.Save();
            }
            catch (Exception ex)
            {
            }
        }