Question : Use of Excel Macro in MS Access 2000

Hello - I have an Access macro whose last step outputs a table to Excel 97 - 2003 format.  I then open the Excel file and run a macro manually to format the file (changing fonts, freezing panes, conditional formatting, print format, etc.).

I want to eliminate the manual second process.  Can you embed the Excel macro into Access so the output file is already formatted?

I am a novice at VBA but should be able to implement with good instructions.    Thanks.
 

Answer : Use of Excel Macro in MS Access 2000

Hi,

I normally do not look at entire databases on EE (that would be my paying job), but I did look at your Excel macro. The code below runs from Access, so you don't need the macro workbook any more. It's easier to maintain this way in the long run. I think I preserved most of your code, in only a fraction of its original length. Please compare it to your original recorded macro and see if I forgot an important line.

The code will open a file named TestOutputFile.xls in the same folder as the Access database. Adjust accordingly. It will work whether the file is already open or not, and will use the current instance of Excel if one is available.

(°v°)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
Sub TextXL()
    Dim wkb As Excel.Workbook
    Dim wks As Excel.Worksheet
    
    Set wkb = GetObject(CurrentProject.Path & "\TestOutputFile.xls")
    wkb.Windows(1).Visible = True
    Set wks = wkb.Worksheets(1)

    With wks.Cells
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Borders.LineStyle = xlNone
        .EntireColumn.AutoFit
        .AutoFilter
        .Columns("D:E").Group
        With .Columns("I:K")
            .NumberFormat = "m/d/yy;@"
            .HorizontalAlignment = xlCenter
        End With
        .Columns("L:M").HorizontalAlignment = xlCenter
        .Columns("N:N").NumberFormat = "$#,##0.00"
        .Columns("O:AR").NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)"
        .Columns("T:Z").Group
        .Columns("AB:AI").Group
    End With
    
    wks.Range("F2").Select
    wkb.Windows(1).FreezePanes = True
    With wks.PageSetup
        .PrintTitleRows = "$1:$1"
        .PrintTitleColumns = ""
        .LeftHeader = ""
        .CenterHeader = "&A"
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = 0.25 * 72
        .RightMargin = 0.25 * 72
        .TopMargin = 1 * 72
        .BottomMargin = 0.75 * 72
        .HeaderMargin = 0.5 * 72
        .FooterMargin = 0.5 * 72
    End With
    wkb.Save
    wkb.Close

End Sub
Random Solutions  
 
programming4us programming4us