1

In reference to vbScript opens up excel but doesn't load macro/modules?

Now I tried to run my vbscript from SQL Job. Though I get Job executed successfully, it didn't open up my excel file(Checked Task Scheduler). I went to view the history for this job and found

Message Executed as user: GSOPS4\SYSTEM. Microsoft (R) Windows Script Host Version 5.7 Copyright (C) Microsoft Corporation. All rights reserved.C:\WINDOWS\system32\TestTOPTMay307.vbs(5, 1) Microsoft Excel: Microsoft Excel cannot access the file '\gsops4\data_extracts\TestTOPTMay307.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. Process Exit Code 0. The step succeeded.

I also followed the suggestions in this thread Run a C# Console Application from SQL Server Agent (Job)?

However I couldn't get it to work. This thing has been annoying me for past 48 hours. Any clue to what's causing it and how do I successfully execute my job( run my vbscript file from SQL) will be highly appreciated. Thanks all in adv!

Any thoughts?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Nemo
  • 1,111
  • 6
  • 28
  • 45
  • 3
    Do you really want to use VBS to open up Excel from a SQL Server job? Why? Anything that requires a dialog or UI of any sort is not going to work well from SQL Server. You also should check your path, `\gsops4\...\` does not seem correct. – Aaron Bertrand May 22 '12 at 20:46
  • 1
    I bet it's a permissions error. But @AaronBertrand has the first issue, there's not a valid path. Unless that file is in C:\gsops4\data_extracts and even then it's customary to always specify the full path. – jcolebrand May 22 '12 at 20:54
  • Generally I would try to avoid involving EXCEL.EXE in any headless process if at all possible. See the answer below for some suggestions for alternatives to extracting data from a spreadsheet. – ConcernedOfTunbridgeWells May 22 '12 at 21:11
  • @Aaron- My Excel vba code is fully automated which doesn't require human intervention at all. I ran vbScript code to execute my excel vba successfully via command line. The file path exists. Wondering i cannot access OS resources from SQL Job. I even tried creating proxy account and used it to access the file path but failed. – Nemo May 22 '12 at 21:19
  • @Nemo what kind of file path is `\gsops4\ `? Shouldn't that be `\\gsops4\ `? Or better yet a local path (since trying to do this over the network is just one more variable hurtling you towards pain)? – Aaron Bertrand May 22 '12 at 21:23
  • @Aaron- sorry. The filepath is \\gsops4\data_extracts\myfile.vbs. What is surprising is I can run it from cmdline successfully. So it made me think that I can logically run it from sql job as well but that isn't true it seems. I might have to try SSIS route as suggested by experts. Might require some learning curve but will get there. Thanks again for your time to take a look into my issue. Thanks. – Nemo May 22 '12 at 21:46
  • *You* running it from the command line is not the same as *SQL Server* running it from the command line, much less *SQL Server Agent* or some proxy account that might not be configured correctly. Never assume that because you can do something on your machine that SQL Server will be able to do the same thing - that takes some work to set up and is usually done incorrectly (e.g. just running SQL Server and Agent as a domain admin account). – Aaron Bertrand May 22 '12 at 21:48

1 Answers1

3

An alternative view

Excel is very good at popping up dialogs on the console and then hanging until they are actioned by a user. This is a very bad thing on a server, as it will freeze the process and leak running excel instances. It also requires you to have excel installed on the server itself.

Generally you would be far better off scheduling a SSIS job via agent that reads the spreadsheet through an OLEDB driver and then replicating the calculation on a server-side job. What exactly does the macro do?

I've done a few ETL jobs off Excel sources in my day and (IMO) the best way to deal with data off excel is to avoid having to invoke EXCEL.EXE at all costs. It is very finicky about dangling COM references, so you have to be very careful about disposing of all COM objects created. In some cases the default references (Worksheet, Workbook, Range etc.) create opaque references behind the scenes that you cannot actually tidy up programatically as the type library does not expose any facility to do so.

The .NET primary interop assemblies add extra complications to this as they generate their own references that have to be explicitly tidied up as well. There is a significant impedance mismatch between COM and .Net - to the extent that several books have been written on making COM and .Net components play nicely together.

Fortunately WSH doesn't involve .Net, but COM remoting on Excel COM servers is not something I would recommend doing from within a DBMS.

Two much safer approaches

  1. Open the workbook in the OLEDB driver - read the sheets into a staging table and then extract the data form there. This doesn't even need Excel to be installed on the server, and is quite robust.

  2. Unravel the .xlsx zip file and get out the worksheets from that - this actually works better than you would think. The sheetxx.xml files are in a fairly straightforward format and the only other thing you are likely to need is sharedStrings.xml. Generally you shouldn't need to do this with SQL Server if you have SSIS available, but it is quite a useful trick if you are working with (say) Oracle on a non-Windows host.

EDIT: In order to use Excel through OLE automation you need to have Excel installed on the machine you're running it on. Generally it's not a great plan to have Excel installed on a server as it's not particularly secure. It's also a desktop tool and has a tendency to leak COM references and running Excel instances if you don't dot your I's and cross your T's with your COM reference creation and disposal.

SSIS has an excel data source. You can see it by creating a SSIS project in BIDS and creating a new connection manager. One of your options will be Excel.

However, If you need to query a sharepoint list you're better off querying it programatically without using Excel at all. A little google-fu should turn up some examples of how to do this, e.g. here.. You can do this through a standalone .Net app or through a script task in the SSIS package (script tasks are .Net custom tasks that you can build within a SSIS package).

If you do this you're probably better off developing it outside SSIS (use Visual C# Express if you don't have any other option) and then porting it across to the script task. If you're familiar with Python, IronPython or Boo are great tools for frigging with .Net APIs interactively to get something working.

ConcernedOfTunbridgeWells
  • 64,444
  • 15
  • 143
  • 197
  • What exactly does the macro do? It refreshes the iqy data and pumps it into Activesheet. The iqy is generated to grab SharePoint list data. The process of getting refreshed data from that iqy file to Excel has to be automated. Automation was completed however there's a need to run this vbs script file from SQL Job because the other half of automation(feeding excel to report generator) is from SQL Job. I was wondering what do you mean by to have excel installed on the server itself? Will it solve my problem? – Nemo May 22 '12 at 21:25
  • I m interested in #1 safer approach. I m fairly new to SSIS. Can you please point me to right direction? Thanks. – Nemo May 22 '12 at 21:27
  • @Nemo - see above for some elaboration on Excel. – ConcernedOfTunbridgeWells May 22 '12 at 21:35
  • Thanks for your suggestions. I was wondering If the console application to read from SharePoint list will be run from a SharePoint Server then Icould use the SharePoint Object Model. I don't have access to the SharePoint server except I have access to the Url to SharePoint server. So If Iplan to run the console application from computer without SharePoint then I should use Web Services. So I used web services to access SharePoint list and get the iqy file that a user gets when he exports to excel from ribbon.Please let me know if I m right? – Nemo May 22 '12 at 22:01
  • You should be able to query the SP server through the web service - better off to do it through .Net than Excel for a server side process. – ConcernedOfTunbridgeWells May 22 '12 at 22:11