J
Jordan
Below is code that has been untouched and working for years. All it does is
opens Excel, creates a workbook and pastes the data on the clipboard onto
the worksheet. On one particular computer I am having a problem when it
gets to the line objXL.Application.Visible . I get the following error:
"Error -2147417851: Method 'Application' of object '_Application' failed"
What is funny is that I can get this to work if I step through the code in
the Debug window only when I have the Locals window open as well and explor
the objXL properties right down to objXL.Application.Visible and then the
rest of the code executes fine. If I don't drill down through the
properties in the Locals window, the code gives the error.
Here is the code:
Sub sTestXL()
Dim objXL As Object
Dim objActiveWkb As Object
On Error GoTo HandleErr
Set objXL = CreateObject("Excel.Application")
objXL.Application.Visible = True
objXL.Application.Workbooks.Add
Set objActiveWkb = objXL.Application.ActiveWorkbook
objActiveWkb.Application.ActiveSheet.Paste
objActiveWkb.Application.ActiveSheet.Cells.EntireColumn.AutoFit
ExitHere:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "Module1.sTestXL"
End Select
End Sub
opens Excel, creates a workbook and pastes the data on the clipboard onto
the worksheet. On one particular computer I am having a problem when it
gets to the line objXL.Application.Visible . I get the following error:
"Error -2147417851: Method 'Application' of object '_Application' failed"
What is funny is that I can get this to work if I step through the code in
the Debug window only when I have the Locals window open as well and explor
the objXL properties right down to objXL.Application.Visible and then the
rest of the code executes fine. If I don't drill down through the
properties in the Locals window, the code gives the error.
Here is the code:
Sub sTestXL()
Dim objXL As Object
Dim objActiveWkb As Object
On Error GoTo HandleErr
Set objXL = CreateObject("Excel.Application")
objXL.Application.Visible = True
objXL.Application.Workbooks.Add
Set objActiveWkb = objXL.Application.ActiveWorkbook
objActiveWkb.Application.ActiveSheet.Paste
objActiveWkb.Application.ActiveSheet.Cells.EntireColumn.AutoFit
ExitHere:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "Module1.sTestXL"
End Select
End Sub