BCX COM Interface

CREATEOBJECT function

Purpose: CREATEOBJECT creates an instance of specified COM object.


 Syntax:

 COMObj = CREATEOBJECT(ComObjectName$)

 Parameters:

  • COMObj COM OBJECT created by CREATEOBJECT.
  • ComObjectName$ Specifies the name of registered COM object.

Remarks: If the function fails, BCX_COM_ERROR flag is set to TRUE. To get extended error information, call BCX_GET_COM_ERROR_CODE or BCX_GET_COM_ERROR_DESC.

Example 1:


 CLS
  
  DIM app AS OBJECT
  
  app = CREATEOBJECT("Excel.Application")
  
  IF BCX_COM_ERROR THEN
  PRINT BCX_GET_COM_ERROR_DESC$
  END
  END IF
 
  app.visible = true
  
  Sleep(2000) ' Excel will be visible for 2 sec, and than it will close.
  
  app.activeworkbook.saved = true ' don't prompt to save workbook
  
  app.quit
  
  SET app = NOTHING ' remmember to call Set xxx = Nothing to release resources used by COM object!!!
  
  
  KEYPRESS

Example 2:


 DIM Link$
 
 Link$ = Startup_Folder$() + "GnotePad.lnk"
 
 CreateLink("c:\windows\notepad.exe",Link$,"","MS Notepad")
 
 FUNCTION Startup_Folder$()
   DIM A$
   DIM Wshell AS OBJECT
   SET Wshell = CreateObject("Wscript.Shell")
   A$ = Wshell.SpecialFolders("Startup")
   SET Wshell = NOTHING
   FUNCTION = A$ + "\"
 END FUNCTION
 
 FUNCTION CreateLink (Exe$, Link$, Args$, Desc$) AS HRESULT
   DIM hres AS HRESULT
   DIM psl AS IShellLink PTR
   DIM w[MAX_PATH] AS WORD
 
   CoInitialize(NULL)
   hres = CoCreateInstance (&CLSID_ShellLink, NULL, _
   CLSCTX_INPROC_SERVER, &IID_IShellLink, (void**)&psl)
 
   IF (SUCCEEDED(hres)) THEN
     DIM ppf AS IPersistFile PTR
     psl->lpVtbl->SetPath(psl, Exe)
     psl->lpVtbl->SetDescription(psl, Desc)
     psl->lpVtbl->SetShowCmd(psl, SW_SHOWMAXIMIZED)
     psl->lpVtbl->SetArguments(psl, Args$ )
     hres = psl->lpVtbl->QueryInterface(psl, &IID_IPersistFile,(void**)&ppf)
     IF (SUCCEEDED(hres)) THEN
       MultiByteToWideChar(CP_ACP, 0, Link, -1, w, MAX_PATH)
       hres = ppf->lpVtbl->Save(ppf, w, TRUE)
       ppf->lpVtbl->Release(ppf)
     END IF
     psl->lpVtbl->Release(psl)
   END IF
 
   CoUninitialize()
   FUNCTION = hres
 END FUNCTION
 

For other examples of the CREATEOBJECT function see the COM folder in the Files section of the BCX Group on Yahoo.

Related topics: Object data type definition | SET ... NOTHING | List of all COM Interface Functions