Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello:

The below procedure allows me to check to see if Excel is opened. If it is not, then it opens Excel, goes to the selected workbook and activates the requested sheet. This works fine, but I jut ran into a problem, that is if the workbook is already opened I don,t want to open it again, simply go to the sheet requested. I have tried modifying my code below, but no luck yet. Here is what I have:

<pre>    Private Sub btnMinSummaryWorksheet_Click(sender As Object, e As EventArgs) Handles btnMinSummaryWorksheet.Click
        'This procedure runs when the btnOpenSummaryWorksheet button is clicked. Calls the
        'Sub procedure opens the Summary Worksheet Dashboard

        Dim xlApp As Excel.Application

        Try
            'get an existing excel.application object
            xlApp = CType(GetObject(, "Excel.Application"), Application)
        Catch ex As Exception
            'no existing excel.application object - create a new one
            xlApp = New Excel.Application
        End Try

        Dim xlBook As Excel.Workbook
        Dim xlWBName As String = "2011.1004.Compensation Template"


        xlApp.Visible = True
        xlBook = xlApp.Workbooks.Open("F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\" & xlWBName & ".xlsx")
        Dim xlSheet As Excel.Worksheet
        xlSheet = CType(xlBook.Sheets("SummaryWorksheet"), Worksheet)

        xlSheet.Activate()
  
    End Sub
Posted
Comments
ZurdoDev 11-Sep-13 12:27pm    
Don't you just do xlBook.Sheets("SummaryWorksheet").Activate()

1 solution

Something like that should do the job ;)
VB
Private Sub btnMinSummaryWorksheet_Click(sender As Object, e As EventArgs) Handles btnMinSummaryWorksheet.Click
       'This procedure runs when the btnOpenSummaryWorksheet button is clicked. Calls the
       'Sub procedure opens the Summary Worksheet Dashboard

       Dim xlApp As Excel.Application

       Try
           'get an existing excel.application object
           xlApp = CType(GetObject(, "Excel.Application"), Application)
       Catch ex As Exception
           'no existing excel.application object - create a new one
           xlApp = New Excel.Application
       End Try

       Dim xlBook As Excel.Workbook
       Dim xlWBName As String = "2011.1004.Compensation Template"


       xlApp.Visible = True
       Try
               'get the opened workbook
                xlBook = xlApp.Workbooks(xlWBName & ".xlsx")
       Catch ex As Exception
               'open it
                xlBook = xlApp.Workbooks.Open("F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\" & xlWBName & ".xlsx")
       End Try

       Dim xlSheet As Excel.Worksheet
       xlSheet = CType(xlBook.Sheets("SummaryWorksheet"), Worksheet)

       xlSheet.Activate()

   End Sub


Another way is to iterate through the collection of Workbooks ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900