Automating Outlook from Other Office Applications

Use Visual Basic for Applications (VBA) em qualquer aplicativo do Microsoft Office para controlar o Microsoft Outlook. For example, if you are developing a cross-application solution using one primary application and several secondary applications, you can write VBA code in the primary application to automate Outlook to send messages and to store and retrieve information in Outlook items. Por exemplo, no Excel você pode escrever rotinas que enviam uma pasta de trabalho para uma lista de distribuição do Outlook.

To control Outlook objects from outside Outlook, you must establish a reference to the Outlook object library from the project in which you are writing code. To do this, use the References dialog box in the Visual Basic Editor in the primary application. Em seguida, você pode escrever um código que retorna uma referência ao objeto aplicativo do Outlook. Through this reference, your code has access to all the objects, properties, methods, and constants defined in the Outlook type library.

There are several ways to return a reference to the Outlook Application object.

  • Use a função CreateObject para iniciar uma nova sessão do Outlook e retornar uma referência ao objeto Application que representa a nova sessão.

  • Use a função GetObject do Visual Basic para retornar uma referência ao objeto Application que representa uma sessão que já está em execução. Note that because there can be only one instance of Outlook running at any given time, GetObject usually serves little purpose when used with Outlook. CreateObject can always be used to access the current instance of Outlook or to create a new instance if one does not exist. However, you can use error trapping with the GetObject method to determine whether Outlook is currently running.

  • Use a nova palavra-chave em vários tipos de instruções para criar implicitamente uma nova instância do objeto Aplicativo outlook usando a instrução Set para definir uma variável de objeto para a nova instância do objeto Application . You can also use the New keyword with the Dim, Private, Public, or Static statement to declare an object variable. The new instance of the Application object is then created on the first reference to the variable.

Cuidado Quando você cria uma nova instância do Outlook, a nova instância não é confiável e pode disparar o guarda de modelo de objeto.

For examples of using these methods of referencing the Outlook Application object, see Automating Outlook from a Visual Basic Application.

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.