ShellExecute brings Office programs to crash - vba

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

Related

ShellExecute printing in grayscale only

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?

Need correct syntax to declare and call HTMLHelp API in 64-bit Office and VBA7

This VBA code displays context-sensitive help in a CHM in 32-bit Microsoft Office and VBA6:
Private Declare Function HtmlHelp Lib "HHCtrl.ocx" Alias "HtmlHelpA" _
(ByVal hWndCaller As Long, _
ByVal pszFile As String, _
ByVal uCommand As Long, _
dwData As Any) As Long
Const HH_DISPLAY_TOPIC As Long = 0
HtmlHelp hwnd, sPathToCHM, HH_DISPLAY_TOPIC, ByVal "topic_name.htm"
This code displays nothing and no error messages in 64-bit Microsoft Office with VBA7:
Declare PtrSafe Function HtmlHelp Lib "hhctrl.ocx" Alias "HtmlHelpA" _
(ByVal hwndCaller As LongPtr, _
ByVal pszFile As String, _
ByVal uCommand As Long, _
ByVal dwData As String) As Long
HtmlHelp(hwndCalling, strHelpTopicFile, HH_DISPLAY_TOPIC, strHelpTopic)

Can some one explain what this is actually doing?

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

Converting Excel VBA 32bit code to 64bit

A friend of mine made this Excel VBA code for me a while ago. It's 32bit, but now I need it converted for 64bit.
Can anyone please help me to do so?
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32.dll" _
Alias "GetWindowLongA" ( _
ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" ( _
ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As Long) As Long
Private Declare Function PostMessage Lib "user32.dll" _
Alias "PostMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" ( _
ByVal xPoint As Long, _
ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32.dll" ( _
ByRef lpPoint As POINTAPI) As Long
You need to use "PtrSafe" when declaring function :
Private Declare PtrSafe Function
Also in 64bit Excel, use LongLong instead of Long
and LongPtr for callback functions
but for ptrSafe compiler itself will warn you if this is missing

RegQueryValueEx calls succeeds but lpcbData is 0

I am writing a vba application using macros. I am trying to read a dword value for IMAP Port using RegQueryValueEx. The call succeeds but I lpcbData points to 0. Here is my declaration
Private Declare PtrSafe Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExW" (ByVal hKey As LongPtr, ByVal lpValueName As LongPtr, ByVal lpReserved As LongPtr, lpType As LongPtr, ByVal lpData As LongPtr, lpcbData As LongPtr) As LongPtr
If RegQueryValueExStr(hKey, StrPtr(queryFieldName), 0, dwType, port, dwBufSize) = ERROR_SUCCESS Then
Dim wport As String
'wport = StrConv(port, vbUnicode)
EnumerateAccounts = EnumerateAccounts & fieldvalue & ": " & wport & "\n\r"
End If
The code above is not very well written, im just trying to hack some code together to read the port value.
I have the same code working in a C++ application so it is not the issue of 32/64bit windows.
Ok so i found out why it was returning a zero. First of all the dword values are/were stored using ASCII not unicode. In case someone else runs into the same problem, the correct declaration is as follows:
Private Declare PtrSafe Function RegQueryValueExDword Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal lpReserved As LongPtr, lpType As LongPtr, lpData As Any, lpcbData As LongPtr) As LongPtr