2

When recording an SAP GUI Script, the first lines of the code are always the same:

If Not IsObject(app) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set app = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = app.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 app, "on"
End If

In this code, four variables are used: app, SapGuiAuto, Connection and session. Why can these variables be used without having been declared? I have made countless scripts based on this, and everything has always worked. I do not like the feeling of not understanding my own code, though. :-)

3
  • 2
    Hard to tell from the code snippet you show. But probably Option Explicit is not set. Commented Jul 14, 2014 at 14:36
  • This is the only code, @0xA3. Put this in a Sub, and it will connect to your SAP like a good boy. Commented Jul 14, 2014 at 18:14
  • @LeeWhite, did the answer work for you? Commented Jul 17, 2014 at 14:15

2 Answers 2

2

There are two possibilities (perhaps more)

  1. These are global variables somewhere in your code, maybe on some other module page
  2. Option Explicit is turned off (or rather, not explicitly specified).

If it's case 2, I can write code such as

Sub Test()
    a = a + 1
    MsgBox(a)
End Sub

and get the result '1' since in this case, VBA automatically creates a variable 'a' and initializes it to 0.

Depending on the way a new variable is first use, VBA can initialize it. So to be safe, it's always better to write Option Explicit as the very first line in your module and then proceed with coding the rest.

Doing so requires explicit declaration of any variables before using them. Always a good coding practice since then, the compiler will catch any uninitialized variables and prevent nasty runtime surprises.

Sign up to request clarification or add additional context in comments.

1 Comment

@LeeWhite Yes it's a local variable; if nothing is initialized, then a variant type variable is created, and it can handle numerous data types. But 1) it affects performance, as variants are an order of magnitude slower than primitive data types and 2) it is better design practice to explicitly declare/initialize variables than let the compiler decide and do so.
2

Unless you add Option Explicit at the beginning of each module, VB/VBA will allow you to use variables without DIMming them (it automatically DIMs them as variants). Most of the time, this causes no problems. When it does cause a problem, and sooner or later it will, it'll be very difficult to figure out.

Best practice is to put Option Explicit at the top of every module. VB/VBA will then force you to explicitly declare your variables before using them and because they're declared, it won't let you put inappropriate data into a variable.

In the IDE, Tools | Options | Editor Tab, checkmark Require Variable Declaration. The IDE will automatically add Option Explicit to all new modules for you. Unless you're working on a Mac, where this feature's broken.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.