dear friends,

icreated small project to open excel using vb.
everything works fine with one exception. i used VB express 2010 in xp OS when developing the project. when it runs on the windows 7 platform the opening excel file goes behind the my application. the VB form topmost function is set to false but still the excel opens behind the application. anyways to get the excel as topmost? for your reference my coding is below.
Thanks.

            TextBox1.Text = "" '(to empty up the search textbox to key in next excel file name )
            Dim excel As New Microsoft.Office.Interop.Excel.Application
            Dim wBook As Microsoft.Office.Interop.Excel.Workbook
            Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet


            wBook = excel.Workbooks.Open(dgv1.CurrentRow.Cells(1).Value.ToString, False, True)



            excel.Visible = True


            'excel.WindowState = XlWindowState.xlMaximized

            With excel
                .DisplayFullScreen = True
                .CommandBars("Full Screen").Visible = False
                .CommandBars("Worksheet Menu Bar").Enabled = False
            End With

Dani AI

Generated

Short version: the behavior you see is Windows focus/z-order rules, not a VB bug. opens Excel and sets DisplayFullScreen; suggested a Win32 approach and showed AppActivate which is a simple fallback that often works. A more reliable method is to grab Excel's window handle and use a small, controlled Win32 sequence: restore the window, bring it to the foreground and (optionally) make it briefly topmost to guarantee visibility.

Example (VB.NET P/Invoke pattern — use once after excel.Visible = True):

Imports System.Runtime.InteropServices

<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
End Function

<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
End Function

<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, cx As Integer, cy As Integer, uFlags As UInteger) As Boolean
End Function

' Use excelApp.Hwnd to get the handle, then call ShowWindow/SetForegroundWindow.

Notes and troubleshooting:

  • Excel interop exposes a top-level handle via Application.hWnd — use that to target the correct window. (learn.microsoft.com)
  • Windows restricts which processes can call SetForegroundWindow; if that call fails, the temporary TOPMOST/NOTOPMOST trick via SetWindowPos usually forces visibility. Use the shortest possible topmost period to avoid focus-stealing. (learn.microsoft.com)
  • If excel.Hwnd is zero or you still see issues, fall back to AppActivate (as did), or find the Excel process and use its MainWindowHandle. Also ensure COM objects are released (Marshal.ReleaseComObject, set to Nothing, GC.Collect/WaitForPendingFinalizers) so you don't leave orphan Excel instances.

Caution: forcing topmost can be intrusive; prefer restoring + SetForegroundWindow first and use the topmost step only when necessary.

Recommended Answers

All 2 Replies

Here is a forum post using the Windows API to get the process and bring it forward.

I hope this gets you on your way.

I could not reproduce the problem you are having without using Me.Activate

Then I got a list of the Processes Names and ID's into a Dictionary and used The 'EXCEL' Process ID to Activate the WorkBook with AppActivate(procId)

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text = "" '(to empty up the search textbox to key in next excel file name )
        excel = New Microsoft.Office.Interop.Excel.Application
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet


        wBook = excel.Workbooks.Open("C:\Users\Marijane\Desktop\Projects\DailySales.xlsx", False, True)


        '
        excel.Visible = True


        'excel.WindowState = XlWindowState.xlMaximized

        With excel
            .DisplayFullScreen = True
            .CommandBars("Full Screen").Visible = False
            .CommandBars("Worksheet Menu Bar").Enabled = False
        End With

        Me.Activate()

        Dim dictProcesses As New Dictionary(Of String, Integer)
        dictProcesses = GetProcesses()
        If dictProcesses.ContainsKey("EXCEL") Then
            AppActivate(dictProcesses.Item("EXCEL"))
        End If
        '        excel.Application.ActiveWorkbook.Activate()

    End Sub


     Private Function GetProcesses() As Dictionary(Of String, Integer)

        Dim ret As New Dictionary(Of String, Integer)
        Dim myProcesses() As Process
        Dim myProcess As Process
        myProcesses = Process.GetProcesses()
        ' Iterate through the process array.
        For Each myProcess In myProcesses
            If Not ret.ContainsKey(myProcess.ProcessName) Then
                ret.Add(myProcess.ProcessName, myProcess.Id)
            End If
            Console.WriteLine(myProcess.ProcessName)
        Next

        Return ret
    End Function

This moved my Form behind the WorkBook.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.