Hi
I'm trying to run a bit of code in Access 2007 on Excel 2007. Its supposed to open 2 workbooks, change the names of the sheets, copy one sheet to the other workbook, save it with a new name then close.
My problem is it works... once. Run it again and I get errors (Run time error 462 at the Move statement), . Run it again and it works - rinse and repeat. Any ideas anyone?
I'm trying to run a bit of code in Access 2007 on Excel 2007. Its supposed to open 2 workbooks, change the names of the sheets, copy one sheet to the other workbook, save it with a new name then close.
My problem is it works... once. Run it again and I get errors (Run time error 462 at the Move statement), . Run it again and it works - rinse and repeat. Any ideas anyone?
Code:
Private Sub cmdATNormal_Click()
On Error GoTo ErrorTrap
On Error Resume Next
Dim strLocation1 As String, strLocation2 As String, strLocation As String
Dim appExcel As Object
Dim myWorkbook As Object
strLocation1 = Me.txtFolder & "\ATXFOC.xlsx"
strLocation2 = Me.txtFolder & "\AT_FOC.xlsx"
fPause (10) 'if I don't pause I get errors!
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open("" & strLocation1 & "")
Set myWorkbook = appExcel.Workbooks.Open("" & strLocation2 & "")
appExcel.Visible = True
With appExcel
.Windows("ATXFOC.xlsx").Activate
.Sheets("qryDummy").Activate
.Sheets("qryDummy").Select
.Sheets("qryDummy").Name = "ATXFOC"
.Windows("AT_FOC.xlsx").Activate
.Sheets("qryDummy").Activate
.Sheets("qryDummy").Select
.Sheets("qryDummy").Name = "AT_FOC"
.Sheets("AT_FOC").Move After:=Workbooks("ATXFOC.xlsx").Sheets(1)
.Windows("ATXFOC.xlsx").Activate
.ActiveWorkbook.SaveAs Filename:="" & Me.txtFolder & "\AssayTracker.xlsx", FileFormat:=51, CreateBackup:=True
.Windows("AssayTracker.xlsx").Close False
End With
Set appExcel = Nothing
Set myWorkbook = Nothing
ErrorTrap:
Select Case Err.Number
Case Is = 0 'no errors
'Do nothing
Case Else
MsgBox "Database Error #: " & Err.Number & vbCrLf & Err.Description
End Select
End Sub