title: How to get result of a Word VBA function in c#?
body: <p>I need to open a Microsoft Office Word document and get the result of a function of a module in that document:</p>
  
  <p>VBA module(<em>MainModule</em>):</p>
  
  <pre><code>Option Explicit
  
  Public Function GetResult() As String
      GetResult = "My Expected Result"
  End Function
  </code></pre>
  
  <p>I tried some codes and latest is: <sup><a href="https://support.microsoft.com/en-us/kb/306683" rel="nofollow">[Reference]</a></sup></p>
  
  <pre><code>using System.Reflection;
  using System.Runtime.InteropServices;
  using MsWord = Microsoft.Office.Interop.Word;
  using Microsoft.Office.Core;
  
  ...
  
  var wordApp = new MsWord.Application();
  wordApp.DisplayAlerts = MsWord.WdAlertLevel.wdAlertsNone;
  wordApp.Visible = true;
  wordApp.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
  
  MsWord._Document wordDoc = wordApp.Documents.Open(ref fileNameObject,  ref falseValue, ref trueValue, ref falseValue,
      ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
      ref missingValue, ref missingValue, ref falseValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue);    
  
  object n;
  n = wordApp.Run("GetResult");
  // or
  n = wordApp.Run("MainModule.GetResult");
  // or 
  n = RunMacro(wordApp, new Object[] {"MainModule.GetResult"});
  // or
  n = RunMacro(wordApp, new Object[] {"GetResult"});
  // or
  n = wordApp.Run("MainModule.GetResult", ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue);
  
  ...
  
  private static object RunMacro(object oApp, object[] oRunArgs)
  {
      return oApp.GetType()
          .InvokeMember("Run", BindingFlags.Default | BindingFlags.InvokeMethod, null, oApp, oRunArgs);
  }
  </code></pre>
  
  <p>The error at <code>RunMacro</code> line is:</p>
  
  <blockquote>
    <p>Exception has been thrown by the target of an invocation.<br>
    [Inner-Exception]: Unable to run the specified macro</p>
  </blockquote>
  
  <p>stack trace is:</p>
  
  <blockquote>
  <pre><code>at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
  at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
  at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
  at ConsoleApplicationTest.Program.RunMacro(Object oApp, Object[] oRunArgs) in c:\..\Visual Studio 2013\Projects\ConsoleApplicationTest\ConsoleApplicationTest\Program.cs:line 57
  at ConsoleApplicationTest.Program.Main(String[] args) in c:\..\Visual Studio 2013\Projects\ConsoleApplicationTest\ConsoleApplicationTest\Program.cs:line 32
  </code></pre>
  </blockquote>
  
  <p>Note:<br>
  My Word document is saved as an Office 97-2003 document by Office 2013.<br>
  And <em>Macro Settings</em> is <em>Enable all macros</em>.<br>
  And <em>Trust access to the VBA project object model</em> is also checked.  </p>
  
  <p>Any Help will be appreciated.</p>