Search code examples
vbscriptaliasaliases

Alias error in VBS


How to use alias in VBScript? I am trying following code:

AliasesExample
Sub AliasesExample
  Dim AliasObj
  ' Obtains the object that corresponds to the Notepad main window
  Set AliasObj = Aliases.notepad.wndNotepad
  ' Checks whether the specified window exists
  If AliasObj.Exists Then
    ' Enters text in the Notepad editor
    AliasObj.Keys("Hello, world.")
  Else
    Log.Error("Notepad is not running.")
  End If
End Sub

but getting the following error:

object required: 'Aliases'

Solution

  • VBScript and Windows Script Host do not support calling DLL and Windows API functions, but there're some possible solutions:

    • You can call DLL functions that are exposed through COM objects:

      Set obj = CreateObject("Foo.Bar")
      Call obj.Method(Param1, Param2)
      
    • You may be able to call some DLL functions using rundll32 if the DLL and the function meet certain requirements.

      ' Open "Programs and Features" using the Control_RunDLL function from shell32.dll
      Set oShell = CreateObject("WScript.Shell")
      oShell.Run "rundll32.exe shell32.dll Control_RunDLL appwiz.cpl,,0"
      

    Other than that, you're out of luck.

    So generally, you need a COM-callable wrapper for your DLL function to be able to use it from VBScript.