VBA calling vbs file form Program Files, won't call - vba

I currently have the following code to call a vbs file that is in a folder within Program Files. It was originally in a different location (within the same folder) and it would work, but now there is a padlock symbol next to the folder within Program Files, and after changing the location (VBA updated for this as well), it won't call the file. I'm wondering why this is happening.
Sub ChangeThemeBasic()
Dim filespe As String
filespe = "cmd.exe /c C:\Program Files\Theme Changer\ChangeTheme.vbs"
X = Shell(filespe, 1)
End Sub
EDIT:
This works for some reason, I don't know why though:
Sub Test()
Shell "Explorer.exe ""C:\Program Files\Theme Changer\ChangeTheme.vbs""",1
End Sub

From comments
Sub ChangeThemeBasic()
Dim filespe As String
filespe = "cmd.exe /c " & Environ("AppData") & "\Theme Changer\ChangeTheme.vbs"
X = Shell(filespe, 1)
End Sub

Related

Filesystemobject Textstream, immediately disappears upon executing Textstream.Close (.vbs extension being created)

I have a situation that is really flummoxing me. Simple code I've used for years is failing in the weirdest way. I have a feeling the cause is related to either anti-virus junk or GPO, but, even those, I have seen them operate before on this scenario--but nothing like how I am seeing it now.
Note - this code has been working perfectly for several people, until one end-user got a new Surface laptop from I.T., purportedly for better compatibility with Teams and 365. ALL users (working, non-working) are on Windows 10.
Scenario:
I'm using Scripting.Filesystemobject
setting an object variable (Textstream intent), as fso.createtextfile
The filepath (name) I am creating is actually filename.vbs...At the moment this line executes, I can see the vbs file successfully in the folder
I use Textstream.Write to put some content in the file
I then use Textstream.Close (normally at this point you get a solid, stable, useable file). Immediately upon execution of the last line, Textstream.Close, the file DISAPPEARS from the folder-GONE.
The folder I'm writing to is the same as Start > Run > %appdata%
I've also tried this in Documents folder (Environ$("USERPROFILE") & "\My Documents") and get the exact same result
I've seen group policies and AV stuff that will prevent VBS from running, but that isn't my case--I've tested with this user, and she has no problem:
Creating a txt file in either of those folders
Manually creating a .vbs file in either of those folders
Even RUNNING the resulting vbs file in either folder
But somehow when I programmatically create .VBS in code, the second I close the textstream, the file is gone from the folder.
Any insight? The internet searches I did were void of all information on this scenario!! It would take me 2 weeks to open a ticket and get any help from I.T.
This is Excel VBA, but I highly doubt the problem has anything to do with Excel nor VBA...this is standard usage of windows scripting.filesystemobject:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'initiate full backup vbs script:
Dim ts As Object, fso As Object, strScriptText As String, strScriptPath As String
'populate our variable with the full text of the script: found on QLoader in this range:
strScriptText = ThisWorkbook.Worksheets("QLoader").Range("z_BackupScriptText").Value
'replace the text "placeholder" with this workbook's actual full path/name:
strScriptText = Replace(strScriptText, "placeholder", ThisWorkbook.FullName)
'fire up FSO:
Set fso = CreateObject("scripting.filesystemobject")
'determine the new VBS file's path
strScriptPath = Environ("AppData") & "\Backup_" & Format(Now, "yymmddhhmmss") & ".vbs"
'create our textstream object:
Set ts = fso.createtextfile(strScriptPath)
'write our script into it
ts.write strScriptText
'save and close it
ts.Close 'RIGHT HERE THE FILE DISAPPEARS FROM THE FOLDER ***********
'GO:
Shell "wscript " & strScriptPath, vbNormalFocus
End Sub
It does look like an antivirus thing...
If the issue is just the vbs extension though, you can use something like this:
Sub tester()
Dim ts As Object, fso As Object, strScriptText As String, strScriptPath As String
Set fso = CreateObject("scripting.filesystemobject")
strScriptPath = Environ("AppData") & "\Backup_" & Format(Now, "yymmddhhmmss") & ".txt"
Set ts = fso.createtextfile(strScriptPath)
ts.write "Msgbox ""Hello"""
ts.Close
'need to specify the script engine to use
Shell "wscript.exe /E:vbscript """ & strScriptPath & """ ", vbNormalFocus
End Sub

Need to check whether a project is checkedout or not

How to check whether we can check out a project or not.
projects are stored in sharepoint.
Always this code is printing unable to checkout
Sub macro()
Dim a As Project
Shell "C:\Program Files (x86)\Microsoft Office\Office15\Winproj.exe /s https://inside.com/PWA/QWER/Project.aspx", vbNormalFocus
Sleep 3000
FileOpenEx Name:="<>\" & "ProjectNAME", ReadOnly:=True, DoNotLoadFromEnterprise:=False
Set a = Projects.Item(1)
a.Activate
If (Projects.CanCheckOut(ActiveProject.Name)) Then
Debug.Print "Can check out the project"
Else
Debug.Print "Cannot checkout the project"
End If
End Sub
It will be very helpful
If you need to run the code inside MS-Project VBA, use the Code below:
Sub CheckOutProject(docCheckOut As String)
' Determine whether project can be checked out
If Projects.CanCheckOut(docCheckOut) = True Then
Debug.Print "Can check out the project"
' if you want, you can check it out
Projects.CheckOut docCheckOut
Else
Debug.Print "Cannot checkout the project"
End If
End Sub
Use the Test code below to test it out:
Sub Test()
Dim FullPath As String
' Full Path equals the full SharePoint Path & File name (including extension)
FullPath = "http://share.Comapny.com/sites/Test123/Project%20Documentsnew/Project%20Files/Project_1.mpp"
CheckOutProject FullPath ' call the Sub
End Sub

Running RScript in VBA

I'm trying to create a VBA script that run RStudio script.
i tried using the Shell() command or the Run oShell at VBA, but the best thing i mange on doing is open the RStudio script, not making it to run automatically.
By the way, the RStudio script create a csv file which i will use.
this is the VBA script i use right now:
Sub RunRStudio()
Dim path As String
path = ThisWorkbook.path & "\Test.R"
Set oShell = CreateObject("WScript.Shell")
oShell.Run "RStudio " & path
shell ("RStudio " & path)
End Sub
How can i run this RStudio script automatically from VBA?
Thanks.
I had the same problem. Thank you, because your code helped me to find the solution.
This code, with a little change, it works to open the window of Rstudio, but it doesn't work to run the script. You need to use "RScript", not "RStudio".
You can try this to run your code:
Sub RunRTest()
Dim path As String
path = """C:\Program Files\R\R-3.5.2\bin\RScript.exe""
""C:\Folder\YourScriptName.R"""
Set oShell = CreateObject("WScript.Shell")
oShell.Run path
End Sub
Or:
path_file = ThisWorkbook.path & "\YourScriptName.R"
path = """C:\Program Files\R\R-3.5.2\bin\RScript.exe"" " & path_file & ""
if you have the path of your file in other var.
I hope to help someone!

How to open a file and make it default in VBA

I am trying to make "python.py" to open in notepad each time...I have got the code that does that but how to make it as default..so next time if the user wants to open the python file in notepad, it should open in notepad all the time using VBA and only after the code is excecuted...please find the code below for opening a python file in notepad..
Shell "notepad C:\Users\stackoverflow\Desktop\python.py", vbNormalFocus
You could try a VBA macro attached to a button that does this. But I am unsure if this is what you are asking for.
Sub LetterChecklist()
Dim strPath As String
Dim strProgram As String
strPath = "\\aw\data\Letters\2099_Correspondence\Incoming Letters\.pdf"
strProgram = "C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Acrobat.exe"
Call Shell("""" & strProgram & """ """ & strPath & """", vbNormalFocus)
End Sub

How can I run a MiniTab Executable file (.mtb) in vba

Preferably I would like to incorporate it into a user form as a command button.
The code I've tried so far:
Private Sub graphButton_Click()
Dim mtbPath As String: mtbPath = "S:\MetLab (Protected)\MetLab Operations\Lab
Reports\Forgings"
Call Shell(Environ$("COMSPEC") & " /s " & mtbPath & "\Updater.mtb", vbNormalFocus)
End Sub
Where Updater.mtb is the actual file I would like to execute. This seems to only open Command prompt- which is not what I'm looking for
Copy and paste into a VBA Module:
Sub Run_Minitab_Macro()
Set MtbApp = CreateObject("Mtb.Application")
With MtbApp.ActiveProject
'This lets you open an excel file from your desktop:
.ExecuteCommand "WOpen 'C:\Users\you\Desktop\TEMP1.xlsx'; FType; Excel."
'Save the minitab project to my documents:
.ExecuteCommand "Save 'C:\Users\you\My Documents\Test.MPJ'; Project; Replace."
'On Error Resume Next
.ExecuteCommand "%MacroFile" 'This is a .MAC macro file stored in the my
'documents folder saved to here,
'any command-line command can go here though
'On Error GoTo 0
.ExecuteCommand "Save."
End With
End Sub