AppActivate Excel-vba: o Excel pisca na barra de tarefas e não é ativado

0

Em um módulo Excel-vba:

Option Explicit
Sub Test()
    Dim Outlook As Object
    Set Outlook = CreateOutlook()
    Set Outlook = Nothing
End Sub
Function CreateOutlook() As Object
    Dim Outlook As Object
    On Error Resume Next
        Set Outlook = GetObject(, "Outlook.Application")
    On Error GoTo 0
    If Outlook Is Nothing Then Shell "Outlook"
    On Error Resume Next
        Do While Outlook Is Nothing
            Set Outlook = GetObject(, "Outlook.Application")
        Loop
    On Error GoTo 0
    Set Outlook = CreateObject("Outlook.Application")
    AppActivate Application.Caption 'This line doesn't cause an error but excel icon flashes in taskbar and doesn't activate.
    Set CreateOutlook = Outlook
    Set Outlook = Nothing
End Function

Eu tenho o Windows 7 Ultimate, de 64 bits.

    
por Riccardo La Marca 06.03.2018 / 16:24

1 resposta

0

Resolvido!

Em vez de:

AppActivate Application.Caption

Isto:

SendKeys "% i"

Então:

Option Explicit
Sub Test()
    Dim Outlook As Object
    Set Outlook = CreateOutlook()
    Set Outlook = Nothing
End Sub
Function CreateOutlook() As Object
    Dim Minimize As Boolean
    Dim Outlook As Object
    Minimize = False
    On Error Resume Next
        Set Outlook = GetObject(, "Outlook.Application")
    On Error GoTo 0
    If Outlook Is Nothing Then
        Shell "Outlook"
        Minimize = True
    End If
    On Error Resume Next
        Do While Outlook Is Nothing
            Set Outlook = GetObject(, "Outlook.Application")
        Loop
    On Error GoTo 0
    If Minimize Then SendKeys "% i"
    Set Outlook = Nothing
    Set CreateOutlook = CreateObject("Outlook.Application")
End Function
    
por 06.03.2018 / 18:22