title: Create Excel VBA code and button programmatically from C#
body: <p>I am in the middle of simple method, that saves my DataGridView into an Excel document (1 sheet only) and also adds VBA code and a button to run the VBA code.</p>
  
  <pre><code>public void SaveFile(string filePath)
      {
  
          Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
          ExcelApp.Application.Workbooks.Add(Type.Missing);
  
          //Change  Workbook-properties.
          ExcelApp.Columns.ColumnWidth = 20;
  
          // Storing header part in Excel.
          for (int i = 1; i &lt; gridData.Columns.Count + 1; i++)
          {
              ExcelApp.Cells[1, i] = gridData.Columns[i - 1].HeaderText;
          }
  
          //Storing Each row and column value to excel sheet
          for (int row = 0; row &lt; gridData.Rows.Count; row++)
          {
              gridData.Rows[row].Cells[0].Value = "Makro";
              for (int column = 0; column &lt; gridData.Columns.Count; column++)
              {
                  ExcelApp.Cells[row + 2, column + 1] = gridData.Rows[row].Cells[column].Value.ToString();
              }
          }
  
          ExcelApp.ActiveWorkbook.SaveCopyAs(filePath);
          ExcelApp.ActiveWorkbook.Saved = true;
          ExcelApp.Quit();
      }
  </code></pre>
  
  <p>I only implemented DataGridView export.</p>
  
  <p><strong>EDIT:</strong> Thanks to Joel I could, with proper words, search again for the solution. I think that <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;303872&amp;Product=vcSne" rel="nofollow">this may be helpful</a>. Would you correct me or give a tip or two about what I should look for.</p>