Hi,
I have used a procedure to transfer data to Excel from Access. I'm using the Excel library from Access. This has worked fine ubtil I added a new sheet to the workbook. Everything works as it should. When I close down Excel, (Which is started when the transfer is in progress) I'm asked to save the file which I do. All sheets is showing in the workbook when Excel closes. If I now go and open the file again, Excel is opened but no workbook shows up. However, if I go to View and select Full Screen, they are displayed. When I close the 'Full screen', it is normal with toolbars etc and the workbook is there. I post the code that is run from Access. Can anyone please tell me what's making this behaviour?
I have used a procedure to transfer data to Excel from Access. I'm using the Excel library from Access. This has worked fine ubtil I added a new sheet to the workbook. Everything works as it should. When I close down Excel, (Which is started when the transfer is in progress) I'm asked to save the file which I do. All sheets is showing in the workbook when Excel closes. If I now go and open the file again, Excel is opened but no workbook shows up. However, if I go to View and select Full Screen, they are displayed. When I close the 'Full screen', it is normal with toolbars etc and the workbook is there. I post the code that is run from Access. Can anyone please tell me what's making this behaviour?
Code:
Sub XLHTransfer(Fname As String, XLTmp As String, FDato As Date, TDato As Date)
Dim FExist As Boolean
Dim Response As Integer
Dim J As Integer
Dim I As Integer
Dim CritCID As Long
Dim FD As String
Dim TD As String
Dim strInfo As String
Dim B As Boolean
Dim K As Integer
Dim P(9) As Integer 'Variabel for Celleposisjoner i Excel-arket
Dim CurrentValue As Variant
Dim CurrentField As Variant
Dim xlApp As Excel.Application
Dim ExcelRunning As Boolean
Dim Sht As Excel.Worksheet
Dim Wbk As Excel.Workbook
Dim CurCell As Object
Dim rst0 As Recordset
Dim rst As Recordset
Dim rst2 As Recordset
Dim dbs As Database
P(0) = 1: P(1) = 2: P(2) = 6: P(3) = 7: P(4) = 8
Set dbs = CurrentDb
strInfo = "Prosesserer data for Hydro" & vbCrLf & vbCrLf _
& "Dette kan ta noe tid, vennligst vent....."
DoCmd.OpenForm "frmMessageBox", acNormal, , , acFormEdit, acWindowNormal, strInfo
DoCmd.Hourglass True
DoCmd.RepaintObject acForm, "frmMessageBox"
FD = DatePart("m", FDato) & "/" & DatePart("d", FDato) & "/" & DatePart("yyyy", FDato)
TD = DatePart("m", TDato) & "/" & DatePart("d", TDato) & "/" & DatePart("yyyy", TDato)
Set rst = dbs.OpenRecordset("SELECT * FROM H19HeaderLineToExcel " _
& "WHERE Dato >= #" & FD & "# And Dato <= #" & TD & "#;")
If (rst.BOF And rst.EOF) Then
DoCmd.Close acForm, "frmMessageBox", acSaveNo
DoCmd.Hourglass False
MsgBox ("Der er ingen data å overføre for valgte periode!")
Else
rst.MoveLast: rst.MoveFirst
'***************Init Excel******
ExcelRunning = IsExcelRunning()
If ExcelRunning Then
Set xlApp = GetObject(, "Excel.Application")
Else
Set xlApp = CreateObject("Excel.Application")
End If
With xlApp
.Visible = True
.WindowState = xlMaximized
.Cursor = xlWait
If fIsFileDIR(Fname) <> 0 Then
Set Wbk = .Workbooks.Open(Filename:=Fname, _
ReadOnly:=False, AddToMru:=False)
Else
Dim NewCID As Long
Dim CurrentCID As Long
Set Wbk = .Workbooks.Add(XLTmp)
Wbk.SaveAs Fname
End If
Set Sht = Wbk.Sheets("Produksjonsdata")
Sht.Select
'***********Datatransfer************************
Sht.Unprotect ("emerhoff")
Sht.Cells(3, 2).Value = Format(Now(), "dd.mm.yyyy kl. hh:nn")
Sht.Cells(4, 2).Value = Format(FDato, "dd.mm.yyyy")
Sht.Cells(5, 2).Value = Format(TDato, "dd.mm.yyyy")
J = 9
rst.MoveFirst
Do While Not rst.EOF
For K = 0 To 4
CurrentField = rst(K)
Sht.Cells(J, P(K)).Value = CurrentField
Next K
CritCID = rst!CID
Set rst2 = dbs.OpenRecordset("SELECT * FROM H25Drossdetaljer " _
& "WHERE CID=" & CritCID & ";")
rst2.MoveLast
rst2.MoveFirst
Do While Not rst2.EOF
For I = 0 To rst2.Fields.Count - 1
CurrentField = rst2(I)
Sht.Cells(J, I + 2).Value = CurrentField
Next I
rst2.MoveNext
J = J + 1
Loop
rst.MoveNext
Loop
rst2.Close
'************Analyser************************
AnalysisUpdate
Set Sht = Wbk.Sheets("Analyser")
Sht.Select
Sht.Unprotect ("emerhoff")
J = 3
rst.MoveFirst
Do While Not rst.EOF
CritCID = rst!CID
Set rst2 = dbs.OpenRecordset("SELECT * FROM H30Analyser " _
& "WHERE CID=" & CritCID & ";")
If Not (rst2.BOF And rst2.EOF) Then
rst2.MoveLast
rst2.MoveFirst
Do While Not rst2.EOF
If IsNull(rst2!SI) Then
Sht.Cells(J, 1).Value = rst2!TCID
Sht.Cells(J, 2).Value = "NoA"
Else
For I = 0 To rst2.Fields.Count - 2
CurrentField = rst2(I)
Sht.Cells(J, I + 1).Value = CurrentField
Next I
End If
rst2.MoveNext
J = J + 1
Loop
Else
Sht.Cells(J, I).Value = "NoA"
J = J + 1
End If
rst.MoveNext
Loop
rst.Close: rst2.Close
'**************Drossbevegelser**********
Set Sht = Wbk.Sheets("Drossleveranser")
Sht.Select
Set rst2 = dbs.OpenRecordset( _
"SELECT * " & _
"FROM qselDrossbevegelser " & _
"WHERE " & _
"TI >= #" & FD & "# And TI <= #" & TD & "#;")
J = 4
If Not rst2.EOF Then
rst2.MoveLast: rst2.MoveFirst
End If
Do While Not rst2.EOF
For I = 0 To rst2.Fields.Count - 1
CurrentField = rst2(I)
If I = 2 And rst2!VLEVERT < 0 Then
Range(Cells(J, 4), Cells(J, 6)).Interior.ColorIndex = 3
Range(Cells(J, 4), Cells(J, 6)).Interior.Pattern = xlSolid
Range(Cells(J, 4), Cells(J, 6)).Interior.PatternColorIndex = xlAutomatic
End If
Sht.Cells(J, I + 1).Value = CurrentField
Next I
rst2.MoveNext
J = J + 1
Loop
rst2.Close
'**************Ferdig**********************
DoCmd.Close acForm, "frmMessageBox", acSaveNo
DoCmd.Hourglass False
Set Sht = Wbk.Sheets("Produksjonsdata")
Sht.Select
Sht.Range("A1").Select
.Cursor = xlDefault
'Sht.Protect ("emerhoff")
Set Sht = Nothing
Set Wbk = Nothing
Set xlApp = Nothing
End With
End If
End Sub