1

Continuing this topic IFileOpenDialog and IFileSaveDialog from VBA, I ask some help.

I started from this point: The Open Dialog Box

  1. I called CoInitializeEx() to initialize the COM library as described above.

  2. I called CoCreateInstance() to create the Common Item Dialog object and get a pointer to the object's IFileOpenDialog interface.

  3. Next I need to "Call the object's Show method", which shows the dialog box to the user.

Can anyone advise me how it can to be made?

Declare Function CoInitializeEx Lib "ole32" (ByVal pRef As Long, ByVal dwCoInit As Long) As Long
Declare Function CoUninitialize Lib "ole32" () As Long
Declare Function CoCreateInstance Lib "ole32" (ByVal CLSID As String, _
                        ByVal pUnkOuter As Long, _
                        ByVal dwClsContext As Long, _
                        ByVal riid As String, _
                        ByVal ppv As Long) As Long
            
Const CLSID_FileOpenDialog As String = "D57C7288-D4AD-4768-BE02-9D969532D960"
Const CLSCTX_INPROC_SERVER = &H1
            
Sub iniComLibr()
    Dim pRef As Long, hComLibr As Long, hObjInt As Long, dwCoInit As Long, oFileOpenDialog As Object
    hComLibr = CoInitializeEx(pRef, dwCoInit)     'activate a COM_library and get the handle

    Dim pUnkOuter As Long, dwClsContext As Long, riid As String, ppv As Long
    ppv = VarPtr(riid)
    hObjInt = CoCreateInstance(CLSID_FileOpenDialog, 0, CLSCTX_INPROC_SERVER, riid, ppv)  'get the pointer to the object's IFileOpenDialog interface

    ' here I need call Show method, but don't know how....

    hComLibr = CoUninitialize() 'This is mandatory balance after CoInitializeEx 
                                'call was made.
End Sub
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Levi.S
  • 13
  • 4
  • Surely a mistake to initialize COM in a thread you don't own. Not checking for errors is also not going to end well. Do you know what it is to perform error checking in this context? – David Heffernan Jun 28 '20 at 18:53
  • 1
    Why don't you just develop a COM object (for example in VB.NET or C#) easily usable from VBA, that either uses .NET's dialog object, or use interop for ex: https://stackoverflow.com/questions/4136477/trying-to-open-a-file-dialog-using-the-new-ifiledialog-and-ifileopendialog-inter – Simon Mourier Jun 28 '20 at 19:50
  • Very thanks for replies! "Do you know what it is to perform error checking in this context?" Not of course. Nothing like I seen in Microsoft TechDocs. Give me a link where I can study it, please. "Why don't you just develop a COM object (for example in VB.NET or C#) easily usable from VBA..." Simply I want explore the borders of the possibility of the VBA. How complex tasks I can resolve by VBA. – Levi.S Jun 28 '20 at 20:48
  • The documentation for the functions that you call describe how to interrogate the return value to check for errors. – David Heffernan Jun 28 '20 at 21:32
  • [CoUninitialize](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize) doesn't return a value. – IInspectable Jun 28 '20 at 21:40
  • If you really like to suffer, check this: https://codekabinett.com/rdumps.php?Lang=2&targetDoc=raw-com-api-vba-hide-taskbar-button-window-itaskbarlist . You can build a .tlb (from an .idl) yo use some interfaces (so you don't have to use CoCreateInstance, etc.) but IFileDialog and related interfaces have types (not automation compatible) that will prevent you from using all its methods anyway. This is the reason why back then Microsoft used to add "Automation" interfaces to most of its APIs like the "Shell Objects for Scripting and Microsoft Visual Basic". But they don't anymore. – Simon Mourier Jun 29 '20 at 05:49
  • @dav Calling `CoInitializeEx` doesn't magically fix `Declare Function CoUninitialize Lib "ole32" () As Long` or `hComLibr = CoUninitialize()`. Declaring a function that doesn't return a value to have a non-void return value is a bug. – IInspectable Jun 29 '20 at 06:29
  • @IInspectable OK, I see what you are getting at. – David Heffernan Jun 29 '20 at 06:31
  • "Declaring a function that doesn't return a value to have a non-void return value is a bug." It looks like a true, but I tried such "Declare Function CoUninitialize Lib "ole32" ()" without "As Long" and got "Run-time error 49: Bad DLL calling convention". On the next step I used "CoUninitialize" instead of "hComLibr = CoUninitialize()". I got the same error "Run-time error 49: Bad DLL calling convention". For my quite regret... May be You know other source of the knowledge, besides of Microsoft TechDocs? Point me, please, there. – Levi.S Jun 29 '20 at 17:15
  • @Simon Mourier "An excursion to the raw COM API with VBA." is very interesting. May be just what I am finding now. Thanks a lot! – Levi.S Jun 29 '20 at 18:16

0 Answers0