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)
Related
I'm using VBA (MSAccess) to print the contents of a folder using ShellExecute. It works perfectly, except for the small detail of printing in color.
Private Declare PtrSafe Function ShellExecute _
Lib "shell32.dll" _
Alias "ShellExecuteA" ( _
ByVal hwnd As LongPtr, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) _
As LongPtr
I'm calling the function like this:
ShellExecute hwnd, "print", ToPath & "\" & file, 0&, 0&, 0&
As I said, everything works exactly as I want it to, except that the print jobs are only printing in grayscale. I really need this to print in color. Does anyone have any idea of why this is only printing in grayscale?
i write this code to change default printer in windows and work fine but in reload the INI file have a error
this is a code :
Private Sub SetDefaultPrinter(ByVal PrinterName As String, ByVal DriverName As String, ByVal PrinterPort As String)
Dim DeviceLine As String
'rebuild a valid device line string
DeviceLine = PrinterName & "," & DriverName & "," & PrinterPort
'Store the new printer information in the
'[WINDOWS] section of the WIN.INI file for
'the DEVICE= item
Call WriteProfileString("windows", "Device", DeviceLine)
'Cause all applications to reload the INI file
Call SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
End Sub
Private Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lparam As String) As Long
Private Const HWND_BROADCAST As Long = &HFFFF&
Private Const WM_WININICHANGE As Long = &H1A
and this is a error :
A call to PInvoke function 'Test!Test.Form2::SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Does anyone have an idea to solve this problem?
Thankful
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lparam As String) As Long
Your data types are wrong. The hwnd parameter is pointer sized, wMsg is a 32 bit value, and wParam, lParam and the return value are pointer sized. Note that Long is a 64 bit type.
Instead it should be
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, _
ByVal lparam As String) As IntPtr
Note that I recommend that, in the modern day, you use pinvoke declarations rather than Declare. This offer much more flexibility.
I have a sub that creates a .txt file and I want to print it in the default printer. How can I achieve this in VBA?
I think I need to call the ShellExecute API Function, but I did not find the correct sintax for that.
I would appreciate any help!
I found a code that do the trick:
Option Explicit
Declare Function apiShellExecute 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
Public Sub PrintFile(ByVal strPathAndFilename As String)
Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
End Sub
Sub Test()
PrintFile ("C:\Test.pdf")
End Sub
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.
I used to printout PDF-files from MS Access 2010 32-bit on Windows 7 32 bit with this code.
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) As Long
Function PrintAttachement()
ShellExecute 0, "print", "\\s1016d\attachments\40297827.pdf", "", ""
End Function
Now, we changed to Windows 7 64 bit, but still Office 32 bit and ALL Office applications crashes when running this function.
Strange, because if I use "open" iso. "print" it works as expected!
Please help, as I am lost how to correct my function to run again.
All I want is to printout a PDF-File from Access without opening the file.
As there are many files in a row, I cannot open any PDF-app to printout the file.
Thanks
Michael
Edit: After Long searches I found the solution!
You have to declare the function like in 64bit application, but to make shure to run it also on machines with 32bit declare both.
#If VBA7 Then
Private Declare PtrSafe 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
#Else
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
#End If
So in order to show this as answered, and also vouch that this does solve the same situation for me. If you are referencing a windows DLL in-line, you need to ensure that the correct DLL is being used based on the environment the access DB is being used in (32bit vs 64bit) you can do this dynamically as described above (and repeated here)
#If VBA7 Then
Private Declare PtrSafe Function apiShellExecute 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
#Else
Private Declare Function apiShellExecute 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
#End If