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

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.