Recherche…


Remarques

Dans sa forme la plus simple, CreateObject crée une instance d'un objet alors que GetObject obtient une instance existante d'un objet. Déterminer si un objet peut être créé ou obtenu dépend de sa propriété Instancing . Certains objets sont SingleUse (par exemple, WMI) et ne peuvent pas être créés s'ils existent déjà. D'autres objets (par exemple, Excel) sont MultiUse et permettent à plusieurs instances de s'exécuter simultanément. Si une instance d'un objet n'existe pas déjà et que vous tentez GetObject , vous recevrez le message récupérable suivant: Erreur d'exécution Run-time error '429': ActiveX component can't create object .

GetObject nécessite qu'au moins un de ces deux paramètres facultatifs soit présent:

  1. Pathname - Variant (String): chemin d'accès complet, y compris nom de fichier, du fichier contenant l'objet. Ce paramètre est facultatif, mais Class est requis si le chemin d'accès est omis.
  2. Class - Variant (String): Chaîne représentant la définition formelle (Application et ObjectType) de l'objet. La classe est obligatoire si le chemin d'accès est omis.

CreateObject a un paramètre requis et un paramètre facultatif:

  1. Class - Variant (String): Chaîne représentant la définition formelle (Application et ObjectType) de l'objet. La classe est un paramètre obligatoire.
  2. Servername - Variant (String): nom de l'ordinateur distant sur lequel l'objet sera créé. S'il est omis, l'objet sera créé sur la machine locale.

La classe est toujours composée de deux parties sous la forme de Application.ObjectType :

  1. Application - Nom de l'application dont l'objet fait partie. |
  2. Type d'objet - Le type d'objet en cours de création. |

Quelques exemples de classes sont:

  1. Word.Application
  2. Feuille de calcul Excel
  3. Scripting.FileSystemObject

Démonstration de GetObject et CreateObject

Fonction MSDN-GetObject

Renvoie une référence à un objet fourni par un composant ActiveX.

Utilisez la fonction GetObject lorsqu'il existe une instance actuelle de l'objet ou si vous souhaitez créer l'objet avec un fichier déjà chargé. S'il n'y a pas d'instance actuelle et que vous ne voulez pas que l'objet démarre avec un fichier chargé, utilisez la fonction CreateObject.

Sub CreateVSGet()
    Dim ThisXLApp As Excel.Application 'An example of early binding
    Dim AnotherXLApp As Object 'An example of late binding
    Dim ThisNewWB As Workbook
    Dim AnotherNewWB As Workbook
    Dim wb As Workbook
    
    'Get this instance of Excel
    Set ThisXLApp = GetObject(ThisWorkbook.Name).Application
    'Create another instance of Excel
    Set AnotherXLApp = CreateObject("Excel.Application")
    'Make the 2nd instance visible
    AnotherXLApp.Visible = True
    'Add a workbook to the 2nd instance
    Set AnotherNewWB = AnotherXLApp.Workbooks.Add
    'Add a sheet to the 2nd instance
    AnotherNewWB.Sheets.Add
    
    'You should now have 2 instances of Excel open
    'The 1st instance has 1 workbook: Book1
    'The 2nd instance has 1 workbook: Book2
    
    'Lets add another workbook to our 1st instance
    Set ThisNewWB = ThisXLApp.Workbooks.Add
    'Now loop through the workbooks and show their names
    For Each wb In ThisXLApp.Workbooks
        Debug.Print wb.Name
    Next
    'Now the 1st instance has 2 workbooks: Book1 and Book3
    'If you close the first instance of Excel,
    'Book1 and Book3 will close, but book2 will still be open
    
End Sub


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow