0

我的 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
4

2 回答 2

1

正如 Ken White 正确地说的那样,该错误非常具有描述性,并且修复很简单:

   xl = New Excel.Application()

您在这方面的知识非常低,因此我建议您使用适当的代码(并对其进行编辑以实现您想要的任何内容)以避免将来出现问题:MSDN 示例代码

于 2013-07-31T07:37:43.443 回答
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

取自: http ://social.msdn.microsoft.com/Forums/en-US/1d89ea6d-f396-4a80-8071-9d24a023a254/how-to-search-all-excel-instances-to-see-if-a -特定的工作簿已经打开

于 2013-07-31T02:26:34.267 回答