VBA - Hide Chrome window - vba

I have a vba routine in excel that downloads something with chrome. But I'd like to stop the chrome window from opening in front of me and instead have it run in the background, keeping the focus on my excel file.
The code looks like this :
Sub dl()
Dim WebUrl As String
WebUrl = [J1]
Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl)
End Sub
I've heard of shell functions (with vbMinimizedNoFocus) which I think could do the trick but I can't find anywhere the correct syntax and an example of how to apply it...
Can anyone help me ?
Thank you very much in advance !

The documentation for Shell defines vbMinimizedNoFocus as 6 and gives the syntax:
Shell "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl, vbMinimizedNoFocus ' Should be defined in the global namespace

Related

Shell.Execute in VBA is not doing anything

I am trying to use powershell to extract some files from a zip folder located on a drive where I only have read access to my desktop in a temp folder where I can do whatever I like to them.
Using the code below I get no errors but the powershell code just does nothing.
Am I missing something?
Sub unzip_test()
Dim myshell As Shell32.Shell
Set myshell = New Shell32.Shell
Dim args As String
args = "Expand-Archive -LiteralPath " & "'C:\Users\user1\Desktop\TEMP\examplezip.zip'" & " -destinationpath " & "'C:\Users\user1\Desktop\TEMP\tester'"
'Debug.Print (args)
myshell.ShellExecute "powershell", vargs:=args
End Sub
The debug.print prints Expand-Archive -LiteralPath 'C:\Users\user1\Desktop\TEMP\examplezip.zip' -destinationpath 'C:\Users\st11524\Desktop\TEMP\tester'
Also, I have "Microsoft Shell Controls and Automation" checked in my references.
It should work. I think the problem is somewhere in the Expand-Archive call, but you can't see the answer.
The quick fix is to add -NoExit to your call, like this
args = "-NoExit Expand-Archive -LiteralPath " ...
This will allow you to read the error before it closes, but you can't read it from your code. If you need that, have a look here
After testing this on another computer I've come to the conclusion that this is a permissions thing. My personal computer runs the script just fine where my work computer won't. I don't see anything wrong with the code listed above so I will consider this issue resolved.

Open Explorer Maximized with Focus and Highlight File from Access VBA

I am trying to open Windows Explorer and highlight a file.
I am using:
Private Sub cmdLoadStl_Click()
Dim shellCmd As String
shellCmd = "explorer.exe /select, """ & Me.txtPath & """,vbMaximizedFocus"
Debug.Print shellCmd
Shell (shellCmd)
End Sub
This works as expected except the window opens minimized.
I have tried this on several Win7 machines running Access 2016 and two Win10 machines, one running Access 2013, the other running Access 2016.
Is there any way to get the window to open maximized?
Edit: More information - If I paste the command generated by debug.print in my code to a command prompt and run it, the explorer window opens maximized, with the file highlighted, as it should.
The command generated from vba looks like this.
explorer.exe /select, "C:\Users\user\Desktop\filename.txt",vbMaximized
Thanks.
Try like this:
shellCmd = "explorer.exe /select, """ & """,vbMaximizedFocus
shell shellcmd,vbMaximizedFocus
The second (optional) algorithm of shell is the focus. vbMaximizedFocus opens it maximized:

Equivalent to Run Method(VBA) in OpenOffice Calc

I have been trying to convert a VBA for Excel code to OpenOffice Calc (basic) and now Im having trouble accessing an external program (and it´s database) to generate an output which I will use later in my code. In other words, I give an input, call the program and later I want an output file.
In VBA:
Prog = Worksheets("Settings").Cells(2, 2) & IPRODB & " -i " & DateiAll_in
Wait = True
Set sh = CreateObject("WScript.Shell")
RetVal = sh.Run(Prog, 0, Wait)
Worksheets("Settings").Cells(2, 2) is the program's path and IPRODB is the database's path, "-i" seems to be a command to the program and DateiAll_in is the Input file (.csv) I created before in the code.
The OpenOffice basic offers the function Shell(Pathname, Windowstyle, Param, bSync) to open external programs but It would not be the same since in the "run method" in VBA I am running a macro containing the program, its´s Database and Input File. (expression.Run(MacroName, varg1,varg2, (...))
Is there any alternative to the Shell function or can I use it in the same way as I used the "run method" in VBA?
From the OpenOffice.org forums (didn't test it myself):
Dim oSvc as object
oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
Rem Launch notepad
oSvc.execute(ConvertToUrl("C:\windows\notepad.exe"), "", 0)
I used the Shell function. In the first argument of the function I added all the equivalents of "Worksheets("Settings").Cells(2, 2) & IPRODB & " -i " & DateiAll_in". I substituted Worksheets("Settings").Cells(2, 2) for the direct path of the program only because it was easier. So it worked with this code below:
Shell("C:\Program Files\*******\*****\*****.exe & IPRODB & "-i" & DateiAll_in",1,,True)

Pass Variable from Excel Macro to .vbs file

I have an excel button with a macro. The code simply stores the path of the workbook in a vaiable names 'MyCompletePath' and then runs a .vbs file. The code is:
MyCompletePath = ActiveWorkbook.FullName
'Run VBS file
Shell "wscript C:\Users\name\Desktop\vb.vbs", vbNormalFocus
I want to pass the variable 'MyCompletePath' from the excel file to the file which is executed using the last line.
I made some searches but didn't fully understand what their solutions do. Maybe someone can tell me how to do it.
UPDATE/EDIT: I'm now having a problem with the filepath after Shell "wscript. It has spaces in the folder name. How can I get it to work with spaces in the name?
Thank you.
You can include command line arguments after the path to your VBS file. For example:
Shell "wscript C:\Users\name\Desktop\vb.vbs BlahBlahBlah", vbNormalFocus
Then inside your VBS script, you can access them using the WScript.Arguments collection. For example:
MsgBox WScript.Arguments(0)
would pop up a message box displaying "BlahBlahBlah".
For a file path, as you indicated, which might include spaces and thus would be treated as multiple arguments to the script, I would include the argument in quotes, like this:
Shell "wscript C:\Users\name\Desktop\vb.vbs ""this has multiple words""", vbNormalFocus
based on passing argument from vba to vbs
When I write that within the vba:
Sub Macro1()
MyCompletePath = "toto"
Shell "wscript D:\\vb.vbs " & MyCompletePath
End Sub
and this in vb.vbs
MsgBox("Hello " & WScript.Arguments(0))
I do get "Hello toto"

Shell *.vbs from within VBA

Hi can anyone tell me how to execute my .vbs from within VBA cause the following code dose not work.
RetBat4 = Shell("c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs", 1)
VBA debug says that the sytax is wrong????
thanks
Try
Shell("cscript ""c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs""",1)
If your VBS needs complete command shell environment, use this:
Shell("cmd /c cscript ""c:\VTS\QUEEN ANNES REVENGE\SYSTEM\VBS\UNDO_2.vbs""",1)
and if your program should wait until the VBS ends, read this post:
VBA Shell and Wait with Exit Code
try This:
Set ws=CreateObject("Wscript.shell")
set ws.exec("ping " & ip & " -n 20")
set ws=nothing