format of excel export (1 Viewer)

zonexs123

Registered User.
Local time
Tomorrow, 02:41
Joined
Feb 6, 2011
Messages
39
Hello,

I have a form where there is a button which export my query "qryReportsBureauT" to excel asking user to save the excel file first than it will open automatically.

My problem is that the export data in excel file doesn't look like in good format. What I want that the format should be pre-defined. ie. Header should be bold and colored. Other data font type, size and autofit should be coded into VBA so it could look better and save user time to format it again and again.

Here is my code which work fine:

Private Sub Command141_Click()
On Error GoTo Err_btnlogin_Click
Dim stDocName As String
stDocName = "qryReportsBureauT" 'my query'
'DoCmd.OpenQuery stDocName, acNormal, acEdit
Const QueryName As String = "qryReportsBureauT"
Const SheetType As Byte = acSpreadsheetTypeExcel9
MsgBox "Save your report...Wait for few seconds...Report will open automatically !!!"
DoCmd.OutputTo ObjectType:=acOutputQuery, ObjectName:="qryReportsBureauT", _
OutputFormat:=acFormatXLSX, AutoStart:=True
Exit_btnlogin_Click:
Exit Sub
Err_btnlogin_Click:
MsgBox Err.Description
Resume Exit_btnlogin_Click
End Sub


I would be thankful for your help !!!


Santosh
 

Rank Am

Registered User.
Local time
Tomorrow, 05:11
Joined
Apr 30, 2005
Messages
68
If you want to manipulate XL, your best bet is to add the Microsoft Excel library in the vba editor (tools menu - References) and define Excel as an object in vba - this allows you to control formating, fonts etc from Access.
First step is to create a recordset (either DAO or ADO)
Second create an instance of excel
third Use the CopyFromRecordset method to get the recordset into an XL range
Forth mess about with formatting to your hearts deire
Code:
'Declare variables
Dim rs as ADODB.Recordset

'Some useful excel objects
Dim objXL As Excel.Application
Dim objWKBK As Excel.Workbook
Dim objWKSHT As Excel.Worksheet
Dim objRNG As Excel.Range

'Step 1 - generate a recordset from "qryReportsBureauT"
'You have not provided enough information - if qryReportsBureauT contains 'parameters or doesn't return every record in the table this will fail 
Set rs = adodb.recordset
With rs
    .ActiveConnection = CurrentProject.Connection
    .CursorType = adReadOnly
    .LockType = adLockOptimistic
    .Open "qryReportsBureauT", , , , adCmdTableDirect
End With

'Step 2 - Generate an instance of excel from access
Set objXL = CreateObject("excel.application")
Set objWKBK = objXL.Workbooks.Add()
Set objWKSHT = objWKBK.Worksheets("Sheet1")
'
'Step3 copy the data to excel and get rid of the recordset in this example 'copying it to cell A2 but you can make the range as big or small as you want
'You can use the recordcount property of the recordset to do this dynamically

objWKSHT.Range("A2").CopyFromRecordset rs
rs.close
set rs = nothing
objXL.Visible = True

'Step 4 - There is a pretty good article on the msdn on how to apply 'formatting to an excel range object

http://msdn.microsoft.com/en-us/library/aa139976(v=office.10).aspx
 

zonexs123

Registered User.
Local time
Tomorrow, 02:41
Joined
Feb 6, 2011
Messages
39
Thanks for your help !! Let me check and try whether its going to be work or not. Keep you updated on the same... Thanks again :)
 

Users who are viewing this thread

Top Bottom