#211414 - 2016-05-04 05:34 PM
Close apps that open multiple times
|
Karon
Getting the hang of it
Registered: 2009-07-16
Posts: 87
Loc: Texas
|
I have several scripts that open multiple databases (access) and excel files. My problem is the applications are not closing so I will have multiple msaccess and excel running in the background, with the only way to close is thru task manager.
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-
$setup = "\\server\share\reports\person.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 person 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.visible = 1
$accessApp.OpenCurrentDatabase("\\server\share\folder\payermixperson.mdb")
$accessApp.DoCmd.RunMacro("exportperson")
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, ","))
$personname = SUBSTR($x, 1, LEN($x) - INSTR($x, ","))
$accessApp.DoCmd.RunSQL("SELECT [prod2]![perno] 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]![perno], prod2.class
HAVING ((([prod2]![perno])=$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 perpct
FROM temptot, tempsum INNER JOIN classdesc ON tempsum.class = classdesc.class;")
sleep 1
$accessApp.DoCmd.RunMacro("exportpersonpct")
sleep 1
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-
; open excel file apptutil created by Access macro above
; format and save with correct name
$app = CreateObject("Excel.Application")
;$app.visible = 1
;Create the Workbooks object
$wbs = $app.Workbooks
$rc = $wbs.Open("\\server\share\folder\payermix monthly\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\payermix monthly\person\$pername-$key-$year-$month.xls"
$app.ActiveWorkbook.SaveAs("$filename",-4143)
Del "\\server\share\folder\payermix monthly\temp.xls"
;IMPORTANT--YOU MUST CLOSE THE WORKBOOKS OBJECT OR ELSE
; THE EXCEL PROCESS WILL NOT TERMINATE!!!
$wbs.Close
;Destroy the Worksheet, Workbooks, and Application objects
$wbs = 0
$app = 0
LOOP
$accessApp = 0
sleep 1
We are using office 2010. Thanks in advance
|
Top
|
|
|
|
#211415 - 2016-05-04 10:00 PM
Re: Close apps that open multiple times
[Re: Karon]
|
ShaneEP
MM club member
Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
|
Typically if you call the Quit function from the app it will terminate the process.
$app.DisplayAlerts = 0
$app.Quit
$app = 0
$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0 The DisplayAlerts=0 portion prevents any unwanted prompts from occurring when it's closed (like asking to save a document).
Edited by ShaneEP (2016-05-04 10:02 PM)
|
Top
|
|
|
|
#211416 - 2016-05-04 10:28 PM
Re: Close apps that open multiple times
[Re: ShaneEP]
|
Karon
Getting the hang of it
Registered: 2009-07-16
Posts: 87
Loc: Texas
|
so at the bottom it should read
$filename = "\\server\share\folder\payermix monthly\person\$pername-$key-$year-$month.xls"
$app.ActiveWorkbook.SaveAs("$filename",-4143)
Del "\\server\share\folder\payermix monthly\temp.xls"
;IMPORTANT--YOU MUST CLOSE THE WORKBOOKS OBJECT OR ELSE
; THE EXCEL PROCESS WILL NOT TERMINATE!!!
$wbs.Close
;Destroy the Worksheet, Workbooks, and Application objects
$wbs = 0
$app.DisplayAlerts = 0
$app.Quit
$app = 0
LOOP
$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0
Should the accessApp quitting be inside the loop so that it doesn launch another access application?
|
Top
|
|
|
|
#211417 - 2016-05-05 12:47 AM
Re: Close apps that open multiple times
[Re: Karon]
|
ShaneEP
MM club member
Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
|
Actually, I would recommend taking the excel application creation out of the loop altogether. Move these lines up before the loop begins.
$app = CreateObject("Excel.Application") There is no need to create an excel process over and over. Just create it once, and then open and edit documents as needed. Then close it at the end, after the loop is complete.
|
Top
|
|
|
|
#211418 - 2016-05-05 12:59 AM
Re: Close apps that open multiple times
[Re: ShaneEP]
|
ShaneEP
MM club member
Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
|
This is untested, but moved the parts out of the loop that didnt need to be in the loop.;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-
$setup = "\\server\share\reports\person.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 person 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.visible = 1
$accessApp.OpenCurrentDatabase("\\server\share\folder\payermixperson.mdb")
; open excel file apptutil created by Access macro above
$app = CreateObject("Excel.Application")
;$app.visible = 1
;Create the Workbooks object
$wbs = $app.Workbooks
$accessApp.DoCmd.RunMacro("exportperson")
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, ","))
$personname = SUBSTR($x, 1, LEN($x) - INSTR($x, ","))
$accessApp.DoCmd.RunSQL("SELECT [prod2]![perno] 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]![perno], prod2.class HAVING ((([prod2]![perno])=$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 perpct
FROM temptot, tempsum INNER JOIN classdesc ON tempsum.class = classdesc.class;")
sleep 1
$accessApp.DoCmd.RunMacro("exportpersonpct")
sleep 1
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-
; format and save with correct name
$rc = $wbs.Open("\\server\share\folder\payermix monthly\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\payermix monthly\person\$pername-$key-$year-$month.xls"
$app.ActiveWorkbook.SaveAs("$filename",-4143)
;IMPORTANT--YOU MUST CLOSE THE WORKBOOKS OBJECT OR ELSE
; THE EXCEL PROCESS WILL NOT TERMINATE!!!
$wbs.Close
Del "\\server\share\folder\payermix monthly\temp.xls"
LOOP
;Destroy the Worksheet, Workbooks, and Application objects
$wbs = 0
$app.DisplayAlerts = 0
$app.Quit
$app = 0
$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0
sleep 1
Edited by Glenn Barnas (2016-05-09 04:52 PM) Edit Reason: Broke long lines
|
Top
|
|
|
|
#211419 - 2016-05-05 12:00 PM
Re: Close apps that open multiple times
[Re: ShaneEP]
|
Karon
Getting the hang of it
Registered: 2009-07-16
Posts: 87
Loc: Texas
|
This looks like it may work out fine! It does make sense! I will setup a test folder to work this up and let you know next week.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 369 anonymous users online.
|
|
|