检查工作簿是否已打开,是否为打开的工作表 [英] Checked if workbook is opened, if it is open sheet

查看:81
本文介绍了检查工作簿是否已打开,是否为打开的工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:



以下程序允许我查看Excel是否已打开。如果不是,则打开Excel,转到所选工作簿并激活所请求的工作表。这工作正常,但我突然遇到一个问题,就是如果工作簿已经打开我不想再打开它,只需转到请求的工作表。我试过在下面修改我的代码,但还没有运气。这就是我所拥有的:



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

推荐答案

这样的事情应该可以胜任;)

Something like that should do the job ;)
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 ;)


这篇关于检查工作簿是否已打开,是否为打开的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆