VBA - Get Win Explorer file previews (1 Viewer)

ironfelix717

Registered User.
Local time
Today, 08:00
Joined
Sep 20, 2019
Messages
193
Greetings all,

This topic is a long shot and well beyond my skill at the current time-- but a long shot that's worth shooting, as I know how deep the knowledge and experience runs on this forum.

Preface:
I have a custom file manager form that has a "preview" image control... pretty standard feature. The problem with this is that it displays only image type files. The files i'm looking to preview in this form range from common formats (Image, Office, PDF, Text, etc) to more un-popular formats (which Windows recognizes in it's Explorer Preview Pane). I once read that PDFs can be displayed as a preview with a web browser control or something, but that solves only one format in the long list (even if that is true).

The ability to display file previews is a feature that is very valuable to this application and is something I've been trying to solve for a long time.

Work Arounds:
After scouring the web extensively for a VB solution on generating preview images, I found nothing. And usually when such scenario occurs, I simply outsource the task to Python where the libraries already exist, build a script, and call from VBA. Done deal.

So, I went that route. And ultimately ended up with a lousy solution. Writing code to handle each format to generate a preview on a case by case basis is tedious and at the end of the day, the result is a slow script that works for only a handful of supported formats. Not to mention, many of the libraries out there come at a cost. With the free versions, the generated preview has a nice watermark on the resultant image.... I even hired a freelancer developer to build me a script that was more improved and supported more formats, but it had some bugs and currently occupies my PC's recycle bin. This is still an option I am re-considering if I can't solve it myself.

Explorer Previews:
The most effective strategy that has the greatest functionality here is to ""simply"" grab the Window's Explorer preview pane image. The preview pane has the ability to display a preview for more formats than there are preview generating libraries for (that includes every language). I believe this is because the writers of the parent application of the file format in question ("non-native" formats, ex: 3D model formats) build their own preview generators internal to their application which is installed in Windows). Explorer just does the handshake between the Preview Pane and the underlying "parent" application to get the preview. (and probably searches cache first). I am struggling with my words, but you get the point... i think... Whether that theory is true or not, the fact remains that whatever Windows is actually doing under the hood to serve a preview image, no code library out there is going to match it's ability. They simply don't exist. It is dynamic and far better than writing a script that can generate previews for maybe only 10 formats at best... Therefore, this makes accessing Explorer's Preview pane images the proper solution.

Where To Go:
So... Easy enough....

My question can be concluded by asking for some direction on how to achieve this in VBA. First, do you know a solution that exists already? If not, I suspect i'll need to use the Win API, which I'm not really experienced in. If that is the case, can someone point me in the right direction for this- if this is even feasible.

Thanks for your time

--Regards

----------------------------------------------
EDIT- SUPPORTED LINKS:
docs.microsoft preview-handlers
thumbnail-for-any-file-in-windows
c-sharp-get-thumbnail-from-file-via-windows-api
 

isladogs

MVP / VIP
Local time
Today, 12:00
Joined
Jan 14, 2017
Messages
18,186
You can indeed view a number of file types within Access using a Web browser control. These include PDF, Office files (Word, Excel, PowerPoint), zip, text as well as image files. There may be others I've forgotten.
See my post #2 at https://www.utteraccess.com/topics/2061897/posts/2786996 for screenshots and a sample app.

In recent versions of Access, you may have to apply a registry fix (supplied) to prevent e.g. Word files opening in their own window instead of Access.
However, I've not been able to open some other file types successfully within Access e.g. JSON
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:00
Joined
May 7, 2009
Messages
19,169
webbrowser control already has that feature?
see this sample.
 

Attachments

  • winExplorer.accdb
    512 KB · Views: 713

ironfelix717

Registered User.
Local time
Today, 08:00
Joined
Sep 20, 2019
Messages
193
Hi,

I had a look at this example by @arnelgp - which includes the reg file to allow office files to open in the control.

Yes, the web browser control does a decent job at previewing a variety of common files. it is a better option that the image control. But doesn't solve the issue of previewing other formats that Windows is able to with supporting software (ex: .DXF).

The other thing is that the browser control offers more than just a 'preview', but rather a near full functional window. I imagine it can be locked to prevent changes. When uploading other types to the control, I am afraid the default application will launch for other formats. What if I select an .EXE? Will it execute? Not sure. .. Just my thoughts.

I'll consider it for a last ditch option, as there is some value there. Its quite fast too. But would really love to tap into the Win API.

Thanks a lot
 

sonic8

AWF VIP
Local time
Today, 13:00
Joined
Oct 27, 2015
Messages
998
But would really love to tap into the Win API.
The easier route is probably to reference a .NET library providing the functionality. The WindowsAPICodePack should include the functionality you are looking for.

If you want to implement your own solution using only VBA and the Windows API, you are facing a tough challenge.
Years ago I wrote a text on using raw COM APIs with VBA. The functionality implemented in that text is not related your requirements, but it is actaually from the same set of APIs you are probably going to need. So, this might give you an idea of the approach.
 

ironfelix717

Registered User.
Local time
Today, 08:00
Joined
Sep 20, 2019
Messages
193
The easier route is probably to reference a .NET library providing the functionality. The WindowsAPICodePack should include the functionality you are looking for.

If you want to implement your own solution using only VBA and the Windows API, you are facing a tough challenge.
Years ago I wrote a text on using raw COM APIs with VBA. The functionality implemented in that text is not related your requirements, but it is actaually from the same set of APIs you are probably going to need. So, this might give you an idea of the approach.


Great information. I saw the API code pack but its basically over my head as to how to actually leverage it...

I was able to play around with what little information exists out there for "generating thumbnails" or.. "Generating file previews."...

I found some C#.Net code (still cannot find the link of the original author) that demonstrates generating "file thumbnails" of any file passed. Which is not the same as rendering a "file preview"... I mistakenly use the terms interchangeably as much as anyone else...

I also played around with the API code pack in C# using the examples I found posted. and this resulted in the same thing. Every example out there just gave "file thumbnails"...

So, I settled on a "thumbnail generating" solution, which isn't all that noble, but it is kind of... useful... i guess. Does not use the codepack. It works well. So, here it is.

Install:
1.) Download project folder
2.) Finished binaries can be located in \bin\Debug\net5.0
3.) *Requires .NET 5.0 Framework from MS.
4.) executable must stay with project folder!

Call from CMD:

CMDPrompt> ThumbIconGenerator.exe [input file] [outputfile] [size]

ex: ThumbIconGenerator.exe C:\Report.pdf C:\ReportThumb.jpeg 256


Ahh, not what I was looking for, and I don't think i'll rest until I find it. But good enough for now...
 

Attachments

  • ThumbIconGenerator.zip
    612.9 KB · Views: 514

ironfelix717

Registered User.
Local time
Today, 08:00
Joined
Sep 20, 2019
Messages
193
Updating to include another interesting link:


Further reinforcing it's da*n near impossible to get these previews right from the shell.

Will update if I find any solutions.
 

Sun_Force

Active member
Local time
Today, 21:00
Joined
Aug 29, 2020
Messages
396
webbrowser control already has that feature?
see this sample.
@arnelgp
Your combo box doesn't show network drives.
To add network drives, I had to check IF FSO.DriveExists("D") =TRUE and loop From D to Z.
Is there any more convenient way to add network drives too?
Code:
    Dim ascii As Integer
    Set fs = CreateObject("Scripting.FileSystemObject")   
    For ascii = 65 To 90
        If fs.DriveExists(Chr(ascii)) Then
            s = s & Chr(ascii) & vbCrLf
        End If
    Next
Thank you.
 
Last edited:

D_Walla

Member
Local time
Today, 12:00
Joined
Aug 1, 2021
Messages
32
Sorry - I appreciate that this thread is now a few weeks old, but is this the sort of thing you're referring to? It uses the WIN32APIs to extracts the low-res thumbnail saved with the file and then displays it in an imagebox. If the file did not save with a thumbnail, then the associated application's generic icon is listed instead. If so, I can pull out the code (both 32- and 64-bit).

Thumbnail.gif
 

isladogs

MVP / VIP
Local time
Today, 12:00
Joined
Jan 14, 2017
Messages
18,186
@D_Walla
I'm sure that code would be useful to others whether or not the OP is still working on this :)
 

D_Walla

Member
Local time
Today, 12:00
Joined
Aug 1, 2021
Messages
32
Of course, sorry.

I attach 1 x bas file (in txt format) comprising the API-related functions. The key function is ThumbnailPicFromFile - it can take 3 arguments: (1) FilePath (string); (2) Width (long), and (3) Height (long), and returns a StdPicture data type. That being the case, you can implement the function by simply assigning it to the image control:

Code:
Set Image1.Picture = ThumbnailPicFromFile(Filename, 128, 128)

I haven't been able to confirm it yet, but I have a sneaking suspicion that credit for the code belongs to Jaafar Tribak.
I hope that this is helpful.
 

Attachments

  • ThumbnailExtraction_API.txt
    6.8 KB · Views: 620

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:00
Joined
May 7, 2009
Messages
19,169
sample in ms access.
you can now use Image control.
I added a routine that saved the thumbnail
to file so it can be used as Picture property.

sample has both Access Image and Form2.Image
 

Attachments

  • thumbnail from file.accdb
    468 KB · Views: 599
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 20:00
Joined
May 7, 2009
Messages
19,169
come to think again, the db i posted on #3 with the webbrowser does Exactly the same
with Minimal coding and Without using any API.

Shoutout!
 

Users who are viewing this thread

Top Bottom