title: Passing collection dictionary data from C# to macro
body: <p>I want to passing the collection data dictionary from C# code into excel macros.
  The problem that i am facing now is an error during passing dictionary into macros.</p>
  
  <p>Here is a simple example of macro code that exists in excel module that I use:</p>
  
  <pre><code>Public Sub dictionaryTest1(dataku As Variant)
  
  Dim I As Integer
  For Each v In dataku.Keys
      MsgBox "Name: " &amp; v &amp; " Age: " &amp; dataku.Item(v)
  Next
  End Sub
  </code></pre>
  
  <p>i
  And below is my c# code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Windows.Forms;
  using Excel = Microsoft.Office.Interop.Excel;
  using Microsoft.Office.Core;
  using System.Reflection;
  using System.IO;
  namespace WindowsMacro
  {
      public partial class Form1 : Form
      {
  public Form1()
      {
          InitializeComponent();
      }
  
  private void RunMacro(object oApp, object[] oRunArgs)
      {
          oApp.GetType().InvokeMember("Run",
              System.Reflection.BindingFlags.Default |
              System.Reflection.BindingFlags.InvokeMethod,
              null, oApp, oRunArgs);
      }
  
  private void button1_Click(object sender, EventArgs e)
      {
          Dictionary&lt;string, int&gt; dataku = new Dictionary&lt;string, int&gt;()
          {
              {"cat", 2},
              {"dog", 1},
              {"llama", 3},
              {"iguana", 5}
          };
  
          object oMissing = System.Reflection.Missing.Value;
  
          Excel.ApplicationClass oExcel = new Excel.ApplicationClass();
          oExcel.Visible = true;
          Excel.Workbooks oBooks = oExcel.Workbooks;
          Excel._Workbook oBook = null;
  
  
          //path to excel file at bin folder
          string executableLocation =   Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
          string xslLocation = Path.Combine(executableLocation, "File.xls");
  
          oBook = oBooks.Open(xslLocation, oMissing, oMissing,
              oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
              oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
  
          //Run macros with passing the dictionary data "dataku".
          RunMacro(oExcel, new Object[] { "dictionaryTest1", dataku });
          // Quit Excel and clean up.
          oBook.Close(false, oMissing, oMissing);
          System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
          oBook = null;
          System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
          oBooks = null;
          oExcel.Quit();
          System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
          oExcel = null;
          GC.Collect();   //Garbage collection.
      }
  }
  }
  </code></pre>