The ShellExecute function
The ShellExecute function opens or prints a specified file in different ways, manipulating window(s) as it goes. The file can be an executable file or a document file. It offers some error indications too. So perhaps it is one of those things where it is the more fundamental and more useful version of something available in VB / VBA, perhaps something similar to like a worksheet function used from Application rather than Application.WorksheetFunction
In this case the difference between the VBA shell function (execute) and the api shell execute are a bit more significant although the difference in error handling follows the similar trend, in that the VBA shell function would error when the api shell execute might give us some useful information. This perhaps ties up with the idea of us being more protected in the VB VBA normal environment, as some wiring is perhaps in place to take action on an error, rafter than telling us about it. The final terminal error that we then might get is then done when all other important things were done. So as ever, we should be more careful how we tread when using api things. I will keep, things simple for now and just open a text file
Here we have perhaps another example of the strange varying way that arguments are handled with these api things, as HdlTextFile = FindWndNumber(………. line which we have a lot, is now used in a strange different way. It can’t be getting a handle this time, but never the less, the documentation suggests it somehow in conjunction with the first argument of ShellExecute( organises which parent window is used for displaying a user interface or error messages?? … maybe another case of … I can't think of any logical way to organise all this back end working at the upper user interface level, so just make an argument work however it can, don' tell anyone too much about it with any decent documentation, let a few hacks think they know, even some here who think they are creating or did create windows, and let them tell people about it and then good riddance to it all, I am off with my wife to enjoy the sun in a distant land, at least until she goes mental, as they all do eventually …. Bill Gates, early 80’s, …. I told him , or maybe it was one of his mates trying to sell windows to me, … I was not interested in those pretty coloured square boxes things, they all look like some sort of small child’s video game xmas presentCode:' https://www.excelfox.com/forum/showthread.php/2989-Rough-Notes-and-posts-to-be-referenced-from-elsewhere-on-VBA-Windows-API?p=24915&viewfull=1#post24915 Private Declare Function FindWndNumber Lib "user32.dll" Alias "FindWindowA" (Optional ByVal lpClassName As String, Optional ByVal lpWindowName As String) As Long 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_NORMAL As Long = 1 Sub TextAPI() ' Dim HdlTextFile As Long ' Let HdlTextFile = FindWndNumber(lpClassName:="Notepad", lpWindowName:="Neues Textdokument.txt - Editor"): Debug.Print HdlTextFile & " " & Hex(HdlTextFile) ' Let HdlTextFile = FindWndNumber(lpClassName:="Notepad", lpWindowName:="TextFile.txt - Editor"): Debug.Print HdlTextFile & " " & Hex(HdlTextFile) ' 0 0 Dim FuncReturn As Variant ' Let FuncReturn = ShellExecute(HdlTextFile, "open", "C:\Users\Elston\Desktop\Neues Textdokument.txt", 0, 0, SW_NORMAL): Debug.Print FuncReturn ' ' Let FuncReturn = ShellExecute(HdlTextFile, "open", "C:\Users\Elston\Desktop\TextFile.txt", 0, 0, SW_NORMAL): Debug.Print FuncReturn ' Let FuncReturn = ShellExecute(HdlTextFile, "open", ThisWorkbook.Path & "\TextFile.txt", 0, 0, SW_NORMAL): Debug.Print FuncReturn ' 42 End Sub




Reply With Quote
Bookmarks