none
Bring application to foreground. RRS feed

  • Question

  • Hello all,

    I'm creating a VB macro in Outlook, on some point in the application the macro asks input of an application called IBM Lotus Notes. However this application stays on the background(outlook still on front side) and you have to click on the application in the toolbar to make it visible. Is there any function I can use that brings the IBM Lotus Notes app to the foreground?

    Code:

    Option Explicit
    Sub ItemList()
    
    Dim Explorer As Explorer
    Dim Selection As Selection
    Dim Mail As Outlook.MailItem
    Dim S As Object
    Dim LotusWorkSpace As Object
    Dim LotusSession As Object
    Dim LotusDB As Object
    Dim LotusNewDoc As Object
    Dim LotusItem As Object
    Dim LotusCollection As Object
    Dim LotusColDoc As Object
                  
    Set Explorer = Application.ActiveExplorer
    Set Selection = Explorer.Selection
    
    Set LotusWorkSpace = CreateObject("notes.notesuiworkspace")
    Set LotusSession = CreateObject("notes.notessession")
    Set LotusDB = LotusSession.GETDATABASE("notes_see", "*******")
    
    Set LotusCollection = LotusWorkSpace.PickListCollection(1, True, "notes_see", "********", "Taak Totaal", "Taak", "Maak uw keuze")
    If LotusCollection.Count = 1 Then
     
        For Each S In Selection
            Set Mail = S
            Set LotusColDoc = LotusCollection.GetNthDocument(1)
    
            Set LotusNewDoc = LotusDB.CreateDocument
            LotusNewDoc.Form = "Niels"
            LotusNewDoc.Projectnummer = LotusColDoc.getitemvalue("Projectnummer")
            LotusNewDoc.Projectomschrijving = LotusColDoc.getitemvalue("Projectomschrijving")
            LotusNewDoc.Taaknummer = LotusColDoc.getitemvalue("Taaknummer")
            LotusNewDoc.Taakomschrijving = LotusColDoc.getitemvalue("Taakomschrijving")
            LotusNewDoc.Veld1 = Mail.SenderName
            LotusNewDoc.Veld2 = Mail.Subject
            Call LotusNewDoc.Save(True, True)
    
        Next
    End If
    Set Mail = Nothing
    Set S = Nothing
    Set Selection = Nothing
    Set Explorer = Nothing
     
    End Sub



    Thanks in advance!

    Niels
    Friday, December 22, 2017 1:22 PM

Answers

  • If you know the app's window caption, and ideally its classname adapt the following -

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
                                    ByVal lpClassName As String, _
                                    ByVal lpWindowName As String) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
                                    ByVal hwnd As Long, ByVal lpClassName As String, _
                                    ByVal nMaxCount As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32.dll" ( _
                                    ByVal hwnd As Long) As Long
    
    Sub test()
    Dim sClass As String
    Dim sCaption As String
    
        sClass = vbNullString
        
        ' test with a known app's caption, eg
        sCaption = "Document1 - Microsoft Word" ' unsaved Word
        sCaption = "Untitled - Notepad"  ' unsaved Word
    
        WinToForegound sClass, sCaption
        Debug.Print "Classname: " & sClass
    
    End Sub
    
    
    Function WinToForegound(Optional sClassname As String = vbNullString, _
                       Optional sCaption As String = vbNullString) As Long
    Dim hWin As Long, ret As Long
    Dim sBuff As String * 32
    
    
        hWin = FindWindow(sClassname, sCaption)
        'SetForegroundWindow hwin
        If hWin Then
            WinToForegound = SetForegroundWindow(hWin)
            'returns 1 if success
        End If
    
        ' for testing get the classname if not known
        If Len(sClassname) = 0 And hWin Then
            ret = GetClassName(hWin, sBuff, 32)
            If ret Then
                sClassname = Left$(sBuff, ret)
            End If
        End If
    
    End Function

    If your caption is always known and unique you don't need the classname, otherwise test with the above with the caption you know, return the classname and use it in future even without the caption (though it'd need to be the only instance).

    The APIs will need adapting for use in 64bit Office

    • Marked as answer by Niels Schutte Tuesday, January 2, 2018 7:46 AM
    Saturday, December 23, 2017 3:31 PM
    Moderator

All replies

  • See VBscript bring window to front for one possibility. WMI is probably another possibility; see How do I minimize a window in VBScript with WMI for a sample that is close to what you need. That sample does not do what you need to do but it uses WMI. Look around in the TechNet Script Center for many more scripts.



    Sam Hobbs
    SimpleSamples.Info

    Friday, December 22, 2017 9:42 PM
  • If you know the app's window caption, and ideally its classname adapt the following -

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
                                    ByVal lpClassName As String, _
                                    ByVal lpWindowName As String) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" ( _
                                    ByVal hwnd As Long, ByVal lpClassName As String, _
                                    ByVal nMaxCount As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32.dll" ( _
                                    ByVal hwnd As Long) As Long
    
    Sub test()
    Dim sClass As String
    Dim sCaption As String
    
        sClass = vbNullString
        
        ' test with a known app's caption, eg
        sCaption = "Document1 - Microsoft Word" ' unsaved Word
        sCaption = "Untitled - Notepad"  ' unsaved Word
    
        WinToForegound sClass, sCaption
        Debug.Print "Classname: " & sClass
    
    End Sub
    
    
    Function WinToForegound(Optional sClassname As String = vbNullString, _
                       Optional sCaption As String = vbNullString) As Long
    Dim hWin As Long, ret As Long
    Dim sBuff As String * 32
    
    
        hWin = FindWindow(sClassname, sCaption)
        'SetForegroundWindow hwin
        If hWin Then
            WinToForegound = SetForegroundWindow(hWin)
            'returns 1 if success
        End If
    
        ' for testing get the classname if not known
        If Len(sClassname) = 0 And hWin Then
            ret = GetClassName(hWin, sBuff, 32)
            If ret Then
                sClassname = Left$(sBuff, ret)
            End If
        End If
    
    End Function

    If your caption is always known and unique you don't need the classname, otherwise test with the above with the caption you know, return the classname and use it in future even without the caption (though it'd need to be the only instance).

    The APIs will need adapting for use in 64bit Office

    • Marked as answer by Niels Schutte Tuesday, January 2, 2018 7:46 AM
    Saturday, December 23, 2017 3:31 PM
    Moderator