azizrasul
Well-known Member
- Joined
- Jul 7, 2003
- Messages
- 1,304
- Office Version
- 365
- 2019
- 2016
- Platform
- Windows
I have the following code in a module.
Public Function Excel_Export(strFileName As String)
Dim dbs As Database
Dim strMessage As String
Dim intX As Integer
Dim strSQL As String
Dim xlApp As Excel.Application
Dim xlBook_S As Excel.Workbook, xlBook_F As Excel.Workbook
Dim xlSheet_S As Excel.Worksheet, xlSheet_F As Excel.Worksheet
Dim intColumnCountS As Integer, intColumnCountF As Integer
Dim lngRowCountS As Long, lngRowCountF As Long
Dim objXL As Object
Dim objActiveWkb As Excel.Workbook
Set dbs = CurrentDb
'~~~Supplied by contributor on this site~~~
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")
'What did we find?...
If Not TypeName(objXL) = "Empty" Then
strMessage = "Excel Running."
Set objActiveWkb = objXL.Application.ActiveWorkbook
objXL.Visible = True
objActiveWkb.Close
objXL.Application.Quit
Set objActiveWkb = Nothing
Set objXL = Nothing
Else
strMessage = "Excel Not Running."
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set xlApp = CreateObject("Excel.Application")
Set xlBook_S = xlApp.Workbooks.Open(strFileName, ReadOnly:=False)
Set xlSheet_S = xlBook_S.Worksheets(1)
xlApp.Workbooks(1).Activate 'This is better as you can specify the workbook.
xlApp.Visible = True 'Makes the MS Excel application visible.
intColumnCountS = xlSheet_S.UsedRange.Columns.Count
lngRowCountS = xlSheet_S.UsedRange.Rows.Count
For intX = 1 To intColumnCountS
xlBook_S.Worksheets(1).Columns(intX).AutoFit
Next
With xlApp
.Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Columns("AQ:AQ").Select
.Selection.Font.ColorIndex = 3
.Range("A1").Select
End With
xlApp.DisplayAlerts = False
xlBook_S.SaveAs strFileName
xlBook_S.Close SaveChanges:=True
xlApp.Application.Quit
Set xlSheet_S = Nothing
Set xlBook_S = Nothing
Set xlApp = Nothing
End Function
If I leave the bit of code within the ~~~~~ section, the With ... End With section does not work. If I delete the ~~~~~ section, the With... End With section does work but each alternative occasion i.e. the Excel application iremains open in the background and I have to delete from the task manager.
Can anyone to resolve this for me.
Public Function Excel_Export(strFileName As String)
Dim dbs As Database
Dim strMessage As String
Dim intX As Integer
Dim strSQL As String
Dim xlApp As Excel.Application
Dim xlBook_S As Excel.Workbook, xlBook_F As Excel.Workbook
Dim xlSheet_S As Excel.Worksheet, xlSheet_F As Excel.Worksheet
Dim intColumnCountS As Integer, intColumnCountF As Integer
Dim lngRowCountS As Long, lngRowCountF As Long
Dim objXL As Object
Dim objActiveWkb As Excel.Workbook
Set dbs = CurrentDb
'~~~Supplied by contributor on this site~~~
On Error Resume Next
Set objXL = GetObject(, "Excel.Application")
'What did we find?...
If Not TypeName(objXL) = "Empty" Then
strMessage = "Excel Running."
Set objActiveWkb = objXL.Application.ActiveWorkbook
objXL.Visible = True
objActiveWkb.Close
objXL.Application.Quit
Set objActiveWkb = Nothing
Set objXL = Nothing
Else
strMessage = "Excel Not Running."
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set xlApp = CreateObject("Excel.Application")
Set xlBook_S = xlApp.Workbooks.Open(strFileName, ReadOnly:=False)
Set xlSheet_S = xlBook_S.Worksheets(1)
xlApp.Workbooks(1).Activate 'This is better as you can specify the workbook.
xlApp.Visible = True 'Makes the MS Excel application visible.
intColumnCountS = xlSheet_S.UsedRange.Columns.Count
lngRowCountS = xlSheet_S.UsedRange.Rows.Count
For intX = 1 To intColumnCountS
xlBook_S.Worksheets(1).Columns(intX).AutoFit
Next
With xlApp
.Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
.Columns("AQ:AQ").Select
.Selection.Font.ColorIndex = 3
.Range("A1").Select
End With
xlApp.DisplayAlerts = False
xlBook_S.SaveAs strFileName
xlBook_S.Close SaveChanges:=True
xlApp.Application.Quit
Set xlSheet_S = Nothing
Set xlBook_S = Nothing
Set xlApp = Nothing
End Function
If I leave the bit of code within the ~~~~~ section, the With ... End With section does not work. If I delete the ~~~~~ section, the With... End With section does work but each alternative occasion i.e. the Excel application iremains open in the background and I have to delete from the task manager.
Can anyone to resolve this for me.