Solved

Open form2.vb from vbscript

Posted on 2009-05-14
11
529 Views
Last Modified: 2012-05-07
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?
0
Comment
Question by:cleaclev
  • 6
  • 5
11 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24387779
What version VB (not the script) are you working with?
0
 

Author Comment

by:cleaclev
ID: 24392669
With Microsoft Visual Basic 2008 Express Edition
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24396889
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

0
 

Author Comment

by:cleaclev
ID: 24410795
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

0
 

Author Comment

by:cleaclev
ID: 24410810
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

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24412602
...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)
0
 

Author Comment

by:cleaclev
ID: 24419106
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

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24419205
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


0
 

Author Comment

by:cleaclev
ID: 24419625
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
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24422856
My recommendation is to get the approach to work INDEPENDENTLY of your current problem to make sure that you've got the code in all the proper places.  Make a bare bones .Net app with just two forms and the StartupNextInstance() code.  Then make a bare bones VBScript that only runs the .Net app with the command line parameter.  Make sure the .Net app is running already before the VBScript is run.  If you can get it to work by itself then maybe you'll be able to see how to apply it to your existing project...
0
 

Author Closing Comment

by:cleaclev
ID: 31581479
I will try that thanks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now