I'm using Excel 2003 SP2 & Access SP2 on a Windows XP machine.
Our problem is when a user has an instance of Excel open prior to running our process we get error 9 "Subscript out of range" when the code tries to move the "FAR Removal Trend.xls" sheet to our template. Our process is as follows, we send the data from Access using a DoCmd.OutputTo method (the first set of code). We then open the template and the code in the template (second set of code) moves the "FAR Removal Trend.xls" worksheet into the template were we then process the data. Everthing works fine when there are no instances of Excel open prior to starting the process. However when an instance of Excel is already open the "FAR Removal Trend.xls" is opened in one instance and the template is opened in another instance. The code in the template can not see the "FAR Removal Trend.xls" workbook and sets error 9. I have been trying to figure out if I can use API (FindWindowEx in the user32 lib) to resolve this issue. No luck so far. Any help would be greatly appreciated.
Our problem is when a user has an instance of Excel open prior to running our process we get error 9 "Subscript out of range" when the code tries to move the "FAR Removal Trend.xls" sheet to our template. Our process is as follows, we send the data from Access using a DoCmd.OutputTo method (the first set of code). We then open the template and the code in the template (second set of code) moves the "FAR Removal Trend.xls" worksheet into the template were we then process the data. Everthing works fine when there are no instances of Excel open prior to starting the process. However when an instance of Excel is already open the "FAR Removal Trend.xls" is opened in one instance and the template is opened in another instance. The code in the template can not see the "FAR Removal Trend.xls" workbook and sets error 9. I have been trying to figure out if I can use API (FindWindowEx in the user32 lib) to resolve this issue. No luck so far. Any help would be greatly appreciated.
Code:
Private Sub Trend_Curve_Click()
Dim xlApp As Object
Dim strDefaultDir As String
Set xlApp = CreateObject("Excel.Application")
strDefaultDir = Application.GetOption("Default Database Directory")
strDefaultDir = strDefaultDir & "\FAR Removal Trend.xls"
On Error Resume Next
Kill (strDefaultDir)
On Error GoTo Error_Handler
If MTBF_PLOT = 0 And MTBR_PLOT = 0 Then
MsgBox "Type of plot not selected, please select MTBF and/or MTBR plot.", vbCritical, "Error"
Else
DoCmd.OutputTo acQuery, "FAR Removal Trend", "MicrosoftExcel03(*.xls)", "", True, ""
Set xlApp = GetObject(, "Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Add Template:= _
"\\MARNV005\Rms-C130J\Apps\APPS\FRACAS\Templates\060927_Universal Trend Template.xlt"
xlApp.Run "'060927_Universal Trend Template1'!Universal_Trend.Universal_Trend"
End If
Exit_Procedure:
Exit Sub
Error_Handler:
If Err.Number = 2501 Or 440 Then
Resume Exit_Procedure
ElseIf Err.Number = 2302 Then
MsgBox "An error has occurred in this application. " _
& vbCrLf & vbCrLf & "Please close the Excel file before running the " _
& vbCrLf & "requested trend chart.", _
Buttons:=vbCritical, TITLE:="DMT Error"
Else
MsgBox "An error has occurred in this application. " _
& "Please contact your technical support person and " _
& "tell them this information:" _
& vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " _
& Err.DESCRIPTION, _
Buttons:=vbCritical, TITLE:="DMT Error"
Resume Exit_Procedure
Resume
End If
Error [(errornumber)]
End Sub
Code:
'===============================================
'Moves Query - Trend data to template workbook
'===============================================
Application.ScreenUpdating = False
On Error GoTo Error_Handler
Windows("FAR Removal Trend.xls").Activate
Sheets("FAR Removal Trend").Move After:=Workbooks( _
"060927_Universal Trend Template1").Sheets("Pie Chart Data")
Error_Handler:
If Err.Number = 9 Then
MsgBox "An error has occurred in this application. " _
& vbCrLf & vbCrLf & "Please close all instances of Excel before running " _
& vbCrLf & "the requested trend chart.", _
Buttons:=vbCritical, Title:="DMT Error"
Application.DisplayAlerts = False
ActiveWindow.Close
Application.DisplayAlerts = True
Exit Sub
Else
MsgBox "An error has occurred in this application. " _
& "Please contact your technical support person and " _
& "tell them this information:" _
& vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " _
& Err.Description, _
Buttons:=vbCritical, Title:="DMT Error"
Resume Exit_Procedure
Resume
End If