Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open form2.vb from vbscript

Posted on 2009-05-14
11
Medium Priority
?
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 86

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 86

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 86

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 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 750 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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