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?
Related
I have an Access 2019 database and want to include a button to open the ODBC administrator. The event procedure on click is written as
Private Sub Command210_Click()
Dim RetVal
RetVal = Shell("odbcad32.exe", 1)
End Sub
however this does not work, if I replace odbcad32.exe with notepad.exe it will open notepad on clicking but odbcad32 does not work - any ideas why?
Based on one of my previous answers you could call the ODBC administrator like that
Option Compare Database
Option Explicit
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
Private Declare PtrSafe Function Wow64EnableWow64FsRedirection _
Lib "kernel32.dll" (ByVal Enable As Boolean) As Boolean
Private Sub RunODBC_on64Bit()
Const SW_SHOWNORMAL = 1
On Error Resume Next
Wow64EnableWow64FsRedirection False
ShellExecute 0, "open", "odbcad32.exe", "", "C:\windows\system32\odbcad32.exe", SW_SHOWNORMAL
Wow64EnableWow64FsRedirection True
End Sub
I got it in the end - I replaced line
RetVal = Shell("odbcad32.exe", 1)
with
RetVal = Shell("Explorer.exe ""C:\Windows\SysWOW64\odbcad32.exe""", 1)
and that sorted it.
I have been searching for a way to close windows explorer using vba, and i have found something that works. However i actually have no idea what it is actually doing, or what any of it means. Could someone please explain what is happening below?
Private Const CLOSE_WIN = &H10
Dim Hwnd As Long
Private Declare Function apiFindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassname As String, _
ByVal lpWindowName As String) _
As Long
Private Declare Function apiPostMessage _
Lib "user32" Alias "PostMessageA" _
(ByVal Hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) _
As Long
Hwnd = apiFindWindow("CabinetWClass", vbNullString)
Dim retval As Long
If (Hwnd) Then
retval = apiPostMessage(Hwnd, CLOSE_WIN, 0, ByVal 0&)
End If
Thank You
I'm using ShellExecute on VBA to force the computer to Sleep (not Hibernate), for that to be done I need to disable hibernation. I've entered rundll32.exe but I'm getting an error in the "powercfg.cpl -hibernate off".
I've also tried:
powercfg.exe /hibernate off
and
powercfg -hibernate off
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpszOp As String, _
ByVal lpszFile As String, _
ByVal lpszParams As String, _
ByVal lpszDir As String, _
ByVal FsShowCmd As Long) As Long
Const SW_SHOWNORMAL As Long = 1
Sub DoSleep()
ShellExecute 0, "runas", "C:\WINDOWS\System32\rundll32.exe", "powercfg.cpl -h off", "C:\", SW_SHOWNORMAL
ShellExecute 0, "runas", "C:\WINDOWS\System32\rundll32.exe", "powrprof.dll,SetSuspendState 0,1,0", "C:\", SW_SHOWNORMAL
ShellExecute 0, "runas", "C:\WINDOWS\System32\rundll32.exe", "powercfg.cpl -hibernate on", "C:\", SW_SHOWNORMAL
End Sub
After quite a bit of trial and error I found the solution. This will put your computer in sleep mode through VBA:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpszOp As String, _
ByVal lpszFile As String, _
ByVal lpszParams As String, _
ByVal lpszDir As String, _
ByVal FsShowCmd As Long) As Long
Const SW_SHOWNORMAL As Long = 1
Sub DoSleep()
ShellExecute 0, "runas", "powercfg.exe", "/hibernate off", "C:\", SW_SHOWNORMAL
Shell "C:\WINDOWS\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0"
ShellExecute 0, "runas", "powercfg.exe", "/hibernate on", "C:\", SW_SHOWNORMAL
End Sub
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 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