title: How can I run a macro from a VBE add-in, without Application.Run?
body: <p>I'm writing a COM add-in for the VBE, and one of the core features involves executing existing VBA code upon clicking a commandbar button.</p>
  <p>The code is unit testing code written by the user, in a standard (.bas) module that looks something like this:</p>
  <blockquote>
  <pre><code>Option Explicit
  Option Private Module
  
  '@TestModule
  Private Assert As New Rubberduck.AssertClass
  
  '@TestMethod
  Public Sub TestMethod1() 'TODO: Rename test
      On Error GoTo TestFail
      
      'Arrange:
  
      'Act:
  
      'Assert:
      Assert.Inconclusive
  
  TestExit:
      Exit Sub
  TestFail:
      Assert.Fail &quot;Test raised an error: #&quot; &amp; Err.Number &amp; &quot; - &quot; &amp; Err.Description
  End Sub
  </code></pre>
  </blockquote>
  <p>So I have this code that gets the current instance of the host <code>Application</code> object:</p>
  <pre><code>protected HostApplicationBase(string applicationName)
  {
      Application = (TApplication)Marshal.GetActiveObject(applicationName + &quot;.Application&quot;);
  }
  </code></pre>
  <p>Here's the <code>ExcelApp</code> class:</p>
  <pre><code>public class ExcelApp : HostApplicationBase&lt;Microsoft.Office.Interop.Excel.Application&gt;
  {
      public ExcelApp() : base(&quot;Excel&quot;) { }
  
      public override void Run(QualifiedMemberName qualifiedMemberName)
      {
          var call = GenerateMethodCall(qualifiedMemberName);
          Application.Run(call);
      }
  
      protected virtual string GenerateMethodCall(QualifiedMemberName qualifiedMemberName)
      {
          return qualifiedMemberName.ToString();
      }
  }
  </code></pre>
  <p>Works like a charm. I have similar code for <code>WordApp</code>, <code>PowerPointApp</code> and <code>AccessApp</code>, too.</p>
  <p>The problem is that Outlook's <code>Application</code> object doesn't expose a <code>Run</code> method, so I'm, well, stuck.</p>
  <hr />
  <p>How can I execute VBA code from a COM add-in for the VBE, without <code>Application.Run</code>?</p>
  <p><a href="https://stackoverflow.com/a/28094773/1188513">This answer</a> links to a <a href="http://blogs.msdn.com/b/sachinsancheti/archive/2006/05/16/598951.aspx" rel="noreferrer">blog post on MSDN that looks promising</a>, so I tried this:</p>
  <pre><code>public class OutlookApp : HostApplicationBase&lt;Microsoft.Office.Interop.Outlook.Application&gt;
  {
      public OutlookApp() : base(&quot;Outlook&quot;) { }
  
      public override void Run(QualifiedMemberName qualifiedMemberName)
      {
          var app = Application.GetType();
          app.InvokeMember(qualifiedMemberName.MemberName, BindingFlags.InvokeMethod, null, Application, null);
      }
  }
  </code></pre>
  <p>But then the best I'm getting is a <code>COMException</code> that says &quot;unknown name&quot;, and the OUTLOOK.EXE process exiting with code -1073741819 (0xc0000005) 'Access violation' - and it blows up just as nicely with Excel, too.</p>
  <hr />
  <h3>UPDATE</h3>
  <p>This VBA code works, if I put <code>TestMethod1</code> inside <code>ThisOutlookSession</code>:</p>
  <pre><code>Outlook.Application.TestMethod1
  </code></pre>
  <p>Note that <code>TestMethod1</code> isn't listed as a member of <code>Outlook.Application</code> in VBA IntelliSense.. but somehow it happens to work.</p>
  <p>The question is, <strong>how do I make this work with Reflection?</strong></p>