Link to home
Start Free TrialLog in
Avatar of cleaclev
cleaclev

asked on

Open form2.vb from vbscript

Hi,

I`ve 2 forms in Visual Basic. Form1 is a window with two buttons for choosing a vbscript, and the second form is a progressbar who goes automaticly from left to right. I have already the right commands for closing the forms, but i want to open the second form with the progressbar when the script goes running.

Is it possible to open only the second form directly from the vbscript?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

What version VB (not the script) are you working with?
Avatar of cleaclev
cleaclev

ASKER

With Microsoft Visual Basic 2008 Express Edition
With VB.Net 2008, make a MINOR modification to your app and you'll be in business.  Go into Project --> Properties and CHECK the "Make Single Instance Application" box.  Then, in the bottom right of that SAME TAB, click on the "View Application Events" button (this will take you to the editor).

Across the top, change the LEFT dropdown from "(General)" to "(MyApplication Events)".  Change the RIGHT dropdown from "(Declarations)" to "StartupNextInstance".

You will get a code stub like this:

        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance

        End Sub

Read up on this event here:
http://msdn.microsoft.com/en-us/library/b9z4eyh8(VS.80).aspx
Good reading to:
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.80).aspx

So, in the VBScript, you would simply "rerun" the .net app AGAIN, but pass in some kind of message to tell it to open Form2:

    Set wshShell = CreateObject("WScript.Shell")
    wshShell.Run "YourVisualBasicDotNetAppHere.exe /Form2"

This will cause the StartupNextInstance() event in the ALREADY RUNNING VB.Net app to fire.  You can get the message(s) passed using "e.CommandLine":

Namespace My
 
    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
 
        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            For Each s As String In e.CommandLine
                If s.ToLower = "/form2" Then
                    Form2.Show()
                    Exit For
                End If
            Next
        End Sub
 
    End Class
 
End Namespace

Open in new window

It does not work, Everytime when i run the script it will open the first form. I give you the script so you can edit it
   rootfolder = Inputbox("Vul hier de locatie in om de profielgrootte te weergeven: " & _
                         "\\Servernaam\Profielenmap)" & chr(10) & chr(10), _
                         "Getfoldersize", "\\server04\profielen$\")
 
   outputfile = "c:\foldersize_" & Day(now) & -Month(now) & -Year(now) & ".xls"
 
   Set fso = CreateObject("scripting.filesystemobject")
   if fso.fileexists(outputfile) then fso.deletefile(outputfile)
 
'Create Excel workbook
   set objXL = CreateObject( "Excel.Application" )
   objXL.Visible = False
   objXL.WorkBooks.Add
 
'Counter 1 for writing in cell A1 within the excel workbook
   icount = 1
   
'Run`s the progressbar
   Set wshShell = CreateObject("WScript.Shell")
   wshShell.Run "Foldersize.exe /Form2"
 
'Run checkfolder
   CheckFolder (FSO.getfolder(rootfolder))
 
Sub CheckFolder(objCurrentFolder)
 
       For Each objFolder In objCurrentFolder.SubFolders
	 On Error Resume Next
         FolderSize = objFolder.Size
         Tmp = (FormatNumber(FolderSize, 0, , , 0)/1024)/1024
         ObjXL.ActiveSheet.Cells(icount,1).Value = objFolder.Path
         ObjXL.ActiveSheet.Cells(icount,2).Value = Tmp
         'Wscript.Echo Tmp & " " & objFolder.Path
	 'raise counter with 1 for a new row in excel 
         icount = icount + 1
       Next
       
End Sub
 
 
'sort data in excel
objXL.ActiveCell.CurrentRegion.Select
objXL.Selection.Sort objXL.Worksheets(1).Range("B1"), _
                   2, _
                   , _
                   , _
                   , _
                   , _
                   , _
                   0, _
                   1, _
                   False, _
                   1
 
'Lay out for Excel workbook 
   objXL.Range("A1").Select
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert
   objXL.Selection.EntireRow.Insert
 
   objXL.Columns(1).ColumnWidth = 60
   objXL.Columns(2).ColumnWidth = 15
   objXL.Columns(2).NumberFormat = "#,##0.0"
   objXL.Range("B1:B1").NumberFormat = "mm-dd-yyyy"
   objXL.Range("A1:B5").Select
   objXL.Selection.Font.Bold = True
   objXL.Range("A1:B3").Select
   objXL.Selection.Font.ColorIndex = 5
   objXL.Range("A1:A1").Select
   objXL.Selection.Font.Italic = True
   objXL.Selection.Font.Size = 16
   ObjXL.ActiveSheet.Cells(1,1).Value = "Foldergrootte lijst" 
   ObjXL.ActiveSheet.Cells(1,2).Value = Day(now) & "-" & Month(now) & "-"& Year(now)
   ObjXL.ActiveSheet.Cells(3,1).Value = UCase(rootfolder)
   ObjXL.ActiveSheet.Cells(5,1).Value = "Folder"
   ObjXL.ActiveSheet.Cells(5,2).Value = "Totaal (MB)"
 
 
'Finally close the workbook
   ObjXL.ActiveWorkbook.SaveAs(outputfile)
   ObjXL.Application.Quit
   Set ObjXL = Nothing
 
 
'Kill foldersize.bat
   set sh = WScript.CreateObject("WScript.Shell")
   sh.Run "kill-foldersize.bat", 0, true
 
 
'Message when finished
   Set WshShell = CreateObject("WScript.Shell")
   Finished = Msgbox ("Script succesvol uitgevoerd, resultaten kunnen worden bekeken in " & Chr(10) _
                     & outputfile & "." & Chr(10) & Chr(10) _
                     & "Wilt u de resultaten nu bekijken?", 65, "Script succesvol uitgevoerd!")
   if Finished = 1 then WshShell.Run "excel " & outputfile

Open in new window

And here is the code of the progressbarform
Public Class Form2
 
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    End Sub
 
    Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click
 
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Shell("\\server04\users$\gilde\Desktop\Foldersize checker\kill-wscript.bat", AppWinStyle.Hide, True, 1) 'Kill wscript.exe
        Shell("\\server04\users$\gilde\Desktop\Foldersize checker\kill-excel.bat", AppWinStyle.Hide, True, 1) 'Kill excel.exe
        End
 
    End Sub
 
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
        End
 
    End Sub
End Class

Open in new window

...and did you implement the changes so that you can handle the StartupNextInstance() event in your .Net exe?
(it's not being shown in your posted code)
Do you mean this code?

I`ve already changed. If I changed the good one..

ApplicationEvents.vb  |
                                  \/
Namespace My
 
    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication
 
        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            For Each s As String In e.CommandLine
                If s.ToLower = "/form2" Then
                    Form2.Show()
                    Exit For
                End If
            Next
        End Sub
 
    End Class
 
End Namespace

Open in new window

It works for me in my tests...

Is your VBScript and VB.Net Exe in the same folder?

I changed the code to:

        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            For Each s As String In e.CommandLine
                If s.ToLower = "/form2" Then
                    Form2.Show()
                    If Form2.WindowState = FormWindowState.Minimized Then
                        Form2.WindowState = FormWindowState.Normal
                    End If
                    Form2.BringToFront()
                    Form2.Activate()
                    Exit For
                End If
            Next
        End Sub


Yes it is in the same folder.

I`ve changed the code from ApplicationEvents.vb to the code you give me. But it`s still not working. My VBScript opens everytime my first form.

Thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will try that thanks.