tonyflavell
Programmer
I export a query from Access to an Excel sheet using "OutPutTo" and then use formatting commands to format the sheet. The problem is that the first time I export the query it remains unformatted, if I then repeat the export it formats perfectly however many times I run the query until Access is closed. Any ideas anyone?
Code used below:
+++++++++++++++++++++++++++++++++
Private Sub Export_Click()
+++++++++++++++++++++++++++++++++
On Error Resume Next
If IsNull(zOverUnderUsages) Then
MsgBox "No Data to display", , "No Data"
Exit Sub
Else
DoCmd.OutputTo acOutputQuery, "zOverUnderUsages", acFormatXLS, "C:\Program Files\Paper Database RT\OverUnderUsages.xls", True
Dim objXL As Object
Dim ExcelWasNotRunning As Boolean
Set objXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear
DetectExcel
Set objXL = GetObject("C:\Program Files\Paper Database RT\OverUnderUsages.xls")
objXL.Visible = True
objXL.Parent.Windows(1).Visible = True
'Formatting Commands here
End If
Set objSheet = Nothing
Set objXL = Nothing
End Sub
+++++++++++++++++++++++++++++++
Sub DetectExcel()
+++++++++++++++++++++++++++++++
Const WM_USER = 1024
Dim hWnd As Long
hWnd = FindWindow("XLMain", 0)
If hWnd = 0 Then
Exit Sub
Else
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub
+++++++++++++++++++++++++++++++
Declarations
+++++++++++++++++++++++++++++++
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWind As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Code used below:
+++++++++++++++++++++++++++++++++
Private Sub Export_Click()
+++++++++++++++++++++++++++++++++
On Error Resume Next
If IsNull(zOverUnderUsages) Then
MsgBox "No Data to display", , "No Data"
Exit Sub
Else
DoCmd.OutputTo acOutputQuery, "zOverUnderUsages", acFormatXLS, "C:\Program Files\Paper Database RT\OverUnderUsages.xls", True
Dim objXL As Object
Dim ExcelWasNotRunning As Boolean
Set objXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear
DetectExcel
Set objXL = GetObject("C:\Program Files\Paper Database RT\OverUnderUsages.xls")
objXL.Visible = True
objXL.Parent.Windows(1).Visible = True
'Formatting Commands here
End If
Set objSheet = Nothing
Set objXL = Nothing
End Sub
+++++++++++++++++++++++++++++++
Sub DetectExcel()
+++++++++++++++++++++++++++++++
Const WM_USER = 1024
Dim hWnd As Long
hWnd = FindWindow("XLMain", 0)
If hWnd = 0 Then
Exit Sub
Else
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub
+++++++++++++++++++++++++++++++
Declarations
+++++++++++++++++++++++++++++++
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWind As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long