Excel Process Hangs when Run by MSTest from CruiseControl.NET - excel

I'm using CruiseControl.NET with MSTest to build my Excel add-in. Some of my tests involve opening Excel, performing some operations and checking the results. This is all achieved through COM.
If I run the tests from with Visual Studio 2008, they work perfectly. However, when the tests are started by CruiseControl, the Excel process is created but just hangs. The test process will only continue if I kill the Excel process. The webdashboard then reports an error in the TestInitialize function: which basically just creates and instance of Excel and loads my XLL.
Does anybody have any idea what might be the solution to my problem? I'm using Excel 2007 on my build server.
Thanks,
Chris

This is usually because there are undisposed objects that are still lingering around. You either need to be somewhat more robust in your test clearup (think workbooks, worksheets, worksheet, range - and possibly more) or you could go for a brute force approach and kill any excel instance using the Process class as part of your TestSetUp() method.

Related

Error in one vba workbook causes other independent workbooks to stop running

I've written a number of programs that monitor devices and records data. The programs are continuously running. I'm usually monitoring several devices using separate workbooks that are independent from each other. However, if one program gets an error, all of VBA stops, including those other programs.
Is there a way to have separate instances of VBA? So if one faults the others can still run? Thanks.
If you want an error in one workbook not to affect running code in another, you can use separate instances of Excel to run each macro.
To start a new instance, hold down the Alt key while opening Excel - it will ask you if you mean to start a new instance. Or you can create a new instance from VBA using CreateObject()

SSIS: Make Excel Visible In Script Task During SQL Server Agent Job Run

I built a package in SSIS that uses a script task to open an Excel file, format, and refresh some data in Excel. I would like to have Excel visible when the script task is running to see if Excel gets hung up which occurs all the time. Is this possible? I am converting a process that is calling Excel via a shell script to using SSIS to call Excel instead. I guess a second question is, is that a bad idea?
Why this is a bad idea
Generally speaking, administrators are tasked with maximizing the amount of "uptime" a server or service on the server has. The more software that gets installed on the machine, the greater the odds of service interruptions and outages due to patching. To be able to manipulate Excel in the mechanism you described, you're going to force the installation of MS Office on that machine. That will cost you a software license and the amount of patching required is going to blow holes in whatever SLAs those admins might be required to adhere to.
Memory leaks. Along with the whole patching bit, in the past at least, there were issues with programmatically manipulating Excel and it basically boiled down to it was easy to end up with memory leaks (I gotta make you understand. Allocated memory but never given it up, never let the allocated memory go down). Over time, the compounded effect is that running this package will result in less and less system memory available and the only way to reclaim it is through a reboot, which gets back to SLAs.
The reason you want to see what Excel is doing is so that you can monitor execution because it "gets hung up which occurs all the time". That doesn't sound like a stable process. Again, no admin is going to want an unstable process running on the servers. Something is not right in the cycle of events. Whether it's your code that opens Excel, the macros it runs, etc, something in there is awry and that's why you need to inspect the process. This is akin to putting a bandaid on a shotgun wound. Stop shooting yourself and you won't require bandages.
The task that you're attempting to perform is "open an Excel file, format, and refresh some data in Excel" SSIS can natively push data into Excel. If you preformat the file, develop your SSIS to write to the formatted file and just copy it off, that should work. It's not graceful but it works. There are better methods of providing formatted data but without knowing your infrastructure, I don't know if SSRS, SharePoint, Excel Services, Power Pivot, etc are viable options.
Why you won't be able to see Excel
Generally speaking, the account that runs SQL Agent is probably going to be fairly powerful. To prevent things like a shatter attack, from Windows 2008+ services are restricted in what they can do. For the service account to be able to interact with the desktop, you have to move it into the user tier of apps which might not be a good thing if you, or your DBA/admins, are risk adverse.
For more information, please to enjoy the following links
InteractWithDesktop
http://lostechies.com/keithdahlby/2011/08/13/allowing-a-windows-service-to-interact-with-desktop-without-localsystem/
https://serverfault.com/questions/576144/allow-service-to-interact-with-desktop
https://superuser.com/questions/415204/how-do-i-allow-interactive-services-in-windows-7
That said, if all of the stars are aligned and you accept the risk, of Allow Service to Interact with the Desktop, the answer is exactly as Sam indicated. In your unshown code, you need to set the Visible property to true.
As you go off and allow interactivity with the desktop and someone leaves some "testing" code in the package that gets deployed to production with MessageBox.Show("Click OK to continue"); be aware that if nobody notices this dialog box sitting there, you'll have a job waiting to complete for a very long time.
Regarding your first question, I understand that you want to debug your script task.
You can make Excel visible by adding the following line of code in your script task (assuming C# is the coding language):
// Create your Excel app
var excelApp = new Excel.Application();
// Make the Excel window visible to spot any issues
excelApp.Visible = true;
Don't forget to remove/comment that line after debugging.
Regarding your second question, I don't that this is a bad idea if you properly handle how Excel is opened and closed, in order to avoid memory issues.

Embedded Excel COM Object clipboard interefernce

I'm writing programs in C\C++ embedding Excel and handling it's COM object.
This automation process works flawlessly to manipulate sheets and getting benefit of excel capabilities.
M problem is that while processing data i use copy/paste operations, so if the processing takes some time, it's possible that interference happens as the clipboard is common between running processes
i don't know if there's a way privatize the clipboard or any other idea to avoid such problem
Thanks in advance
You cannot make a private clipboard and expect it to work with normal cut/copy/paste operations. You can use delays to avoid clipboard clashes. i.e. after you force a copy operation, wait a few hundred ms before pasting.
Also, programmatic use of the clipboard is considered bad practice. The clipboard is provided for the convenience of the user, not the programmer. See my favorite quote on the subject:
“Programs should not transfer data into our out of the clipboard
without an explicit instruction from the user.” — Charles Petzold,
Programming Windows 3.1, Microsoft Press, 1992
I found a way to do it for PowerShell scripts:
Create a Scheduled Task that call your script/program
Set it to "Run whether user is logged on or not"
When you run the Scheduled Task, Windows launch the script/program in a background session that uses its own Clipboard, not interfering with the one of the active session you have.

Excel on exit and macro run: 'Code execution has been interupted'

I am getting a 'Code execution has been interrupted' message on exit from excel intermittently of late. And its frequency is increasing. End allows me to leave excel, but once I get the message, my machine must be rebooted to allow for excel to open and run macros again. This intermittent message does come up with basic excel usage at exit, and is not limited to excel sessions following VBA macros use. Has anyone seen this or have a solution. It is getting very annoying.
I have came across this issue few times during the development of one complex Excel VBA app. Sometimes Excel started to break VBA object quite randomly. And the only remedy was to reboot machine. After reboot, Excel usually started to act normally.
Soon I have found out that solution to this issue is to hit CTRL+Break once when macro is NOT running. Maybe this can help to you too.

Is Scheduled, Unattended Server-Based Creation of Excel Workbook With VBA Possible?

I'd like to be able to schedule an Excel macro (VBA) to run in the middle of the night (after a file is ready) to create a customized workbook (multiple sheets, pivot tables, charts, filters, outlines, custom formatting, etc.). Currently, the macro is fired up manually the next day. Furthermore, it needs to run unattended on a server (laptop goes home at night!). Anybody successfully do something like this? Please, no Unix-side hacks (e.g., Perl modules) - need full access to VBA features, including database functions. Thanks!
Well you have some options.
First, for all Excel has to be installed on server.
Then you create a sheduled task to call a program.
In this case you can write e.g. a vbscript or .NET program to call the app, load the document and starts its content (your VBA). That should work at all.
Or you move the VBA code to a program and target Excel with your code, but prolly more work.
If you do this with .NET you have prolly best success. e.g. you can add an eventlog for successful run, etc.
If you can leave Excel running on the server all the time, you can use Application.OnTime to schedule the next runs of a particular macro (once it's run, reschedule another in the macro code). When I worked in banking we used this all the time to run night-time jobs.
If you cannot leave Excel running, I have to say you may be in a world of pain. It's possible to start Excel using an AT job (scheduled task) but you may have headaches getting it to run under the correct user privileges and if you use any addins you'll experience regular disasters where they failed to load and stopped Excel from starting up. At the end of the day, Excel isn't really meant to be run on servers (it's actually a violation of the terms of use) and starting/running/stopping it is not going to be a reliable system even if you do get it to work.

Resources