我的excel应用程序上有一个winform,它有一个按钮可以最小化表单。但我也想最小化Excel应用程序,这已经打开了。我制定了下面的代码,但告诉我在赋值之前使用了我的变量。
这是我的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xl As Excel.Application
Me.WindowState = FormWindowState.Minimized
xl.Application.WindowState = XlWindowState.xlMinimized
End Sub
答案 0 :(得分:1)
尝试此操作以查找打开的Excel实例:
Imports System.Runtime.InteropServices
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create an Excel App
Dim excelApp As Microsoft.Office.Interop.Excel.Application = Marshal.GetActiveObject("Excel.Application")
MessageBox.Show(excelApp.Workbooks.Count.ToString)
For i As Integer = 1 To excelApp.Workbooks.Count
MessageBox.Show(excelApp.Workbooks(i).FullName)
Next
excelApp.Visible = False
'do your worh here
excelApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)
End Sub
End Class
答案 1 :(得分:1)
正如肯·怀特正确地说的那样,这个错误非常具有描述性,并且修复很简单:
xl = New Excel.Application()
您对此方面的了解非常少,因此我建议您使用正确的代码(并编辑它以实现您想要的任何代码),以避免将来出现问题:MSDN sample code。