Opening an external VBScript program from a VBA sub routine-VBForums
Results 1 to 18 of 18

Thread: Opening an external VBScript program from a VBA sub routine

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Opening an external VBScript program from a VBA sub routine

    Hi, I want to have a sub routine that will run through an excel spreadsheet and whenever a cell has "check" then it will let that cell be double-clicked to open up an external VBscript that I have written. How can I do this through VBA? This is what I have so far, but I think I was mixing VBscript with VBA syntax.


    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    If Target.Value = "check" Then
    dblReturn = Shell("G:\VSNCOMM\Sutton\AddressLookup.vbs", vbNormalFocus)
    If dblReturn = 0 Then ' error
    MsgBox "There is no known program", vbInformation, "Eve"
    End If
    End If

    End Sub

  2. #2
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Opening an external VBScript program from a VBA sub routine

    Welcome to the VBF

    The above code is absolutely fine for VBA. i juste tested it. What does the VBS do?

    vb Code:
    1. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    2.     If Target.Value = "check" Then
    3.         '~~> I tried it with notepad :-)
    4.         dblReturn = Shell("C:\WINDOWS\system32\notepad.exe", vbNormalFocus)
    5.         If dblReturn = 0 Then ' error
    6.             MsgBox "There is no known program", vbInformation, "Eve"
    7.         End If
    8.     End If
    9.     '~~> Just add this line else the cell will go in the edit mode
    10.     Cancel = True
    11. End Sub
    Last edited by Siddharth Rout; Sep 2nd, 2009 at 04:27 PM.
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: Opening an external VBScript program from a VBA sub routine

    What does the VBS do?
    calls the scripting host

    try like
    shell "wscript.exe ""C:\Documents and Settings\pete\My Documents\basic\vbs\typeof.vbs"""
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: Opening an external VBScript program from a VBA sub routine

    The original code works for notepad.exe, but it doesn't work when I want to open a .vbs file. Is there any reason why it won't open a .vbs program?

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: Opening an external VBScript program from a VBA sub routine

    Also, I want to be able to have the code loop through the spreadsheet and then have the option in each of the cells with "check" in them to double-click and open the program. This code, when tested with notepad, is just opening up notepad everytime it comes across "check." How can I adapt it?

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: Opening an external VBScript program from a VBA sub routine

    so what is the question?

    the shell cammond i posted works for any vbs script
    you must enclose the path to the script file in quotes as per the sample

    use like this in a loop

    shell "wscript.exe " & chr(34) & cells(x, y) & chr(34)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: Opening an external VBScript program from a VBA sub routine

    The call to an external program works fine, it's just that sub routine is executing the program immediately when it sees "check" in a cell, where I want it to give the cell the option to be double-clicked to open the program. The question was, how can I change the program to do this instead of immediately executing the item in the Shell path?

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: Opening an external VBScript program from a VBA sub routine

    you have the code in the before double click event, as posted?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: Opening an external VBScript program from a VBA sub routine

    Yes, here is my whole code in the worksheet:

    vb Code:
    1. Sub ErrorRateAnalysis()
    2.  
    3. Dim excelReport As Workbook
    4. Dim cellValue As String
    5. Dim rng As String
    6.  
    7.  
    8. Set excelReport = ActiveWorkbook
    9.  
    10. Range("R2").Select
    11. rng = ActiveCell.Address
    12.  
    13. Do While Not ActiveCell.Value = ""
    14.     cellValue = ActiveCell.Value
    15.     Worksheet_BeforeDoubleClick Range(rng), True
    16.     ActiveCell.Offset(1, 0).Select
    17. Loop
    18.  
    19.  
    20.  
    21. End Sub
    22.  
    23. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    24.  
    25.   If Target.Value = "check" Then
    26.     Shell "wscript.exe ""C:\Documents and Settings\2346570.FXG\Desktop\Sutton\AddressLookup2.vbs"""
    27.   End If
    28.   Cancel = True
    29.  
    30. End Sub

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: Opening an external VBScript program from a VBA sub routine

    When I run the code, I have a ton of instances of the program that I called, and it owns my computer. It all executes immediately.

  11. #11
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Opening an external VBScript program from a VBA sub routine

    Can you zip and upload your AddressLookup2.vbs file?
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  12. #12

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: Opening an external VBScript program from a VBA sub routine

    I'll just post it, it's not long:

    vb Code:
    1. On Error Resume Next
    2.  
    3. Set objWShell = CreateObject("Wscript.Shell")
    4. Set objShell = CreateObject("Shell.Application")
    5. Set objShellWindows = objShell.Windows
    6. Set IE1 = objShellWindows.Item
    7. Set IE2 = objShellWindows.Item
    8. Set IE3 = objShellWindows.Item
    9.  
    10. Dim Address
    11. Dim strLine
    12.  
    13. Const ForReading = 1
    14. Set objFSO = CreateObject("Scripting.FileSystemObject")
    15. Set objTextFile = objFSO.OpenTextFile("address.txt",ForReading)
    16. strLine = objTextFile.ReadLine
    17. Address = Split(strLine, ",")
    18.  
    19.  
    20.  
    21. URL1 = "http://maps.google.com/"
    22. URL2 = "http://maps.google.com/"
    23.  
    24.  
    25. IE2.Navigate2 URL2
    26. If err.number <> 0 Then
    27.  Set IE2 = wscript.CreateObject("InternetExplorer.Application")
    28.  IE2.Visible = True
    29.  IE2.Navigate2 URL2
    30.  IE2.Height = 1100
    31.  IE2.Width = 575
    32.  IE2.Left = 575
    33.  IE2.Top = 0
    34. End If
    35.  
    36.  
    37. IE1.Navigate2 URL1
    38. If err.number <> 0 Then
    39.  Do While IE2.Busy
    40.  Loop
    41.  Set IE1 = wscript.CreateObject("InternetExplorer.Application")
    42.  IE1.Visible = True
    43.  IE1.Navigate2 URL1
    44.  IE1.Height = 432
    45.  IE1.Width = 575
    46.  IE1.Left = 0
    47.  IE1.Top = 0
    48. End If
    49.  
    50.  
    51. GoogleLookup(IE1)
    52. GoogleLookup(IE2)
    53.  
    54.  
    55. Set IE1 = Nothing
    56. Set IE2 = Nothing
    57. Set objShellWindows = Nothing
    58. Set objShell = Nothing
    59.  
    60.  
    61.  
    62. Private Sub GoogleLookup(ie)
    63.  
    64. Dim wshShell
    65. Set wshShell = WScript.CreateObject("WScript.Shell")
    66.  
    67. wshShell.appactivate ("Google Maps") 'Title of the window...
    68. wscript.sleep(100)
    69.  
    70. wshShell.SendKeys "{TAB}"
    71. wshShell.SendKeys "{TAB}"
    72. wshShell.SendKeys "{TAB}"
    73. wshShell.SendKeys "{TAB}"
    74. wshShell.SendKeys "{TAB}"
    75. wshShell.SendKeys "{TAB}"
    76. wshShell.SendKeys "{TAB}"
    77. wshShell.SendKeys "{TAB}"
    78. ie.Document.All.Item("q").Value = Address(0) & "," & Address(1) & "," & Address(2) & "," & Address(3)
    79. wshShell.SendKeys "{ENTER}"
    80.  
    81. End Sub

  13. #13
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Opening an external VBScript program from a VBA sub routine

    When I run the code, I have a ton of instances of the program that I called, and it owns my computer. It all executes immediately.
    Ok, could you also elaborate on this?
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: Opening an external VBScript program from a VBA sub routine

    when does the code run if it does not wait for double click what is triggering the procedure?
    i did a small test and it only ran when double clicked

    post a sample workbook where this happens and someone will test it, note i don't have 2007, so i can only test prior to that

    alternatively add a command button to call the procedure, instead of relying on the double click event

    i just noticed you are calling the double click event from within the loop, so it will keep running as it always passes r2 as the target, for each non empty cell

    avoid using selection and active cell, use a specified range

    vb Code:
    1. for each c in range("R:R")
    2.     if isempty(c) then exit for
    3.     if c.value = "check" then Worksheet_BeforeDoubleClick c, True
    4. next

    anyway why not incorporate the code from the vbs into a vba procedure?
    Last edited by westconn1; Sep 4th, 2009 at 05:03 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Opening an external VBScript program from a VBA sub routine

    post a sample workbook where this happens and someone will test it, note i don't have 2007, so i can only test prior to that
    Pete I am sure he can post a 2007 file saved as 2003 file...
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: Opening an external VBScript program from a VBA sub routine

    Pete I am sure he can post a 2007 file saved as 2003 file...
    no need now the problem is in his loop
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: Opening an external VBScript program from a VBA sub routine

    Thanks for pointing out the problem, it works now. However, I still have a problem opening up the external VBS program when the cell is clicked. I have copy and pasted the VBS code into the same Worksheet's VBA editor, so that the whole script is just a new sub routine in the worksheet. However, it gives me an error that the macro is not found, when it should be right there. My code for executing the script is below.

    vb Code:
    1. Sub ButtonMacro()
    2. '
    3.     ActiveSheet.Buttons.Add(ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height).Select
    4.     Selection.OnAction = "AddressLookup"
    5.     Selection.Characters.Text = "check"
    6.     With Selection.Characters.Font
    7.         .Name = "Arial"
    8.         .FontStyle = "Regular"
    9.         .Size = 10
    10.     End With
    11. End Sub
    12.  
    13.  
    14. Sub AddressLookup()
    15.  
    16. 'code in here
    17.  
    18. End Sub

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,096

    Re: Opening an external VBScript program from a VBA sub routine

    avoid working with selection and active cell as much as possible
    vb Code:
    1. set mybut = ActiveSheet.Buttons.Add(ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height)
    2. with mybut
    3.    .onaction = "addresslookup"
    4.    .characters.text = "check"  
    5.    ' etc
    6. end with
    is the addresslookup code in the same sheet's code module as the button?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.