VBScript to run a file from desktop (any user) - vbscript

I've created a VBScript to run a macro from excel without opening the file.
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\MyUser\Desktop\RM.xlsm'!Module4.Email"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
I want to use this VBScript & XLSM file on different computers, so how can i change this script to work without editing the path every time?
(Maybe a code to run from current folder or a code to run from any user desktop)

If the file will always be on the desktop of every user then you can use environment variables to determine the location.
Set wshShell = CreateObject( "WScript.Shell" )
userName = wshShell.ExpandEnvironmentStrings( "%UserName%" )
path = "'C:\Users\" + userName + "\Desktop\RM.xlsm'!Module4.Email"
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run path
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
(Untested code)
If the file is not on each user's desktop then you'll need to store it in a common location on the network and have each user access it from there, e.g. in .
\\YourFileSever\SharedFiles\RM.xlsm
In practical terms the latter is preferable as it means the workbook is in only one place and when it comes to releasing a new version you only have to update one copy

Related

VBScript setting environment variables but executable not picking up them up

I have the below VBScript (values and names changed)
Dim InstanceName
Set objShell = WScript.CreateObject("WScript.Shell")
Set objEnv = objShell.Environment("USER")
objEnv("PLUGIN") = "plugin"
objEnv("CONF") = "location"
Set ArgObj=Wscript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("path.txt", 1)
AbsolutePath = objTextFile.ReadLine
objTextFile.Close
directory = "C:\Program Files\"&AbsolutePath&"\bin"
executable = directory&"\app-"&InstanceName&".exe"
objShell.CurrentDirectory = directory
objShell.Run Chr(34) & executable & Chr(34), 1, false
Set objShell = Nothing
My program uses the environment variables PLUGIN and CONF. However, the first time the program is launched using the script, it cannot find the environment variables. The subsequent launches work fine as the environment variables are already set.
I can see them when I check in control panel even after the first launch but the executable doesn't seem to pick them up.
How can I make the variables be set correctly for the executable the first time the script is run?
Thanks!
The problem is that the values are not being stored as User environment variables. Reading through the documentation you want to make sure that you set the Environment property of the WScript.Shell object to the correct set of environment variables, as per the documentation the values are;
System
User
Volatile
Process
Would recommend changing the code to use;
Set objEnv = objShell.Environment("SYSTEM")
and go from there.

Script to remove shortcuts won't work with version numbers

I'm running a vbscript to remove desktop shortcuts installed by another program, the problem I have though is one shortcut is being stubborn
set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("Desktop" )
' delete this shortcut
strShortcut = strDesktop & "\Shortcut Name 2.0.lnk"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strShortcut) Then fso.DeleteFile(strShortcut)
I have a feeling it's to do with the version number and decimal point in-between? Can anyone confirm my suspicion as I'm completely out of clues.
Thanks.
The file you want to delete probably doesn't already exist, If fso.FileExists(strShortcut) Then masks that fact.
A shortcut you see in your desktop doesn't have to be in your Desktop folder that you acquired with WshShell.SpecialFolders("Desktop").
There is another location where desktop items are stored as an extension to the all users' desktop directories, but may require administrative privileges to modify, I'm not sure, you need to try.
So, in addition to SpecialFolders("Desktop"), you should also consider the SpecialFolders("AllUsersDesktop") directory.
Set Fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
userDesktop = WshShell.SpecialFolders("Desktop")
publicDesktop = WshShell.SpecialFolders("AllUsersDesktop")
shortcutName = "Shortcut Name 2.0.lnk"
userShortcut = Fso.BuildPath(userDesktop, shortcutName)
publicShortcut = Fso.BuildPath(publicDesktop, shortcutName)
If Fso.FileExists(userShortcut) Then
Fso.DeleteFile userShortcut, True
MsgBox "User shortcut deleted."
End If
If Fso.FileExists(publicShortcut) Then
Fso.DeleteFile publicShortcut, True
MsgBox "Public shortcut deleted."
End If

.GetAbsolutePathName(".") in vbscript does not get correct path when run by task scheduler

I am trying to run a vb script using task scheduler.
I am using the .GetAbsolutePathName(".") to get the full path of the script.
When I run the script manually, it was able to get the correct path. But when run as scheduled task, it outputs "C:\Windows\System32\ as the path.
I am using the path name to get the full path of an Excel file that I would like to run. The excel file is saved within the same path as the script.
Below is my code:
Set fso = CreateObject("Scripting.FileSystemObject")
scurDir = fso.GetAbsolutePathName(".")
set fso = nothing
Set myxlApplication = CreateObject("Excel.Application")
wscript.echo scurDir & "\OOO Automation Tool.xlsm"
Set myWorkBook = myxlApplication.Workbooks.Open( scurDir & "\OOO Automation Tool.xlsm" )
myxlApplication.Visible = False
myxlApplication.Wait(Now + TimeValue("0:00:10"))
'Run routine
myWorkBook.Application.Run "MOutofOffice.pDetectIdleTime"
'Close application
myxlApplication.Quit
'Release objects
set myxlApplication = nothing
set myWorkBook = nothing
Can you help me get the correct path?
You can try with
With WScript.CreateObject("Scripting.FileSystemObject")
WScript.Echo .GetFile(WScript.ScriptFullName).ParentFolder.Path
End With
You need the path where the script is stored, not the current active directory, what you retrieve with the "." reference.

Creating folder in network share fails with "bad path" error

I have a network share already setup. I am trying to create a VBScript that will create a folder in the share called computername, which is the PC's name. The script will run locally on the PC, access the share and create the folder in the share.
My error is "bad path". I'm guessing I can't just state the network share path?
My script is below:
Dim objShell
Set oWS = WScript.CreateObject("WScript.Shell")
Set objShell = Wscript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
computername = oWS.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
If NOT (objFSO.FolderExists("\\ServerPath\Share$" + computername)) Then
objFSO.CreateFolder("\\ServerPath\Share$" + computername)
End If
You're lacking a backslash between the name of the share and the name of the folder you want to create. Also, I'd recommend using a variable for the path, so you don't have to construct it multiple times.
Change this:
If NOT (objFSO.FolderExists("\\ServerPath\Share$" + computername)) Then
objFSO.CreateFolder("\\ServerPath\Share$" + computername)
End If
into this:
path = "\\ServerPath\Share$\" & computername
If NOT objFSO.FolderExists(path) Then
objFSO.CreateFolder(path)
End If

File wont move to folder vbs

So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).

Resources