Look to see if Excel is open or not

NickJ

Member
Joined
Apr 29, 2005
Messages
21
Programming Experience
Beginner
Hi People

I have a small problem

I have a VB program that exports some values to a Excel Sheet, now if I open the Sheet to have a look or work on it and I then open the VB Program and try to export the values it opens it again. How do I write the code so the VB program looks to see if the Excel Sheet is open and switches to it?

Thanks People
 
Look at the System.Diagnostics.Process class to get a list of running processes. To activate a particular window, you can use the AppActivate function. This is in the Microsoft.VisualBasic namespace, though. I haven't found a "pure .NET" method myself, although I believe it can be done using APIs as well.
 
still cant get it to work

Ok I have been playing around with it but I still cant get it to work.

May be I didn’t explain it probably

If i open my Excel file manually and then run my .net application, my application will create another Excel instance instead of connecting to the already open Excel instance.

What’s the code to get around this little dilemma


Thanks People
 
This code is not fool-proof because it does not account for two files with the same name in different folders, but it's a start:
VB.NET:
	Private Sub OpenFileInExcel(ByVal fileName As String)
		For Each runningProcess As Process In Process.GetProcessesByName("Excel")
			If runningProcess.MainWindowTitle.IndexOf(IO.Path.GetFileName(fileName)) <> -1 Then
				AppActivate(runningProcess.Id)
				Return
			End If
		Next

		Process.Start(fileName)
	End Sub
 
Hi and thanks for that but....

As im a beginner, how do i but that in the button procedure?

Thanks for your time on this
 
btw, why you don't use a named mutex instead?

System.Threading.Mutex

if they're given a name, they're global to the system. just open the mutex when you start the app - if it's already held, you have another instance of the app running

You could find some good examples about Mutex ... google for it

Cheers ;)
 
I'm not aware of a way to take control of an existing instance of Excel. Perhaps you should tell your user that the file is open and they need to close it before your app can perform the desired operation.
 
There are a number of ways to do this. We already mentioned couple of them, bit here is few more scenarios:

  1. Your app open a document and writes the filename to a specific folder, say C:\FilesOpen. Subsequent process runs of your app checks this folder for the filename and if present says "Sorry, you already have this doc open".
  2. Create a reg key where you store "Currently open docs". Again us the same method as above to check if a previous process has already opened the doc in question
  3. If your app opens a doc, set the window's caption with a specific title including the doc's name. Then subsequent app processes cycle thru all open windows checking for your app name and the doc name
Of the above, I would use the last option as it will be valid even if previous instances of your app crash (thus either leaving filenames in the FilesOpen folder or registry entries behind).

Cheers ;)
 
Back
Top