Question : Exporting to excel and formatting the spreadsheet from VB

Hi All,

I need your assistance once again. I have the following code below which exports queries (from MS Access) to a spreadsheet and names each sheet accordingly.

Is it possible to bold the field names, give it a white fill colour and wrap text for each cell in the spreadsheet? This will apply to all sheets.

Some of the text on the cells is long and I need to wrap text the cell and do additional formatting to the spreadsheet to be more presentable.

Can any one help please.


Thanks

Private Sub Command9_Click()
if dcount("*", "query1") > 0 then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query1", "C:\Temp\" & Forms.Form1.dropdown1 & ".xls", , "Query1"
End if

if dcount("*", "query2") > 0 then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query2", "C:\Temp\" &Forms.Form1.dropdown1 & ".xls", , "Query2"
End if

if dcount("*", "query3") > 0 then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query3", "C:\Temp\" & Forms.Form1.dropdown1 & ".xls", , "Query3"
End if

End Sub

Answer : Exporting to excel and formatting the spreadsheet from VB

Hello jose11au

You can do this all from within Access. It's best if you add a reference to the "Microsoft Excel ?.?? Object Library" from VB, "Tools / References". Then, once you are done exporting:


Sub GoGo()

    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet

    ' your exportation here...
    ' ....

    ' now format one sheet as example:

    Set xlBook = GetObject("C:\Temp\" &Forms.Form1.dropdown1 & ".xls")
    Set xlSheet = xlBook.Worksheets("Query2")
    ' titles
    With xlSheet.Range("1:1")
        .Interior.Color = vbWhite
        .Font.Bold = True
    End With
    ' formatting
    With xlSheet.Cells
        .ColumnWidth = 20
        .WrapText = True
        .EntireRow.AutoFit
        .EntireColumn.AutoFit
    End With
    ' correct automation bug:
    xlBook.Application.Windows(xlBook.Name).Visible = True
    xlBook.Save

End Sub

Cheers!
(°v°)
Random Solutions  
 
programming4us programming4us