7

This might be a very simple question but I have been trying for last 4-5 hours w/o success. :(

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.

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.

As per SilverNinnjas' suggestions to create a proxy account:

-- Create a credential 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 called ExcelProxy and assign the PowerUser credential to it

EXEC msdb.dbo.sp_add_proxy 
@proxy_name=N'ExcelProxy',
@credential_name=N'PowerUser1',
@enabled=1

-- Grant ExcelProxy access to the "CmdExec" subsystem

EXEC msdb.dbo.sp_grant_proxy_to_subsystem 
@proxy_name=N'ExcelProxy', 
@subsystem_name =N'CmdExec'

-- Grant the login testUser the permissions to use ExcelProxy

EXEC msdb.dbo.sp_grant_login_to_proxy 
@login_name = N'shress2', 
@proxy_name=N'ExcelProxy'
GO

I m still getting the same error xecuted as user: CORP\shress2.

Unhandled Exception: System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 'E:\data_extracts\RenameSheets.xlsm'. There are several possible reasons:
The file name or path does not exist.
The file is being used by another program.
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.

Any reasons why? I am eagerly waiting for any feedback. Thanks a lot.

@SilverNinja, here's my C# 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();
    }
}
}
8
  • 2
    I would imagine that the user account that is used by default to run SQL Server Agent Jobs does not have sufficient permissions to interact with something that has an interactive GUI as Excel has. Using Aspose.Cells within a Console application could be a solution.
    – Uwe Keim
    May 21, 2012 at 19:37
  • 1
    Does your project use SQL? If not, you would be better off with a Scheduled Task.
    – jrummell
    May 21, 2012 at 19:43
  • I tried using Task Scheduler. It runs perfectly fine there. The problem is we are using SQL JOB to automate the process of feeding excel file(in my case my C# application generates) to a third party reporting tool. That's the only reason to make it run from SQL job. Thanks.
    – Nemo
    May 21, 2012 at 20:01
  • 1
    My guess is that the E: drive is a mapped network drive. You probably mapped it under your user, but the job is running under CORP\shress2 or whatever. Try specifying the UNC path to the file instead- something like "\\server\path\to\file\". May 21, 2012 at 21:19
  • Hi Chris Shain, I specified UNC Path in my C# console application to "\\\\myserver\\data_extracts\\RenameSheets.xlsm" and compiled/ran the code successfully. Now my question is on SQL Job. Under Command Area, it points to the path to my compiled exe which is like this : C:\Users\shress2\Documents\visual studio 2010\projects\T_OpenExcel\T_OpenExcel\bin\Debug\T_OpenExcel.exe I m still getting the same error. :(
    – Nemo
    May 21, 2012 at 21:33

3 Answers 3

1

You just need to pick the appropriate Job Type in the New Job Step editor. You could use either Powershell or CmdExec.

In the Command area, click the Open button to locate your console application compiled executable (exe).

If you have any parameters, add them here - otherwise configure the schedule.

You may have to use elevated permissions. To use elevated permissions, just navigate to Security->Credentials in SSMS and right-click New Credential. Next, configure the Proxy Account under Sql Server Agent->Proxies and right-click New Proxy. Configure the Proxy for CmdExec and use the Credentials you previously setup. In your SQL Agent Job step you can pick this credential to use when running your command.

In the SQL Job Step Command Area, you should type something like this:

excel E:\data_extracts\RenameSheets.xlsm
12
  • @SilverNinja, Thanks for prompt response. I did that. But it failed.I used CmdExec. Under Command: I pointed to the exe file that was generated by VS 2010 for my C# console application. Is that correct? I m failing while executing the Job. It says Unhandled Exception: System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 'E:\data_extracts\RenameSheets.xlsm'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook.
    – Nemo
    May 21, 2012 at 19:40
  • Did you add your custom credentials - by default SQL Agent Service Account cannot access the filesystem. You need a proxy account configured. May 21, 2012 at 19:41
  • 1
    @SilverNinja, In the Command Area, when I could either paste the path to my compiled exe, or as you said browse to the .exe file for my project. When I paste the path, it looks fine though it fails at the end. When I browsed to the path, it gives me gibberish code. (copied-pasted here-) MZ� Is this something that's causing the problem on accessing the file? Please let me know. Thanks.
    – Nemo
    May 21, 2012 at 19:53
  • See edits above...you need to tell CmdExec to open the file with Excel. CmdExec cannot open XLSM files. May 21, 2012 at 20:11
  • @SilverNinja- I understand that I cannot use CmdExec to open XLSM files. Does that mean I cannot provide path to me .exe file (compiled from my c# console application)? This is my path to .exe file that I pasted under Command Area C:\Users\shress2\Documents\visual studio 2010\projects\T_OpenExcel\T_OpenExcel\bin\Debug\T_OpenExcel.exe
    – Nemo
    May 21, 2012 at 20:16
0

New job

CmdExec

Now run exe file from it

It will give exception.

change exe code.

you cannot have remote server path make it local to same sql server \\myserver\data_extracts\RenameSheets.xlsm to d:\data_extracts\RenameSheets.xlsm or use file path where bin exe file path is copied . Path.GetDirectoryName(Application.ExecutablePath) eg. value: C:\Projects\ConsoleApplication1\bin\Debug\RenameSheets.xlsm

Make sure only one exe is running otherwise there will be error file is already opened

run exe code It will work. Command prompt cannot access network path .

xlApp = new Excel.Application(); xlApp.Visible = true; xlWorkBook = xlApp.Workbooks.Open("C:\Projects\ConsoleApplication1\bin\Debug\RenameSheets.xlsm

", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

   xlApp.DisplayAlerts = false;
   xlWorkBook.SaveAs("C:\Projects\ConsoleApplication1\bin\Debug\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;
-3

just need to pick the appropriate Job Type in the New Job Step editor. You can use either Powershellor CmdExec.

In the Command area, click the Open button to locate your console application compiled executable (exe).

If you have any parameters, add them here - otherwise configure the schedule.

You may have to use elevated permissions. To use elevated permissions, just navigate to Security->Credentials in SSMS and right-click New Credential. Next, configure the Proxy Account under Sql Server Agent->Proxies and right-click New Proxy. Configure the Proxy for CmdExec and use the Credentials you previously setup. In your SQL Agent Job step you can pick this credential to use when running your command.

In the SQL Job Step Command Area, you should type something like this:

excel E:\data_extracts\RenameSheets.xlsm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.