Page 1 of 1

Windows Recent File List

Posted: Mon Jan 24, 2022 11:46 am
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

Re: Windows Recent File List

Posted: Mon Jan 24, 2022 6:53 pm
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

Re: Windows Recent File List

Posted: Mon Jan 24, 2022 8:45 pm
by SPerman
This is exactly what I was looking for. Thank you!

Re: Windows Recent File List

Posted: Tue Jan 25, 2022 11:20 am
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

Re: Windows Recent File List

Posted: Tue Jan 25, 2022 11:52 am
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.

Re: Windows Recent File List

Posted: Tue Jan 25, 2022 11:54 am
by CarrieIves
@JSculley THANKS ><

Re: Windows Recent File List

Posted: Wed Jan 26, 2022 9:47 am
by SPerman
I tested it this morning and it works perfectly. Thanks again @JSculley

Re: Windows Recent File List

Posted: Wed Dec 21, 2022 5:56 pm
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.