Exemplo n.º 1
0
 private bool isMacroInjected()
 {
     try
     {
         var firstCodeModule = ExcelUtils.GetFirstCodeModule(Globals.ThisAddIn.Application.ActiveWorkbook);
         return(firstCodeModule != null);
     }
     catch (Exception ex)
     {
         //Logger.Log.Fatal("Macro Injection Check could not be executed.", ex);
         return(false);
     }
 }
Exemplo n.º 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)
            {
            }
        }