Link to home
Create AccountLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access VBA manipulate Excel object

Hi

What Access VBA code would I use to create an Excel object, push data to a sheet in it
and then format certain ranges?

Thanks
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Hi Murray
I posted the code on the previous question :  https://www.experts-exchange.com/questions/29166288/Access-Push-data-to-Excel.html?anchorAnswerId=42991565#a42991565
Take it for a spin and see what is missing ...
Avatar of Murray Brown

ASKER

Thanks John. Will do
Hi John. Is there by any chance a more simple example. I am finding that code a bit tricky
Hi Murray...you are right...this is the *Complex* edition... :)
This is the simple one
Public Sub ExportToExcel(frm As Form)
Dim rst As dao.Recordset
    Const StartRow = 3
    Dim MaxRow As Integer
    Dim objXL As Object
    Dim objWkb As Object
    Dim objSht As Object
    Dim FieldNames() As String
    Dim X As Integer
    Dim strSQL As String
    Dim strFields As String
    Dim ctl As Control
    Dim ExcelPath as String
    
    
    
    Set rst = frm.RecordsetClone
    rst.MoveLast
    rst.MoveFirst
    MaxRow = rst.RecordCount
    
    ReDim FieldNames(rst.Fields.Count)
    For X = 0 To rst.Fields.Count - 1
       FieldNames(X + 1) = Nz(rst.Fields(X).NAME, "N.A")
    Next


Set objXL = CreateObject("Excel.Application")
    With objXL
        .Visible = True
        
        Set objWkb = .Workbooks.Add

        Set objSht = objWkb.Worksheets(1)
        objSht.NAME = frm.Name

        With objSht
        


            .Range(.Cells(StartRow, 2), .Cells(StartRow + MaxRow, 2)) _
                    .CopyFromRecordset rst
                    For X = 0 To rst.Fields.Count
                    .Cells(1, 1 + X).Value = Nz(FieldNames(X), "N.A")
                    Next X
        End With
        .Columns.autofit
    End With
ExcelPath = Application.CurrentProject.Path & "\" & frm.Name & Format(Date, "mmm-dd-yy") & ".xlsx"

objXL.DisplayAlerts = False
objWkb.SaveAs ExcelPath , AccessMode:=3
objWkb.Close

objWkb.Close
objXL.Quit


Set rst = Nothing

Exit_cmdExcel_Click:
    Exit Sub

Err_cmdExcel_Click:
    MsgBox Err.Description
    Resume Exit_cmdExcel_Click
End Sub

Open in new window

I found the following code. Instead of specifying a path is it possible to just generate  a new Excel file?

Sub ToExcel()


    Dim ExceApp As Object
    Dim wbk As Object
    Dim rst As Recordset
    Set ExcelApp = CreateObject("Excel.Application")
    Set wbk = ExcelApp.workbooks.Open("Your Excel Workbook Path")
    Set rst = CurrentDb.OpenRecordset("Select * from t_Arms")
    wbk.worksheets(1).range("a1").copyfromrecordset rst
    wbk.Save
    wbk.Close
    ExcelApp.Quit
    Set rst = Nothing
    Set wbk = Nothing
    Set ExcelApp = Nothing

End Sub

Open in new window

Take a look at the "simpler" edition
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Daniel Pineault
Daniel Pineault

Murray,

you may like to review https://www.devhut.net/2017/03/15/ms-access-vba-export-recordset-to-excel/ to see an example of various elements of Excel automation.
thanks very much