检查打开的工作簿,获取COMException错误 [英] Checking for open workbook, getting COMException error
问题描述
您好:
我编写了一个程序,可以让我检查工作簿是否已打开。如果打开工作簿,则转到指定的工作表,否则,打开工作簿并转到工作表。
我有2个问题,一个是在工作簿尚未打开时两次打开相同的工作簿。其次,在它打开并关闭第二个实例后,我收到此错误:
System.Runtime.InteropServices.COMException未处理
ErrorCode = -2146827284
HResult = -2146827284
消息=来自HRESULT的异常:0x800A03EC
错误是在粗线上:
如果IsWorkbookAlreadyOpen(xlApp,workbookName)那么
xlBook = xlApp.Workbooks.Open( workbookName )
这是我的其余代码:
私有共享函数IsWorkbookAlreadyOpen(app as Excel.Application,workbookName As String)As Boolean
Dim isAlreadyOpen As Boolean = True
尝试
app.Workbooks.Open(workbookName)
Catch theException As Exception
isAlreadyOpen = False
结束尝试
返回isAlreadyOpen
结束函数
Private Sub btnMinSum maryWorksheet_Click(sender As Object,e As EventArgs)处理btnMinSummaryWorksheet.Click
'此过程在单击btnOpenSummaryWorksheet按钮时运行。调用
'Sub程序打开Summary Worksheet Dashboard
Dim xlApp As New Excel.Application
xlApp.Visible = True
Dim xlBook as Excel .Workbook
Dim workbookName =F:\ Test Environment \ Compensation Workbook \ Compensation Workbook \bin\Debug\2011.1004.Compensation Template.xlsx
If IsWorkbookAlreadyOpen(xlApp,workbookName)然后
xlBook = xlApp.Workbooks.Open(workbookName)
Else
xlBook = xlApp.Workbooks.Open(workbookName)
End if
Dim xlSheet作为Excel.Worksheet
xlSheet = CType(xlBook.Sheets(SummaryWorksheet),工作表)
xlSheet.Activate()
Me.Close()
End Sub
我在这里做错了什么?
< blockquote>您正确地发现Excel Application对象具有 Workbooks
对象。
现在正确使用它:它是 IEnumerable
。枚举其中的所有工作簿
,并检查每个工作簿
对象(如果它是你的)。
提示:使用表示
或表示
阻止。
Hello:
I wrote a procedure that allows me to check if a workbook is open. If the workbook is opened, then go to the specified sheet, otherwise, open the workbook and go to the sheet.
I am having 2 issues, one is that is opening the same workbook twice when the workbook is not already opened. Second, after it opens and I close the second instance, I get this error:
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HResult=-2146827284
Message=Exception from HRESULT: 0x800A03EC
The error is on the bold line:
If IsWorkbookAlreadyOpen(xlApp, workbookName) Then xlBook = xlApp.Workbooks.Open(workbookName)
Here is the rest of my code:
Private Shared Function IsWorkbookAlreadyOpen(app As Excel.Application, workbookName As String) As Boolean Dim isAlreadyOpen As Boolean = True Try app.Workbooks.Open(workbookName) Catch theException As Exception isAlreadyOpen = False End Try Return isAlreadyOpen End Function 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 New Excel.Application xlApp.Visible = True Dim xlBook As Excel.Workbook Dim workbookName = "F:\Test Environment\Compensation Workbook\Compensation Workbook\bin\Debug\2011.1004.Compensation Template.xlsx" If IsWorkbookAlreadyOpen(xlApp, workbookName) Then xlBook = xlApp.Workbooks.Open(workbookName) Else xlBook = xlApp.Workbooks.Open(workbookName) End If Dim xlSheet As Excel.Worksheet xlSheet = CType(xlBook.Sheets("SummaryWorksheet"), Worksheet) xlSheet.Activate() Me.Close() End Sub
What am i doing wrong here?
You correctly found out that the Excel Application object has aWorkbooks
object.
Now use it correctly: it is anIEnumerable
. Enumerate all of theWorkbook
s in it, and check eachWorkbook
object if it is yours.
Hint: use afor
orforeach
block.
这篇关于检查打开的工作簿,获取COMException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!