B
B. Meincke
I'm afraid I just don't understand why the following code runs without error
the first time, but returns an "Object variable or With block variable not
set" error on the split when run for a second time (the Excel file exists).
Can anyone shed some light on what I am doing wrong?
Thanks in advance.
***************************
Private Sub cmdExport_Click()
Dim objExcel As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim strFile As String
Dim strDest As String
Dim strMyDB As String
strMyDB = CurrentDb.Name
strPath = Left(strMyDB, LastInStr(strMyDB, "\"))
strFile = strPath & "This week's Open Houses.xls"
If Dir(strFile) <> "" Then
Kill strFile
End If
Sleep 3000
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"qryOHLReview", strFile, True
Set objExcel = New Excel.Application
objExcel.Visible = True
Set xlWB = objExcel.Workbooks.Open(strFile)
Set xlWS = xlWB.ActiveSheet
With xlWS
.Range("A1:R1").Font.Bold = True
.Range("A1:R1").HorizontalAlignment = xlCenter
.Range("A1:R1").Font.ColorIndex = 2
.Range("A1:R1").Interior.ColorIndex = 1
.Range("A:R").HorizontalAlignment = xlLeft
.Range("E:E").NumberFormat = "$#,##0.00"
.Range("E:E").HorizontalAlignment = xlRight
.Range("A:R").Columns.AutoFit
.Range("2:2").Select
End With
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
xlWS.Range("A1:A1").Select
Set objExcel = Nothing
Set xlWB = Nothing
Set xlWS = Nothing
End Sub
********************************
the first time, but returns an "Object variable or With block variable not
set" error on the split when run for a second time (the Excel file exists).
Can anyone shed some light on what I am doing wrong?
Thanks in advance.
***************************
Private Sub cmdExport_Click()
Dim objExcel As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim strFile As String
Dim strDest As String
Dim strMyDB As String
strMyDB = CurrentDb.Name
strPath = Left(strMyDB, LastInStr(strMyDB, "\"))
strFile = strPath & "This week's Open Houses.xls"
If Dir(strFile) <> "" Then
Kill strFile
End If
Sleep 3000
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,
"qryOHLReview", strFile, True
Set objExcel = New Excel.Application
objExcel.Visible = True
Set xlWB = objExcel.Workbooks.Open(strFile)
Set xlWS = xlWB.ActiveSheet
With xlWS
.Range("A1:R1").Font.Bold = True
.Range("A1:R1").HorizontalAlignment = xlCenter
.Range("A1:R1").Font.ColorIndex = 2
.Range("A1:R1").Interior.ColorIndex = 1
.Range("A:R").HorizontalAlignment = xlLeft
.Range("E:E").NumberFormat = "$#,##0.00"
.Range("E:E").HorizontalAlignment = xlRight
.Range("A:R").Columns.AutoFit
.Range("2:2").Select
End With
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
xlWS.Range("A1:A1").Select
Set objExcel = Nothing
Set xlWB = Nothing
Set xlWS = Nothing
End Sub
********************************