I have created a windows forms application
Is there a way i can make a VBS script open the application and pass a parameter to it?
Thank You
This worked for me, Thanks!
Sub WhatEver()
Dim strArg() as string
strArg = Command().Split(" ")
' strArg(0) is first argument and so on
'
'
End Sub
Related
I am looking for a way to copy the text "Hello world" to the clipboard using either VBS or batch. I've done a lot of research but couldn't find anything.
As Squashman proposed you can use :
echo string|clip
thought this will set one enter at the end of the string.
To strip the enter you can use this:
mshta "javascript:Code(close(clipboardData.setData('text','string')));"
You can do it with an html object to retrieve the contents of the clipboard:
' Get clipboard text
Set objHTML = CreateObject("htmlfile")
Set Ws = CreateObject("WScript.Shell")
Clipboardtext = objHTML.ParentWindow.ClipboardData.GetData("text")
MsgBox Clipboardtext,vbInformation,"Get Clipboard"
sText = "Hello World"
'Here we set the string sText into Clipboard
Ws.Run "mshta.exe ""javascript:clipboardData.setData('text','" & Replace(Replace(sText, "\", "\\"), "'", "\'") & "');close();""", 0, True
I'm afraid this isn't easily achievable using batch or VBScript.
To access clipboard, you need to use a series of Windows APIs, which is not directly possible with either batch or VBScript. Your best bet could be writing a CLI program (helper program), then call it in yout batch / VBS.
I have a batch script MyBatch.bat, it successfully calls MyVbScript.vbs by the statement:
cscript //nologo %~dp0\MyVbScript.vbs %NewPort%
Where NewPort is a environment variable value.
MyVbScript.vbs is getting called successfully (tested) and value of NewPort is passed to VBS successfully (tested) from batch script.
But I am NOT able to generate the URL for launching in default browser. I want to generate the URL like http://localhost:7006/MyWebApplication where 7006 is value of NewPort variable.
MyVbScript.vbs contains:
Option Explicit
Dim wsh
Dim port
Dim myNum
Set wsh=WScript.CreateObject("WScript.Shell")
port = WScript.Arguments(0)
'MsgBox port//For testing
wsh.Run " "http://localhost:"&port&"/MyWebApplication" "
'End of VB script
How to generate the URL and launch it in default browser. I don't want to specify Chrome or Mozilla and IE because user may not have a particular browser.
The following worked:
wsh.Run "http://localhost:"&port&"/MyWebApplication"
Any seasoned programmer can suggest other code too if I have written more LOC unnecessarily.
Batch Code (option 1)
I typically used this to simply call vbs from batch, no variables included.
#echo off
myVBSpath=\\UNC\path
set vbsFileName=myVBS.vbs
cscript "%myVBSpath%\%vbsFileName%"
pause
Batch Code (option 2)
I typically use this option when passing a variable from batch to VBS... including the variable name & value after the vbs file path and name.
#echo off
set myVBSpath=\\UNC\path
set vbsFileName=myVBS.vbs
cscript //NoLogo "%myVBSpath%\%vbsFileName%" /attachment: "%myAttachment%"
pause
Resulting Batch Error Message
Regardless of which option I use above, I get the following error message:
\\UNC\path\myVBS.vbs(11, 1) Microsoft VBScript runtime error: Argument
not optional
VBS Code
Dim xlApp
Dim xlBook
'Dim attachmentFullName
'attachmentFullName = WScript.Arguments.Named("attachment")
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("\\UNCpath\myExcelFile.xlsm", 0, True)
xlApp.DisplayAlerts = False
xlApp.Run "myMacro" ', Cstr(attachmentFullName)
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Troubleshooting
I do not require a variable to be passed to vbs, so batch code (option 1) should be the way to go, and its how I started. When I got the runtime error message above, I tried to pass a false variable using option 2 just to see if that provided the extra argument the cscript command is looking for. Unfortunately, option 2 gave the same error message.
This is a fairly straight forward batch script, so I'm really at a loss and feel like maybe I've just been staring at it too long, making it harder than it needs to be, and I'm just missing something simple.
Would any of you be kind enough to give me a fresh set of eyes and give me an idea of what is causing my runtime error?
Thank you!
My solution was likely a one off - but here is a quick summary of troubleshooting steps for those noticing this error in their batch script:
Check the cscript line to make sure no arguments are being accidentally passed (or not passed).
Check VBS to ensure the code is no longer requesting an argument variable (or is accepting one if passed) -- thanks #Hackoo
Check VBA to ensure the Sub is not requesting a variable -- thanks #DavyC!
Is there a way to launch two Explorer windows side-by-side (vertically tiled) with a Batch script?
If not, how might I do this with VBS?
I have modified the VBS script above by Hackoo to do exactly what the OP wants...
The comments in the script explain exactly what it will do.
If the two windows don't set into correct position, increase the 'Sleep' time and try again.
If you want a horizontal split, use 'objShell.TileHorizontally'.
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Launches two Explorer windows side-by-side filling the screen dimensions.
''' Minimizes all current open windows before launch; if this is not done,
''' the current open windows will also be resized along with our two windows.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Calc,AppData,objShell
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Set objShell = CreateObject("shell.application")
objShell.MinimizeAll
Call Explore(Calc)
WScript.Sleep 800
Call Explore(AppData)
WScript.Sleep 800
objShell.TileVertically
Set objShell = nothing
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************
This might be in the same category as your question. :)
How can a batch file run a program and set the position and size of the window?
Unfortunately it seems that its not possible without any external third part software in batch. Probably easier in VBS - if so the answer should be in the link.
Try this code :
Option Explicit
Dim Calc,AppData
Calc = "%windir%\system32\calc.exe"
AppData = "%AppData%"
Call Explore(Calc)
Call Explore(AppData)
'*****************************************************
Function Explore(Path)
Dim ws
set ws = CreateObject("wscript.shell")
Explore = ws.run("Explorer /n,/select,"& Path &"")
End Function
'*****************************************************
Take the following script that I have:
x=msgbox ("Do you want to recycle the Premiere Pro Media Cache?" ,4, "Recycle Premiere Pro Media Cache")
If box =6 Then
CreateObject("wscript.shell").run "C:\Expedited\Scripts\PrMCRecycler1"
End If
My goal is to get this VBS file (which brings up a message box) to run the batch file (the same way it would run when double-clicking it) when the yes button is pressed. I'm not sure what I'm doing wrong above. When I click No, nothing needs to happen, so I didn't specify anything for it.
Basically, since it brings up a yes/no message box, I just need to make the yes button execute the specified batch file. I could really use some assistance in figuring out what's wrong. When I try the code listed above, nothing happens upon choosing yes (besides the dialogue box going away).
Try this example and change the path of your batch file.
Option Explicit
Dim ws,Question,PathProgram
Set ws = CreateObject("wscript.shell")
'change the path of your batch file
PathProgram = "C:\Program Files\Internet Explorer\iexplore.exe"
Question = Msgbox("Do you want to recycle the Premiere Pro Media Cache?",VbYesNO + VbQuestion, "Recycle Premiere Pro Media Cache")
If Question = VbYes Then
ws.run DblQuote(PathProgram)
End If
'***************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'***************************************