VBA: Running "Elevated" Command (Shell vs. ShellExecute) - vba

In my VBA procedure, I need to run the app "Skitch" and use it to open a JPEG file. This is the command I've been using:
ReturnValue = Shell("C:\Program Files (x86)\Evernote\Skitch\Skitch.exe " & """" & aPic & """", 1)
...where "aPic" is the path and filename.
After some experimenting, I think I need to run the command as if it were in an Elevated Command window (in other words, run it "as Administrator"). Is it possible to run Shell elevated?
If that's not possible: If I understand correctly, using ShellExecute instead of Shell will automatically elevate the command. But I'm much less familiar with it. Can someone show me how to run my command using ShellExecute? (BTW, I know that ShellExecute is good for running commands associated with the file type, but on this user's computer *.jpg will likely not be associated with Skitch.)
Thanks.

Try this:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1
Public Sub test()
ShellExecute 0, "runas", "C:\Program Files (x86)\Evernote\Skitch\Skitch.exe", aPic, vbNullString, SW_SHOWNORMAL
End Sub
I don't have skitch so can't try this, but it should work.
For more information about ShellExecute, click here to have a look on MSDN.

Related

Printing .pdf in vb6 without preview Adobe Reader

I'm trying to print a .pdf file using a shell. The printout comes from the default printer, but I would like to do it in the mode without opening Adobe Reader, I would like it to be invisible to the user. I tried to use the "SW_HIDE" variable but at this point the printout does not come out at all. I will be grateful for your help.
My code:
Const SW_SHOWNORMAL = 1
Const SW_HIDE = 0
Option Explicit
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Private Sub Command1_Click()
Dim PrintIt As Long
PrintIt = ShellExecute(Me.hwnd, "print", "C:\Test.pdf", "", "", SW_SHOWNORMAL)
'Pdf1.src = "C:\test.pdf"
'Pdf1.printAll
End Sub```
If I understand the question correctly, you want to use a headless PDF printer so the user does not know which application is doing the printing.
Looks like Adobe does not allow this and doing this could violate their license:
https://community.adobe.com/t5/acrobat-sdk/how-to-print-a-pdf-programmatically-without-the-adobe-reader-window/m-p/4343967
Most important – have you read the Acrobat Reader EULA to ensure that
your use is compliant? Many batch and headless operations are not
compliant with the EULA.
If you do not have to use Adobe Reader, there are a number of other PDF printer applications that allow silent installation and headless printing.
http://www.columbia.edu/~em36/pdftoprinter.html
https://www.biopdf.com/
https://www.coolutils.com/TotalPDFPrinter
https://www.bullzip.com/products/pdf/info.php

What is the VB 6 equivalent of Process.Start?

I am stuck on a really stuck with this one line. In vb.net this is easy, but how do I do this in vb6? Tried to search from google for few hours and got nothing. Feels almost embrassing.
Here's the code. (.NET)
Process.Start("runme.exe", " -parameter1 " & "-parameter2 " & "-parameter3")
I want to run EXE, from the same directory as where the program is, with parameters. I am sure there is some very simple solution, but I can't get it to work. Any help would be appreciated.
You can use Shell and ShellExecute
Shell "c:\runme.exe", vbNormalFocus
http://msdn.microsoft.com/en-us/library/aa242087(v=vs.60).aspx
Just call Shell, and the parameters should be passed also with the string of the .exe name, like this:
Call Shell("""runme.exe"" ""-parameter1 "" ""-parameter2""", vbNormalFocus)
PS: The quotes make the difference, dont ignore it :)
You can use ShellExecute for this:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
ShellExecute 0, "open", App.Path & "\runme.exe", "-parameter1 -parameter2 -parameter3", vbNullString, vbNormalFocus
I have found that using Shell causes a delay in the calling program waiting for the return value, whereas ShellExecute does not.

Using Shell Open specific ACCESS form

I'm trying to open a specific Access form using Shell in vb6, but after reading lots of documentation, I am unable to find out why Access can not find my database.
dim ss as string
ss = MSAccFolder & "\MSAccess.exe" & " " & app.path & "\database\db.mdb /cmd " & ""formname""
Shell sss, vbNormalFocus
The interesting thing that I shoud tell you is that after a while Access opens and it says that the database with this path does not found
MYAPPPATH/MYFORMNAME.mdb
What's wrong?
app.path may contain spaces. Surround app.path & "\database\db.mdb" with quotes. I apologise but I have forgotten the VB syntax to do it.
Finally i found the solution.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'This will open Access with the specified name
Dim ss As String
ss = App.path & "\database\db.mdb"
ShellExecute Me.hwnd, vbNullString, ss, vbNullString, "C:\", SW_SHOWNORMAL

Isn't VBA Shell supposed to be asynchronous?

When I call a rebol script with
call shell("rebol.exe myscript.r")
The shell doesn't return until the script is finished. Is there a way to force VBA Shell to be asynchronous ?
If you just want to use Shell function, try another option with using API :
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
This is the full code of ShellEx function :
http://www.vbaccelerator.com/codelib/shell/shellex.htm
You'll want to pass in a second argument to make the program run in the background. Here's the documentation.
call shell("rebol.exe myscript.r", vbMinimizedNoFocus)

How to launch a URL when an email arrives

I would like to launch a URL when an email arrives in Outlook. I setup a rule and have it trigger a script function. It looks like I want to call ShellExecute to launch the URL in a browser, but when I hit this line:
ShellExecute(0&, "open", URL, vbNullString, vbNullString, _
vbNormalFocus)
The method is not defined. Any ideas?
ShellExecute is a function in a windows dll.
You need to add a declaration for it like this in a VBA module:
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
The difference between your Shell solution and ShellExecute is that ShellExecute will use the default system handler for URLs to open the link. This doesn't have to be IE. Your solution will always open it in IE. Yours is the equivalent of putting iexplore.exe into the run box in windows. ShellExecute is the equivalent of just putting the url in the run box in windows.
You can also use Followhyperlink from VBA to open URLs in the default browser. It can also be used to open documents with the registered application, to send emails and to browse folders.
Alternatively, use Shell, like this:
Sub LaunchURL(Item As Outlook.MailItem)
Shell ("C:\Program Files\Internet Explorer\IEXPLORE.EXE" & " " & Item.Body)
End Sub
You can create batch file where you write this:
start http://someurl.com/?a=1^&b=2
And you configure Outlook rule to launch this batch file. Notice ^ sign before &. This is escape sequence for & in batch files. Also notice that you need to have default browser set in your Windows OS, almost 100% probability that you have it.
Shell ("CMD /C start http://www.spamcop.net"), vbNormalFocus