Partager via


AutomationFactory.GetObject Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a reference to the previously activated and currently running Automation server with the specified programmatic identifier (ProgID).

Namespace:  System.Runtime.InteropServices.Automation
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Shared Function GetObject ( _
    progID As String _
) As Object
public static Object GetObject(
    string progID
)

Parameters

  • progID
    Type: System.String
    The ProgID of the registered Automation server to retrieve a reference to.

Return Value

Type: System.Object
A late-bound reference to the specified Automation server.

Exceptions

Exception Condition
Exception

No object was found registered for the specified progID.

Remarks

You must assign the return value to a variable of type Object in Visual Basic and type dynamic in C#. This enables you to use the reference as a late-bound object.

For more information about Automation, see Automation.

Examples

The following code example demonstrates how to use this method.

This example is part of a larger example in How to: Use Automation in Trusted Applications.

Private outlook As Object

Private Function InitializeOutlook() As Boolean

    Try
        ' If GetObject throws an exception, then Outlook is 
        ' either not running or is not available.
        outlook = AutomationFactory.GetObject("Outlook.Application")
        Return True
    Catch
        Try
            ' Start Outlook and display the Inbox, but minimize 
            ' it to avoid hiding the Silverlight application.
            outlook =
                AutomationFactory.CreateObject("Outlook.Application")
            outlook.Session.GetDefaultFolder(6).Display() ' 6 = Inbox
            outlook.ActiveWindow.WindowState = 1  ' minimized
            Return True
        Catch
            ' Outlook is unavailable.
            Return False
        End Try
    End Try

End Function
private dynamic outlook;

private bool InitializeOutlook()
{
    try
    {
        // If GetObject throws an exception, then Outlook is 
        // either not running or is not available.
        outlook = AutomationFactory.GetObject("Outlook.Application"); 
        return true;
    }
    catch (Exception)
    {
        try
        {
            // Start Outlook and display the Inbox, but minimize 
            // it to avoid hiding the Silverlight application.
            outlook = 
                AutomationFactory.CreateObject("Outlook.Application");
            outlook.Session.GetDefaultFolder(6 /* Inbox */).Display();
            outlook.ActiveWindow.WindowState = 1; // minimized
            return true;
        }
        catch (Exception)
        {
            // Outlook is unavailable.
            return false;
        }
    }
}

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.