This may be a very simple question, but I tried the last 4-5 hours without success. :(
I have a C # console application that just opens an excel file. This excel file has a Workbook_Open () event in which my macro runs. My macro just renames sheet1 in RenameSheet1 to the active worksheet.
I can run my C # project from the IDE. I want to run this project from an SQL job (SQL Server 2008). How should I do it? Please help me get this job. Thank.
As suggested by SilverNinnjas to create a proxy account:
- create credentials containing the domain account CORP \ PowerUser1 and its password
CREATE CREDENTIAL PowerUser1 WITH IDENTITY = N'CORP\shress2', SECRET = N'P@ssw0rd'
GO
USE [msdb]
GO
- Create a new proxy named ExcelProxy and assign it PowerUser credentials
EXEC msdb.dbo.sp_add_proxy
@proxy_name=N'ExcelProxy',
@credential_name=N'PowerUser1',
@enabled=1
- ExcelProxy "CmdExec"
EXEC msdb.dbo.sp_grant_proxy_to_subsystem
@proxy_name=N'ExcelProxy',
@subsystem_name =N'CmdExec'
- ExcelProxy
EXEC msdb.dbo.sp_grant_login_to_proxy
@login_name = N'shress2',
@proxy_name=N'ExcelProxy'
GO
: CORP\shress2.
: System.Runtime.InteropServices.COMException: Microsoft Excel 'E:\data_extracts\RenameSheets.xlsm. :
.
.
, , , . 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) T_OpenExcel.Program.Main(String [] args) C:\Users\shress2\documents\visual studio 2010\projects\T_OpenExcel\T_OpenExcel\Program.cs: 24. -532462766. .
? . .
@SilverNinja,
#:
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;
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)
{
}
}
}