Page 1 of 1 1
Topic Options
#211939 - 2016-09-02 06:56 PM If file doesn't exist - I need the script to exit
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
This is my current script:
 Code:
Dim $accessApp

$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 log file
IF Open(2, "\\server\folder\monthlylog$month.txt",5) = 0
    $w = WriteLine( 2 , "allowA.kix started at " +@DATE + " - " + @TIME + Chr(13) + Chr(10) )
  ELSE
  BEEP
  ? "Error opening log file: [" + @ERROR + "]"
  sleep 5
  EXIT
ENDIF

$accessApp = CreateObject("Access.Application")
;$accessApp.visible = 1
$accessApp.OpenCurrentDatabase("\\Server\folder\allow.mdb")

$accessApp.DoCmd.RunMacro("A")

$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0

; open excel file allowable 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

  sleep 5

$rc = $wbs.Open("\\server\folder\allow.xls")

$app.Range("A1:L1").Font.Bold = 1
$app.Range("I1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("K1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("L1").Entirecolumn.NumberFormat = "$#,##0.00"

$app.Range("A1:L1").EntireColumn.AutoFit

$filename = "\\server\folder\A-$year-$month.xls"

$app.ActiveWorkbook.SaveAs("$filename",-4143) 

$rc = $wbs.Open("\\server\folder\missing.xls")

$app.Range("A1:K1").Font.Bold = 1
$app.Range("H1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("I1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("J1").Entirecolumn.NumberFormat = "$#,##0.00"

$app.Range("A1:K1").EntireColumn.AutoFit
$filename = "\\server\folder\Amissing-$year-$month.xls"

$app.ActiveWorkbook.SaveAs("$filename",-4143) 
Del "\\server\folder\allow.xls"
Del "\\server\folder\missing.xls"
$wbs.Close
$wbs = 0

$app.DisplayAlerts = 0
$app.Quit
$app = 0

$w = WriteLine( 2 , "allowa.kix finished at " +@DATE + " - " + @TIME + Chr(13) + Chr(10) )
$w = WriteLine( 2 , "   " + Chr(13) + Chr(10) )
$ = Close (2)


This is what I THINK I need to add to close the script if the initial file does not exist:

 Code:
Dim $accessApp

$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 log file
IF Open(2, "\\server\folder\monthlylog$month.txt",5) = 0
    $w = WriteLine( 2 , "allowA.kix started at " +@DATE + " - " + @TIME + Chr(13) + Chr(10) )
  ELSE
  BEEP
  ? "Error opening log file: [" + @ERROR + "]"
  sleep 5
  EXIT
ENDIF

$accessApp = CreateObject("Access.Application")
;$accessApp.visible = 1
$accessApp.OpenCurrentDatabase("\\Server\folder\allow.mdb")

$accessApp.DoCmd.RunMacro("A")

$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0
[b]If exist "\\server\folder\allow.xls"[/b]

; open excel file allowable 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

  sleep 5

$rc = $wbs.Open("\\server\folder\allow.xls")

$app.Range("A1:L1").Font.Bold = 1
$app.Range("I1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("K1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("L1").Entirecolumn.NumberFormat = "$#,##0.00"

$app.Range("A1:L1").EntireColumn.AutoFit

$filename = "\\server\folder\A-$year-$month.xls"

$app.ActiveWorkbook.SaveAs("$filename",-4143) 

[b]Else EXIT[/b]
[b]ENDIF[/b]

$rc = $wbs.Open("\\server\folder\missing.xls")

$app.Range("A1:K1").Font.Bold = 1
$app.Range("H1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("I1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("J1").Entirecolumn.NumberFormat = "$#,##0.00"

$app.Range("A1:K1").EntireColumn.AutoFit
$filename = "\\server\folder\Amissing-$year-$month.xls"

$app.ActiveWorkbook.SaveAs("$filename",-4143) 
Del "\\server\folder\allow.xls"
Del "\\server\folder\missing.xls"
$wbs.Close
$wbs = 0

$app.DisplayAlerts = 0
$app.Quit
$app = 0

$w = WriteLine( 2 , "allowa.kix finished at " +@DATE + " - " + @TIME + Chr(13) + Chr(10) )
$w = WriteLine( 2 , "   " + Chr(13) + Chr(10) )
$ = Close (2)



I want to exit the script if the initial files doesn't exist. If it isn't there, the 2nd won't be either.

Thanks All!!

Top
#211940 - 2016-09-02 09:53 PM Re: If file doesn't exist - I need the script to exit [Re: Karon]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Here's one way to do it.

 Code:
Dim $accessApp

$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 log file
IF Open(2, "\\server\folder\monthlylog$month.txt",5) = 0
    $w = WriteLine( 2 , "allowA.kix started at " +@DATE + " - " + @TIME + @CRLF )
  ELSE
  BEEP
  ? "Error opening log file: [" + @ERROR + "]"
  sleep 5
  EXIT
ENDIF

$accessApp = CreateObject("Access.Application")
;$accessApp.visible = 1
$accessApp.OpenCurrentDatabase("\\Server\folder\allow.mdb")

$accessApp.DoCmd.RunMacro("A")

$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0

If Not Exist("\\server\folder\allow.xls")
   $w = WriteLine( 2 , "Error: Initial file not found.  Exiting..." + @CRLF )
   $w = WriteLine( 2 , "allowa.kix finished at " +@DATE + " - " + @TIME + @CRLF )
   $w = WriteLine( 2 , "   " + @CRLF )
   $ = Close (2)
   Exit 1
EndIf

; open excel file allowable 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

  sleep 5

$rc = $wbs.Open("\\server\folder\allow.xls")

$app.Range("A1:L1").Font.Bold = 1
$app.Range("I1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("K1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("L1").Entirecolumn.NumberFormat = "$#,##0.00"

$app.Range("A1:L1").EntireColumn.AutoFit

$filename = "\\server\folder\A-$year-$month.xls"
$app.ActiveWorkbook.SaveAs("$filename",-4143) 

$rc = $wbs.Open("\\server\folder\missing.xls")

$app.Range("A1:K1").Font.Bold = 1
$app.Range("H1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("I1").Entirecolumn.NumberFormat = "$#,##0.00"
$app.Range("J1").Entirecolumn.NumberFormat = "$#,##0.00"

$app.Range("A1:K1").EntireColumn.AutoFit
$filename = "\\server\folder\Amissing-$year-$month.xls"

$app.ActiveWorkbook.SaveAs("$filename",-4143) 
Del "\\server\folder\allow.xls"
Del "\\server\folder\missing.xls"
$wbs.Close
$wbs = 0

$app.DisplayAlerts = 0
$app.Quit
$app = 0

$w = WriteLine( 2 , "allowa.kix finished at " +@DATE + " - " + @TIME + @CRLF )
$w = WriteLine( 2 , "   " + Chr(13) + Chr(10) )
$ = Close (2)

Top
#211945 - 2016-09-06 05:49 PM Re: If file doesn't exist - I need the script to exit [Re: ShaneEP]
Karon Offline
Getting the hang of it

Registered: 2009-07-16
Posts: 87
Loc: Texas
That did it!! Thanks!
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1383 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.053 seconds in which 0.026 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org