SAP GUI Script (VBA) - Variable Not defined - vba

I have this script below copied from a video tutorial somewhere. I recorded the SAP GUI script in 'development' system and tested. It worked fine for me. Now, when other users are trying to use the script using the 'production' system, they get
Compile error: variable not defined
in this line:
Set objConn = objGui.Children(0)
Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
Sub COGS_Report()
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Any tip what to modify in the code?
Regards

I had almost the same issue, where some code needed to be commented away
Try using this code that should only start IE02 transaction (Change equipment). Depending on our system and your user, SAP should respond that a Script is trying to access the Scripting object of SAP. And if you have a Session open it should pick this to use.
SystemName = "KO3" 'change as needed or use a variable
Transaction = "SESSION_MANAGER" 'change as needed or use a variable
On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
MsgBox "No connection to SAP"
End
End If
'MsgBox Sap_Applic.Children(0).info.SystemName
bSession = False
For iConnectionCounter = 0 To qConnections - 1
Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
'MsgBox Connection.Description
'If Not Connection.Description = "" Then
qSessions = Connection.Children.Count
For iSessionCounter = 0 To qSessions - 1
Set Session = Connection.Children(Int(iSessionCounter))
'MsgBox Session.info.SystemName
If Session.info.SystemName <> SystemName Then Exit For
If Session.info.Transaction = Transaction Then
bSession = True
Exit For
End If
Next
'End If
If bSession Then Exit For
Next
If Not bSession Then
MsgBox SystemName & " not available or free session not available"
End
End If
'Stop
Session.findById("wnd[0]").resizeWorkingPane 154, 24, False
Session.findById("wnd[0]/tbar[0]/okcd").Text = "ie02"
Session.findById("wnd[0]").sendVKey 0
Exit Sub
ErrorHandler:
MsgBox "No connection to SAP"
End

Related

Using a module to block shift key in access but when I write ap_DisableShift() in Immediate window I get this error "Compile Error Expected: ="

Option Compare Database
Function ap_DisableShift()
'This function disable the shift at startup. This action causes
'the Autoexec macro and Startup properties to always be executed.
On Error GoTo errDisableShift
Dim db As DAO.Database
Dim prop As DAO.Property
Const conPropNotFound = 3270
Set db = CurrentDb()
'This next line disables the shift key on startup.
db.Properties("AllowByPassKey") = FALSE
'The function is successful.
Exit Function
errDisableShift:
'The first part of this error routine creates the "AllowByPassKey
'property if it does not exist.
If Err = conPropNotFound Then
Set prop = db.CreateProperty("AllowByPassKey", _
dbBoolean, False)
db.Properties.Append prop
Resume Next
Else
MsgBox "Function 'ap_DisableShift' did not complete successfully."
Exit Function
End If
End Function
Function ap_EnableShift()
'This function enables the SHIFT key at startup. This action causes
'the Autoexec macro and the Startup properties to be bypassed
'if the user holds down the SHIFT key when the user opens the database.
On Error GoTo errEnableShift
Dim db As DAO.Database
Dim prop As DAO.Property
Const conPropNotFound = 3270
Set db = CurrentDb()
'This next line of code disables the SHIFT key on startup.
db.Properties("AllowByPassKey") = TRUE
'function successful
Exit Function
errEnableShift:
'The first part of this error routine creates the "AllowByPassKey
'property if it does not exist.
If Err = conPropNotFound Then
Set prop = db.CreateProperty("AllowByPassKey", _
dbBoolean, True)
db.Properties.Append prop
Resume Next
Else
MsgBox "Function 'ap_DisableShift' did not complete successfully."
Exit Function
End If
End Function
Use the following:
Function DatabaseDisableShift()
On Error GoTo FunctionError
Dim CurrentDatabase As DAO.Database, DatabaseProperty As DAO.Property
Const PropertyNotFound = 3270
Set CurrentDatabase = CurrentDb()
CurrentDatabase.Properties("AllowByPassKey") = False
Exit Function
FunctionError:
If Err = PropertyNotFound Then
Set DatabaseProperty = _
CurrentDatabase.CreateProperty("AllowByPassKey", dbBoolean, False)
'Change to True, to enable shift.
CurrentDatabase.Properties.Append DatabaseProperty
Resume Next
Else
MsgBox Err.Description
Exit Function
End If
End Function

How to trap any VBS error and return it to the calling VBA in Access

I have a program in Microsoft Access. I have VBS script files to automate SAP GUI screens ("transactions"). Using VBA in Access opens these different VBS script files using the Scriptcontrol object and performs a transaction in a SAP system.
Now, sometimes there is an error while running the transaction and then the script stops. I have written the error handler in every VBS script files.
My goal is that if there is an error in the SAP while running .VBS then it should close the active SAP session and store the status information in a string called "ScriptStatus". Then I pull this string to the calling vba back and again run the same .vbs script.
Code in the .VBS
dim ScriptStatus
Function (DoWork)
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
on error resume Next
'SAP Code
session.findById("wnd[0]").maximize
'Furhter SAP Code
'Change the ScriptStatus to completed
ScriptStatus = "Script Completed"
If Err.Number <> 0 Then
'Change ScriptStatus
ScriptStatus = "Script Error"
'Close SAP Session
session.findById("wnd[0]").Close
End If
End Function
The code in the calling VBA
Sub Foo()
Dim vbsCode As String, result As Variant, script As Object, ScriptInfo As String
ReRunScript:
'// load vbs source
Open "x.vbs" For Input As #1
vbsCode = Input$(LOF(1), 1)
Close #1
On Error GoTo ERR_VBS
Set script = CreateObject("ScriptControl")
script.Language = "VBScript"
script.AddCode vbsCode
result = script.Run("DoWork")
ScriptInfo = script.Eval("ScriptStatus")
If ScriptInfo = "Script Completed" Then
Exit Sub
Elseif ScriptInfo = "Script Error" Then
Goto ReRunScript
End if
ERR_VBS:
MsgBox Err.Description
MsgBox script.Eval("ScriptStatus")
End Sub
Rather than running them via cscript you can execute them directly using the ScriptControl (32 bit only) - this would let you catch the errors directly in Access with a standard On Error (As well as allowing you to capture a return value).
Example .VBS file:
function DoWork
'// do some work
msgbox 1
'// error
x = 100 / 0
DoWork = "OK"
end function
VBA:
Sub Foo()
Dim vbsCode As String, result As Variant
'// load vbs source
Open "x.vbs" For Input As #1
vbsCode = Input$(LOF(1), 1)
Close #1
On Error GoTo ERR_VBS
With CreateObject("ScriptControl")
.Language = "VBScript"
.AddCode vbsCode
result = .Run("DoWork")
End With
Exit Sub
ERR_VBS:
MsgBox Err.Description
End Sub
Edit - To capture your Status variable make it global in the script (declared outside of a sub/function) and use the .Eval() method to read in in VBA.
Example .VBS file:
dim Status
function DoWork
'// do some work
msgbox 1
Status = "Hello World"
'// error
x = 100 / 0
DoWork = "OK"
end function
VBA:
Sub Foo()
Dim vbsCode As String, result As Variant, script As Object
'// load vbs source
Open "x.vbs" For Input As #1
vbsCode = Input$(LOF(1), 1)
Close #1
On Error GoTo ERR_VBS
Set script = CreateObject("ScriptControl")
script.Language = "VBScript"
script.AddCode vbsCode
result = script.Run("DoWork")
Exit Sub
ERR_VBS:
MsgBox Err.Description
'// read VBS global
MsgBox script.Eval("Status")
End Sub

How to verify the Active Window in SAP GUI using VBA

I'm currently trying to fix some issues in a SAP procedure using VBA.
By now I have a piece of code that can correctly handle the error pop-up that sometimes appears:
If session.findById("wnd[2]/usr/txtMESSTXT1").Text = "Material já atualizado para esta operação" Then
'Saves error text
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i, "d") = "error"
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i, "e") = Now
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i, "f") = session.findById("wnd[2]/usr/txtMESSTXT1").Text
'Goes to the inicial SAP window
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nBPMDG/UTL_BROWSER"
session.findById("wnd[0]/tbar[0]/btn[0]").Press
'Goes to the last line of the while loop
GoTo end_while
End If
The issue is this: this error is triggered by just some of the lines of my database. If I leave the code this way, it will cause an error in the lines that don't need the error handling. I need to verify if the active session is the error pop-up and then proceed to handle the error, ie, I need something like this:
If ActiveSession.Name = "wnd[2]/usr/txtMESSTXT1" Then
If session.findById("wnd[2]/usr/txtMESSTXT1").Text = "Material já atualizado para esta operação" Then
'Saves error text
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i, "d") = "error"
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i, "e") = Now
ThisWoorkbook.Worksheets("ExpandirMateriais").Cells(i, "f") = session.findById("wnd[2]/usr/txtMESSTXT1").Text
'Goes to the inicial SAP window
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nBPMDG/UTL_BROWSER"
session.findById("wnd[0]/tbar[0]/btn[0]").Press
'Goes to the last line of the while loop
GoTo end_while
End If
End If
but I don't know how to properly verify if the active windows is THE error window I'm working with.
I appreciate any help with this syntax.
You could use the following 2 methods to find the relevant SAP session:
the easy way
Set SapGuiAuto = GetObject("SAPGUI")
Set SapApplication = SapGuiAuto.GetScriptingEngine
Set connection = SapApplication.Children(0)
set session = SapApplication.activeSession
...
If session.Name = "wnd[2]/usr/txtMESSTXT1" Then
...
the complicated method
Set SapGuiAuto = GetObject("SAPGUI")
Set SapApplication = SapGuiAuto.GetScriptingEngine
Set connection = SapApplication.Children(0)
for mySession = 0 to connection.children.count - 1
Set ActiveSession = connection.Children(int(mySession))
If ActiveSession.Name = "wnd[2]/usr/txtMESSTXT1" Then
set session = ActiveSession
exit for
end if
next
If session.Name = "wnd[2]/usr/txtMESSTXT1" Then
...
You will have to find out for yourself which of the two methods is applicable to you.
Regards, ScriptMan

OTA QC ALM :ActiveX component can't create object:Set Scheduler = theTestSet.StartExecution("")

I am trying to execute below OTA code from VBS file to have each test in the test set executed
but I am getting an error ActiveX component can't create object at the step
Set Scheduler = theTestSet.StartExecution("")
My System details:
OS: Windows 7 Professional
System Type: 64-bit Operating System
ALM Version: 12.0
Code:
Set ALMConnection = CreateObject("TDAPIOLE80.TDConnection")
'Connect to server
ALMConnection.InitConnectionEx "http://alm.zebra.com/qcbin"
'Authenticate user
ALMConnection.Login "uk1346", "aJanuary$2018"
' Connect to project
ALMConnection.Connect "ZEBRA_PROJECTS", "Zebra_Global_Projects"
Set TSetFact = ALMConnection.TestSetFactory
Set tsTreeMgr = ALMConnection.TestSetTreeManager
nPath = "Root\" & Trim("Automation\Automation Dry Run")
Set tsFolder = tsTreeMgr.NodeByPath(nPath)
If (tsFolder Is Nothing) Then
MSGBOX "Folder is not found"
Else
MSGBOX "Folder is found"
End If
Set tsList = tsFolder.FindTestSets("Dry Run")
If tsList.Count > 0 Then
MSGBOX "FindTestSets found more than one test set"
ElseIf tsList.Count < 1 Then
MSGBOX "FindTestSets: test set not found"
End If
Set theTestSet = tsList.Item(1)
MSGBOX theTestSet.ID
'
Set Scheduler = theTestSet.StartExecution("")
Scheduler.RunAllLocally = True
Scheduler.Run
Set execStatus = Scheduler.ExecutionStatus
MSGBOX execStatus
Set ALMConnection = Nothing
Running the “ALM Client Registration” solved my problem: http://XXXX/qcbin/start_a.jsp?common=true

How can I use vba to get access to a table in Access without opening the mdb file?

I am trying to add a field using VBA to a table in a mdb file if the field does not exist. If I open the mdb file in Access, and run the VBA code, it works fine. However, if I clode Access, I will encounter 'Error 3265 : Item not found in this collection.' at 'With Access.Application.DBEngine(0)(0).TableDefs("Contract")' stage.
Thanks!
Here is my code:
Sub ResetDB()
Dim nlen As Long
MsgBox ("Select the Access Database using this browse button")
NewFN = Application.GetOpenFilename(FileFilter:="mdb.Files (*.mdb), *.mdb", Title:="Please select a file")
If NewFN = False Then
' They pressed Cancel
MsgBox "Try Again if database needs to be reset"
Application.DisplayAlerts = False
'ActiveWorkbook.Close
Application.DisplayAlerts = True
Exit Sub
Else
ActiveWorkbook.Unprotect ("12345")
Sheets("Version").Visible = True
Worksheets("Version").Unprotect (strPW)
Range("Database").Value = NewFN
'On Error GoTo Failed ' I comment this line just to see where the error is
' following line is when the error occurs
With Access.Application.DBEngine(0)(0).TableDefs("Contract")
.Fields.Refresh
nlen = Len(.Fields("Industry_Type").Name)
If nlen > 0 Then Sheets("Instructions").Range("a1") = 1 ' do nothing
End
End With
Failed:
If Err.Number = 3265 Then Err.Clear ' Error 3265 : Item not found in this collection.
With Access.Application.DBEngine(0)(0).TableDefs("Contract")
.Fields.Append .CreateField("Industry_Type", dbLong)
End With
End
End If
End Sub
If the Access is closed, you will not be able to work on it.
You must open the MDB file:
Dim db As New Access.Application
db.OpenAccessProject filepath
The use db to retrieve the tables:
db.TableDefs....