$setup = "\\server\share\folder\names.txt"
$thismo = @monthno -1
IF $thismo = 0
$month = "12"
ELSE
IF $thismo > 9
$month = CSTR($thismo)
ELSE
$month = "0" + CSTR($thismo)
ENDIF
ENDIF
$year = IIf(@MONTHNO = 1, @YEAR -1, @YEAR)
;open txt file with names
IF Open(1, "$setup") <> 0
BEEP
? "spec file not opened, error code: [" + @ERROR + "]"
sleep 5
EXIT
ENDIF
Dim $accessApp
$accessApp = CreateObject("Access.Application")
$accessApp.DisplayAlerts=0
;$accessApp.visible = 1
$accessApp.OpenCurrentDatabase("\\server\share\folder\file.mdb")
$accessApp.DoCmd.RunMacro("exportnames")
$app = CreateObject("Excel.Application")
$app.DisplayAlerts = 0
;$app.visible = 1
;Create the Workbooks object
$wbs = $app.Workbooks
sleep 10
;main program loop
WHILE $readerr = 0
$x = ReadLine(1)
$readerr = @ERROR
IF $readerr <> 0
EXIT
ENDIF
$key = SUBSTR($x, 1, INSTR($x, ",")-1)
$x = SUBSTR($x, INSTR($x, ",")+1, LEN($x) - INSTR($x, ","))
$name = SUBSTR($x, 1, LEN($x) - INSTR($x, ","))
$accessApp.DoCmd.RunSQL("SELECT [prod2]![no] AS [Key], prod2.class, Sum(prod2.Oct_amt) AS Oct_amt, Sum(prod2.Nov_amt) AS Nov_amt, Sum(prod2.Dec_amt) AS Dec_amt, Sum(prod2.Jan_amt) AS Jan_amt, Sum(prod2.Feb_amt) AS Feb_amt, Sum(prod2.Mar_amt) AS Mar_amt, Sum(prod2.Apr_amt) AS Apr_amt, Sum(prod2.May_amt) AS May_amt, Sum(prod2.Jun_amt) AS Jun_amt, Sum(prod2.Jul_amt) AS Jul_amt, Sum(prod2.Aug_amt) AS Aug_amt, Sum(prod2.Sep_amt) AS Sep_amt, Sum(prod2.Tot_amt) AS Tot_amt INTO tempsum
FROM prod2
GROUP BY [prod2]![no], prod2.class
HAVING ((([prod2]![no])=$key));")
Sleep 1
$accessApp.DoCmd.RunSQL("SELECT tempsum.Key, Sum(tempsum.Oct_amt) AS Oct_amt, Sum(tempsum.Nov_amt) AS Nov_amt, Sum(tempsum.Dec_amt) AS Dec_amt, Sum(tempsum.Jan_amt) AS Jan_amt, Sum(tempsum.Feb_amt) AS Feb_amt, Sum(tempsum.Mar_amt) AS Mar_amt, Sum(tempsum.Apr_amt) AS Apr_amt, Sum(tempsum.May_amt) AS May_amt, Sum(tempsum.Jun_amt) AS Jun_amt, Sum(tempsum.Jul_amt) AS Jul_amt, Sum(tempsum.Aug_amt) AS Aug_amt, Sum(tempsum.Sep_amt) AS Sep_amt, Sum(tempsum.Tot_amt) AS Tot_amt INTO temptot
FROM tempsum
GROUP BY tempsum.Key;")
sleep 1
$accessApp.DoCmd.RunSQL("SELECT tempsum.Key, classdesc.description, tempsum.Oct_amt, [tempsum]![Oct_amt]/[temptot]![Oct_amt] AS [Oct_pct], tempsum.Nov_amt, [tempsum]![Nov_amt]/[temptot]![Nov_amt] AS [Nov_pct], tempsum.Dec_amt, [tempsum]![Dec_amt]/[temptot]![Dec_amt] AS [Dec_pct], tempsum.Jan_amt, [tempsum]![Jan_amt]/[temptot]![Jan_amt] AS [Jan_pct], tempsum.Feb_amt, [tempsum]![Feb_amt]/[temptot]![Feb_amt] AS [Feb_pct], tempsum.Mar_amt, [tempsum]![Mar_amt]/[temptot]![Mar_amt] AS [Mar_pct], tempsum.Apr_amt, [tempsum]![Apr_amt]/[temptot]![Apr_amt] AS [Apr_pct], tempsum.May_amt, [tempsum]![May_amt]/[temptot]![May_amt] AS [May_pct], tempsum.Jun_amt, [tempsum]![Jun_amt]/[temptot]![Jun_amt] AS [Jun_pct], tempsum.Jul_amt, [tempsum]![Jul_amt]/[temptot]![Jul_amt] AS [Jul_pct], tempsum.Aug_amt, [tempsum]![Aug_amt]/[temptot]![Aug_amt] AS [Aug_pct], tempsum.Sep_amt, [tempsum]![Sep_amt]/[temptot]![Sep_amt] AS [Sep_pct], tempsum.Tot_amt, [tempsum]![Tot_amt]/[temptot]![Tot_amt] AS [Tot_pct] INTO namepct
FROM temptot, tempsum INNER JOIN classdesc ON tempsum.class = classdesc.class;")
sleep 1
$accessApp.DoCmd.RunMacro("exportnamepct")
sleep 1
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-
; open excel file created by Access macro above
; format and save with correct name
$rc = $wbs.Open("\\server\share\folder\temp.xls")
$app.Range("d1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("f1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("h1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("j1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("l1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("n1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("p1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("r1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("t1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("v1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("x1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("z1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("ab1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("c1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("e1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("g1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("i1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("k1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("m1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("o1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("q1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("s1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("u1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("w1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("y1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("aa1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("c24").Formula = "=SUM(c1:c23)"
$app.Range("e24").Formula = "=SUM(e1:e23)"
$app.Range("g24").Formula = "=SUM(g1:g23)"
$app.Range("i24").Formula = "=SUM(i1:i23)"
$app.Range("k24").Formula = "=SUM(k1:k23)"
$app.Range("m24").Formula = "=SUM(m1:m23)"
$app.Range("o24").Formula = "=SUM(o1:o23)"
$app.Range("q24").Formula = "=SUM(q1:q23)"
$app.Range("s24").Formula = "=SUM(s1:s23)"
$app.Range("u24").Formula = "=SUM(u1:u23)"
$app.Range("w24").Formula = "=SUM(w1:w23)"
$app.Range("y24").Formula = "=SUM(y1:y23)"
$app.Range("aa24").Formula = "=SUM(aa1:aa23)"
$app.Range("A1:AB1").insert
$app.Range("A1:AB1").merge
$array = "Fiscal Year 2011"
$app.Range("A1:AB2").HorizontalAlignment = 3
$app.Range("A1:AB1").Value = $array
$app.Range("A1:AB2").Font.Bold = 1
$app.Range("A1:AB2").Borders.LineStyle = 1
$app.Range("A1:AB2").Borders.Weight = 1
$app.Range("A1:AB1").EntireColumn.AutoFit
;$app.Range("A1:AB2").Interior.ColorIndex = 15
$filename = "\\server\share\folder\folder2\$name-$key-$year-$month.xls"
$app.ActiveWorkbook.SaveAs("$filename",-4143)
Del "\\server\share\folder\temp.xls"
LOOP
$wbs = 0
$app.DisplayAlerts = 0
$app.Quit
$app = 0
;IMPORTANT--YOU MUST CLOSE THE WORKBOOKS OBJECT OR ELSE
; THE EXCEL PROCESS WILL NOT TERMINATE!!!
;Destroy the Worksheet, Workbooks, and Application objects
$wbs.Close
$wbs = 0
$app.DisplayAlerts = 0
$app.Quit
$app = 0
$accessApp.Quit
$accessApp = 0
sleep 1