-
Sep 2nd, 2009, 03:12 PM
#1
Thread Starter
New Member
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
-
Sep 2nd, 2009, 03:23 PM
#2
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:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Value = "check" Then '~~> I tried it with notepad :-) dblReturn = Shell("C:\WINDOWS\system32\notepad.exe", vbNormalFocus) If dblReturn = 0 Then ' error MsgBox "There is no known program", vbInformation, "Eve" End If End If '~~> Just add this line else the cell will go in the edit mode Cancel = True End Sub
Last edited by Siddharth Rout; Sep 2nd, 2009 at 03:27 PM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 2nd, 2009, 09:51 PM
#3
Re: Opening an external VBScript program from a VBA sub routine
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
-
Sep 3rd, 2009, 07:09 AM
#4
Thread Starter
New Member
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?
-
Sep 3rd, 2009, 07:13 AM
#5
Thread Starter
New Member
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?
-
Sep 3rd, 2009, 07:25 AM
#6
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
-
Sep 3rd, 2009, 07:35 AM
#7
Thread Starter
New Member
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?
-
Sep 3rd, 2009, 04:14 PM
#8
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
-
Sep 4th, 2009, 08:39 AM
#9
Thread Starter
New Member
Re: Opening an external VBScript program from a VBA sub routine
Yes, here is my whole code in the worksheet:
vb Code:
Sub ErrorRateAnalysis()
Dim excelReport As Workbook
Dim cellValue As String
Dim rng As String
Set excelReport = ActiveWorkbook
Range("R2").Select
rng = ActiveCell.Address
Do While Not ActiveCell.Value = ""
cellValue = ActiveCell.Value
Worksheet_BeforeDoubleClick Range(rng), True
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Value = "check" Then
Shell "wscript.exe ""C:\Documents and Settings\2346570.FXG\Desktop\Sutton\AddressLookup2.vbs"""
End If
Cancel = True
End Sub
-
Sep 4th, 2009, 08:40 AM
#10
Thread Starter
New Member
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.
-
Sep 4th, 2009, 01:00 PM
#11
Re: Opening an external VBScript program from a VBA sub routine
Can you zip and upload your AddressLookup2.vbs file?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 4th, 2009, 01:30 PM
#12
Thread Starter
New Member
Re: Opening an external VBScript program from a VBA sub routine
I'll just post it, it's not long:
vb Code:
On Error Resume Next
Set objWShell = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
Set IE1 = objShellWindows.Item
Set IE2 = objShellWindows.Item
Set IE3 = objShellWindows.Item
Dim Address
Dim strLine
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("address.txt",ForReading)
strLine = objTextFile.ReadLine
Address = Split(strLine, ",")
URL1 = "http://maps.google.com/"
URL2 = "http://maps.google.com/"
IE2.Navigate2 URL2
If err.number <> 0 Then
Set IE2 = wscript.CreateObject("InternetExplorer.Application")
IE2.Visible = True
IE2.Navigate2 URL2
IE2.Height = 1100
IE2.Width = 575
IE2.Left = 575
IE2.Top = 0
End If
IE1.Navigate2 URL1
If err.number <> 0 Then
Do While IE2.Busy
Loop
Set IE1 = wscript.CreateObject("InternetExplorer.Application")
IE1.Visible = True
IE1.Navigate2 URL1
IE1.Height = 432
IE1.Width = 575
IE1.Left = 0
IE1.Top = 0
End If
GoogleLookup(IE1)
GoogleLookup(IE2)
Set IE1 = Nothing
Set IE2 = Nothing
Set objShellWindows = Nothing
Set objShell = Nothing
Private Sub GoogleLookup(ie)
Dim wshShell
Set wshShell = WScript.CreateObject("WScript.Shell")
wshShell.appactivate ("Google Maps") 'Title of the window...
wscript.sleep(100)
wshShell.SendKeys "{TAB}"
wshShell.SendKeys "{TAB}"
wshShell.SendKeys "{TAB}"
wshShell.SendKeys "{TAB}"
wshShell.SendKeys "{TAB}"
wshShell.SendKeys "{TAB}"
wshShell.SendKeys "{TAB}"
wshShell.SendKeys "{TAB}"
ie.Document.All.Item("q").Value = Address(0) & "," & Address(1) & "," & Address(2) & "," & Address(3)
wshShell.SendKeys "{ENTER}"
End Sub
-
Sep 4th, 2009, 02:23 PM
#13
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?
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 4th, 2009, 03:53 PM
#14
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:
for each c in range("R:R") if isempty(c) then exit for if c.value = "check" then Worksheet_BeforeDoubleClick c, True next
anyway why not incorporate the code from the vbs into a vba procedure?
Last edited by westconn1; Sep 4th, 2009 at 04: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
-
Sep 4th, 2009, 03:57 PM
#15
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...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 4th, 2009, 04:22 PM
#16
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
-
Sep 8th, 2009, 10:28 AM
#17
Thread Starter
New Member
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:
Sub ButtonMacro()
'
ActiveSheet.Buttons.Add(ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height).Select
Selection.OnAction = "AddressLookup"
Selection.Characters.Text = "check"
With Selection.Characters.Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 10
End With
End Sub
Sub AddressLookup()
'code in here
End Sub
-
Sep 8th, 2009, 04:33 PM
#18
Re: Opening an external VBScript program from a VBA sub routine
avoid working with selection and active cell as much as possible
vb Code:
set mybut = ActiveSheet.Buttons.Add(ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height) with mybut .onaction = "addresslookup" .characters.text = "check" ' etc 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|