-
I have a program that opens excel and a certain template through a shell statement, and then exports text form textboxes into certain cells of excell. Everything worked great in trial runs until I compiled it and put people on the program. It ends up that if Excel is already open then the shell opens a second Excel and the template, butinstead of writting to the template version of Excel it writes to the first version that was already open. Is there a way to detect if Excel is already open or maybe a diiferent way to open the template. I am fairly new to VB and don't know all of the tricks. If the code is needed let me know and I will provide it. I just thought it was probably too long for a forum.
Thanks for any help
MBStumped
-
You can use the GetObject function to get the active instance of Excel. If no instance is found, an error is returned.
Code:
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
'Excel is not currently running
Err.Clear ' Clear Err object
Else
'Excel is running
End If
On Error GoTo ErrHndlr 'Send future errors to your error handling routine
Or, you can use the FindWindow API function.
Code:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
' If Excel is running this API call returns its handle.
hwnd = FindWindow("XLMAIN", 0)
If hwnd <> 0 Then ' Excel is running.
MsgBox "Please Close the Microsoft Excel Application Before Running This Program."
End If
Hope this helps!