title: Run a C# Console Application from SQL Server Agent (Job)?
body: <p>This might be a very simple question but I have been trying for last 4-5 hours w/o success. :(</p>
  
  <p>I have a C# console application that simply opens an excel file. This excel file has Workbook_Open() event, which runs my macro. My macro simply renames sheet1 to RenameSheet1 in an active worksheet.</p>
  
  <p>I could run my C# project from IDE. I want to run this project from SQL job(SQL server 2008).  How do I do it? Please help me get this working. Thanks.</p>
  
  <p>As per SilverNinnjas' suggestions to create a proxy account:</p>
  
  <p>-- Create a credential containing the domain account CORP\PowerUser1 and its password</p>
  
  <pre><code>CREATE CREDENTIAL PowerUser1 WITH IDENTITY = N'CORP\shress2', SECRET = N'P@ssw0rd'
  GO
  USE [msdb]
  GO
  </code></pre>
  
  <p>-- Create a new proxy called ExcelProxy and assign the PowerUser credential to it</p>
  
  <pre><code>EXEC msdb.dbo.sp_add_proxy 
  @proxy_name=N'ExcelProxy',
  @credential_name=N'PowerUser1',
  @enabled=1
  </code></pre>
  
  <p>-- Grant ExcelProxy access to the "CmdExec" subsystem</p>
  
  <pre><code>EXEC msdb.dbo.sp_grant_proxy_to_subsystem 
  @proxy_name=N'ExcelProxy', 
  @subsystem_name =N'CmdExec'
  </code></pre>
  
  <p>-- Grant the login testUser the permissions to use ExcelProxy</p>
  
  <pre><code>EXEC msdb.dbo.sp_grant_login_to_proxy 
  @login_name = N'shress2', 
  @proxy_name=N'ExcelProxy'
  GO
  </code></pre>
  
  <p>I m still getting the same error
  xecuted as user: CORP\shress2. </p>
  
  <blockquote>
    <p>Unhandled Exception: System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 
    'E:\data_extracts\RenameSheets.xlsm'. 
    There are several possible reasons:<br>
    The file name or path does not exist.<br>
    The file is being used by another program.<br>
    The workbook you are trying to save has the same name as a currently open workbook.     at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)     at T_OpenExcel.Program.Main(String[] args) in C:\Users\shress2\documents\visual studio 2010\projects\T_OpenExcel\T_OpenExcel\Program.cs:line 24.  Process Exit Code -532462766.  The step failed.</p>
  </blockquote>
  
  <p>Any reasons why? I am eagerly waiting for any feedback. Thanks a lot.</p>
  
  <p>@SilverNinja,
  here's my C# code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Excel = Microsoft.Office.Interop.Excel;
  using System.Threading;
  
  
  namespace T_OpenExcel
  {
  class Program
  {
      static void Main(string[] args)
      {
  
          Excel.Application xlApp;
          Excel.Workbook xlWorkBook;
          //Excel.Worksheet xlWorkSheet;
  
          object misValue = System.Reflection.Missing.Value;
  
          xlApp = new Excel.Application();
          xlApp.Visible = true;
          xlWorkBook = xlApp.Workbooks.Open("\\\\myserver\\data_extracts\\RenameSheets.xlsm", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
  
         xlApp.DisplayAlerts = false;
         xlWorkBook.SaveAs("\\\\myserver\\data_extracts\\RenameSheets.xlsm", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  
  
                    xlWorkBook.Close(true, misValue, misValue);
          xlApp.DisplayAlerts = true;
  
          xlApp.Quit();
      }
  
      private static void RunMacro(Excel.Workbook xlWorkBook, object[] p)
      {
          //throw new NotImplementedException();
      }
  }
  }
  </code></pre>