Djones4822
Djones4822

Reputation: 767

Retrieving COM errors with win32com.client similar to VBS

I have an application called "SAS Enterprise Guide" which is a process flow development program which is compatible with the SAS platform. In it, I create a process flow to my liking, then I can "schedule" it. This "scheduling" process outputs a vbs script and creates a Task Scheduler object. The vbs script creates a COM object, opens the process flow project, runs it, saves it, and then closes it. That's it, overall it's a very short and simple script.

That process is very inefficient for a number of reasons, so I wrote a python script to iterate over a bunch of EG projects (trying to schedule 30+ scripts this way is not feasible).

import win32com.client, os

os.chdir('EG Scripts')
app = win32com.client.Dispatch("SASEGObjectModel.Application.7.1")

for file in os.listdir()

    project = app.Open(file, "")

    project.Run()
    project.Save()
    project.Close()
    project = None

app.Quit()
app = None
del app

However, their provided vbs script seems to include some error catching, I'd like to do this as well but I'm not sure how. To test, I've created an EG project that will fail 100% of the time. When I run it through python, it just fails silently. I know that script did not successfully execute but nothing was returned or printed in the console.

Within their provided script I see them running these lines:

Set app = CreateObject("SASEGObjectModel.Application.7.1")
If Checkerror("CreateObject") = True Then
    Exit Sub
End If

Function Checkerror(fnName)
    Checkerror = False

    Dim strmsg
    Dim errNum

    If Err.Number <> 0 Then
        strmsg = "Error #" & Hex(Err.Number) & vbCrLf & "In Function " & fnName & vbCrLf & Err.Description
        'MsgBox strmsg  'Uncomment this line if you want to be notified via MessageBox of Errors in the script.
        Checkerror = True
    End If

End Function

But I can't seem to figure how to replicate that in python. Does anyone have any idea?

Upvotes: 1

Views: 329

Answers (1)

Djones4822
Djones4822

Reputation: 767

It's been 2 years and I have since become much more familiar with the COM interface of EG.

Running SAS EG project with Python

There, I have written a pretty extensive answer to a similar question. I recommend reading (and reading the including reference to official SAS documentation by Chris Hemedinger).

To this one specifically, I've found that the answer is to run the project, save the project to a temporary file, then re-open the project to read the logs for an error.

To read the logs, you need to iterate over each item in the flow, check for a log, and then scan for "ERROR" in the log text. If the flow item is an egData item then iterate over it for the egTask items and read those logs.

As I've said, Chris Hemedinger has written great documentation including a lookup of all EG Type codes. While there are no examples in Python, it's very accessible. The current link to the SAS Article written in 2017 is here: https://communities.sas.com/t5/SAS-Communities-Library/Doing-More-with-SAS-Enterprise-Guide-Automation/ta-p/417832?title=Not_Just_for_Scheduling:_Doing_More_with_SAS_Enterprise_Guide_Automation

Please let me know if anyone has questions or issues.

Upvotes: 0

Related Questions