Error Level of Excel macro to Batch script

Shalih

New Member
Joined
Mar 2, 2011
Messages
16
Can any please let me know how to set an error level in excel macro. here is the scenario. I have an ecel that runs automatically from the scheduled task which runs a few macros. this excel sheet is initiated with in a batch script. If the macro returns an error or fails, i need to get an error level returned back to the script which can trigger a mail. Please help me in setting an error level inside the macro when the excel fails and let it return to the batch script.

Thanks!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Write the macro as a function and return the errorlevel from the function. As an example, this function will error if the string passed to it is empty:-
Code:
Option Explicit
 
Public Function MyFunction(ByVal sParameter As String) As Boolean
 
  If Len(sParameter) = 0 Then
    MyFunction = False
  Else
    MyFunction = True
  End If
 
End Function
Then instead of merely calling the macro as a procedure, you call it as a function and test its value:-
Code:
  If MyFunction(strValue) Then
[COLOR=black]    MsgBox "Okay"[/COLOR]
  Else
    MsgBox "Error!"
  End If
Obviously I've just put dummy comments in there but it gives you an idea of how to do it.

If you need to return a range of possible values from the function instead of just True/False, you'd do something like this:-
Code:
Option Explicit
 
Public Function MyFunction(ByVal Parameter As [I]{type}[/I]) As Integer
 
  If [I]{some test fails}[/I] Then MyFunction = 1 : Exit Function
 
  If [I]{some other test fails}[/I] Then MyFunction = 2 : Exit Function
 
  If [I]{final test fails}[/I] Then MyFunction = 3 : Exit Function
 
 [COLOR=green] ' passed all tests - flag as okay[/COLOR]
  
  MyFunction = 0
 
End Function
then you'd test the value of the function as a number: 0 is okay, 1-3 indicate an error.
 
Upvote 0
R. thanks for the prompt reply. I am running a macro which calls a lot of functions inside it. Now i need to pass this value to a notepad file.
 
Upvote 0
This will create (or overwrite) a file. Insert your desired filename in place of the red bit and whatever value(s) you want to write to the text file in place of the blue bit.
Code:
dim intfh as integer
close
intfh=freefile()
open [COLOR=red][B]"c:\temp\filename.txt"[/B][/COLOR] for output as #intfh
print#intfh,[B][COLOR=blue]"some value"[/COLOR][/B]
close #intfh
Output multiple values separated by semicolons:-
Code:
print#intfh,format(now(),"dd/mm/yyyy hh:nn:ss");"  ";[I]somevariable[/I]
 
Upvote 0
R.. it would take another 8 hours before i test this ... but a doubt.. within the macro, i am suppressing the ON ERROR statement in some parts.. Will this still fetch the desired result... I cant remove the suppression as it would cause issues to the existing functionality...

again as a summary, if the macro returns a 'debug/end' message with any kind of error valu; this is what i need to capture... becuase i cant let the excel sheet not do its task and me sit idle without the checking on the server....i needed this value in a notepad file so that i can compare and shoot a mail using the batch script....

Will be really great for your quick response!! i've been banging my head for 2 days on this and i am a newbie in this VBA stuff.. :eeek:
 
Upvote 0
The function only returns a value indicating how it completed. It doesn't raise an error. You have to check the return value and take whatever action you want to.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top