0

I have a winform on my excel application that has a button to minimize the form. But I also want to minimize the Excel application as well, which is already open. I worked out the code below, but is telling me that my variable is used before being assigned a value.

Here is my code:

    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
Jose M.
  • 2,242
  • 8
  • 44
  • 66
  • 2
    Of course it is - you haven't done anything to actually connect it to Excel. Declaring a variable with a type doesn't automatically make it fully functional as that type. You have to make the variable that has it "already open" available (in scope) to your `Button1_Click` handler and use it instead of declaring a new variable. – Ken White Jul 31 '13 at 02:03
  • Not an expert here on excel stuff but shouldnt it be Dim xl as New Excel.Application? Or reference an existing one? My first thought would be to use interop though. Not sure if you can do this with the office objects. – Mike Cheel Jul 31 '13 at 02:04
  • I did use New Excel.Application but that creates a new instance of excel. Since excel is already open, I want to minimize the open instance. – Jose M. Jul 31 '13 at 02:05
  • Can you post the code where you create the variable that opens Excel? – Nick Jul 31 '13 at 02:07
  • @Nick it appears that is what I am missing. I have not created that variable, nor do I know how to access it. This is my first project with VB.net and Excel so I am fumbling somewhat. – Jose M. Jul 31 '13 at 02:17
  • Are you using VB.NET or VBA? I mean, you have a 'winform' that opens Excel (VB.NET) or you have Excel that opens a form (VBA)? – Nick Jul 31 '13 at 02:19
  • @nick I am using VB.Net. Basically, the user opens Excel through the regular icon on their desktop. The form is opened by the User once Excel is opened to enter information into a spreadsheet. – Jose M. Jul 31 '13 at 02:21
  • Isn't this is what you are looking for (Though C#, and you should be easily able to convert to VB)? http://stackoverflow.com/questions/9254037/how-to-programmatically-minimize-opened-window-folders – Prash Jul 31 '13 at 02:27

2 Answers2

1

Try this to find the open Excel instance:

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

Taken from: http://social.msdn.microsoft.com/Forums/en-US/1d89ea6d-f396-4a80-8071-9d24a023a254/how-to-search-all-excel-instances-to-see-if-a-particular-workbook-is-already-open

Nick
  • 727
  • 10
  • 20
  • ran the code and got a count of 0. But I can see the workbook open. I have to look closer at my form, there is something else going on here that i have not noticed – Jose M. Jul 31 '13 at 02:39
  • You can also try with "FindWindowEx", look at the URL in the answer. It's a little more complicated and a lot less reliable since it's based on the window title, but maybe it will point you in the right direction – Nick Jul 31 '13 at 02:47
  • The problem with your suggestion is that does not fix what the OP is asking. There is no problem with minimising the Excel workbook once it is "properly started" but he didn't do that!!. What he did is equivalent to: Dim test as String = Nothing test.Split() -> you get an error because of intending to access members of an object which is null, rather than an instance of the target class. – varocarbas Aug 01 '13 at 07:53
1

As Ken White rightly said, the error is quite descriptive and the fix straightforward:

   xl = New Excel.Application()

Your knowledge on this front is pretty low, so I recommend you to use a proper code (and edit it to achieve anything you want) in order to avoid future problems: MSDN sample code.

varocarbas
  • 12,354
  • 4
  • 26
  • 37