Windows Recent File List

Programming and macros
User avatar
SPerman
Posts: 2055
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2226
x 1876
Contact:

Windows Recent File List

Unread post by SPerman »

This is going to be a strange one. Hopefully I can describe it properly.

If I do a "Save as PDF" on a drawing, and then go to outlook, that pdf appears in my "Recent Items" list (for example, if I want to attach that file to an email.)

However, if I run the macro I created that does the same "Save as PDF" process, it does NOT appear in my "Recent Items" list. Do any of you API guru's know if I can add something to my macro to change this behavior?
image.png
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
JSculley
Posts: 643
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 877

Re: Windows Recent File List

Unread post by JSculley »

image.png
It doesn't show up because the SaveAs isn't using the correct Windows API calls to ensure the files get added to the MRU (most recently used) list. You can programmatically add it to the MRU after you save by hooking into the Windows API.

Add this enum to your namespace:

Code: Select all

public enum ShellAddToRecentDocsFlags
    {
        Pidl = 0x001,
        Path = 0x002,
        PathW = 0x003
    }
Add this method declaration to you class:

Code: Select all

[DllImport("Shell32.dll", BestFitMapping=false, ThrowOnUnmappableChar=true)]
private static extern void SHAddToRecentDocs(ShellAddToRecentDocsFlags flag, [MarshalAs(UnmanagedType.LPStr)]string path);
Then, after you save the PDF, call the method:

Code: Select all

mExt.SaveAs(path, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, exportData, ref  errors, ref warnings);
SHAddToRecentDocs(ShellAddToRecentDocsFlags.Path, path);
The file will now show up:
image.png
User avatar
SPerman
Posts: 2055
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2226
x 1876
Contact:

Re: Windows Recent File List

Unread post by SPerman »

This is exactly what I was looking for. Thank you!
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 377
x 136

Re: Windows Recent File List

Unread post by CarrieIves »

@JSculley ,

My save as PDF macro is in VBA. Are the code snippets you provided in VBA or something else? I'm still learning my way around macros for SolidWorks.

Thanks,
Carrie
User avatar
JSculley
Posts: 643
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 877

Re: Windows Recent File List

Unread post by JSculley »

CarrieIves wrote: Tue Jan 25, 2022 11:20 am My save as PDF macro is in VBA. Are the code snippets you provided in VBA or something else? I'm still learning my way around macros for SolidWorks.
The snippets are C#. You can do the same sort of thing with VBA. Add these constants:

Code: Select all

Const Pidl = 1
Const Path = 2
Const PathW = 3
And this declaration:

Code: Select all

Private Declare PtrSafe Sub SHAddToRecentDocs Lib "shell32" _
(ByVal uFlags As Long, ByVal pv As Any)
After you save your PDF, call the new function like this:

Code: Select all

SHAddToRecentDocs Path, filePath
Note: Path is the constant that was declared at the top, whereas filePath is the path to the file you saved.
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 377
x 136

Re: Windows Recent File List

Unread post by CarrieIves »

@JSculley THANKS ><
User avatar
SPerman
Posts: 2055
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2226
x 1876
Contact:

Re: Windows Recent File List

Unread post by SPerman »

I tested it this morning and it works perfectly. Thanks again @JSculley
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
Jaylin Hochstetler
Posts: 387
Joined: Sat Mar 13, 2021 8:47 pm
Answers: 4
Location: Michigan
x 380
x 355
Contact:

Re: Windows Recent File List

Unread post by Jaylin Hochstetler »

FYI,
If you ever want to do this in .NET, which I did recently your code needs to look like this

Code: Select all

Public Enum ShellAddToRecentDocsFlags
    Pidl = &H1
    Path = &H2
    PathW = &H3
End Enum

<DllImport("Shell32.dll", BestFitMapping:=False, ThrowOnUnmappableChar:=True)>
Private Shared Sub SHAddToRecentDocs(ByVal flag As ShellAddToRecentDocsFlags,
<MarshalAs(UnmanagedType.LPStr)> ByVal path As String)
Granted, I'm sure there are other ways of doing it too.
A goal is only a wish until backed by a plan.
Post Reply