Attach the latest created PDF to email

Peque

Member
Joined
Sep 14, 2023
Messages
15
Programming Experience
Beginner
Hi Group.
I'm interested in if its possible to find and get the latest created PDF file on the PC - and attach it automatically?
I'm just trying to learn a little about the Outlook Addin - and trying to create my own groups, where I have some predefined steps

The Scenario is - when creating an order - the system sends an email with our attachment to our inbox, where we saved the file on a servershare as PDF-XXXXXXXXXXX .
Now we create a order reply to the customer - and having to get back and attach the PDF file. Since it possible through the attach file group, and its showing the latest saved 10 files ?
And I'm thinking I cannot be the first one, but cannot find any examples of that, or I'm not using the right wordswhen googling this

Thanks in advance
P
 
Try search terms like "c# latest recent newest file"
 
You're asking multiple questions in one here. One of the main reasons that people can't solve problems is that they try to treat multiple problems as one and look for a single solution. Break the problem down into its constituent parts and address each part individually. Some you will be able to do off your own bat, while others you'll be able to do after researching that specific subject. The ones you can't solve on your own, you can ask us about, one at a time. Scanning a PC for files is one thing. Determining the newest file from multiple is another thing. Attaching a file to an email is another thing. None of them are directly related to the other. Work out how to do each on its own and then put the parts together. That's how programming works: learn to do elementary things and then put them together in various combinations as needed.
 
Well so sorry - not for asking correctly, and not see the right way .
I saw it as a simple question, trying to describing the a scenario, for the job, since my meaning was just to get the latest edited PDF - where the Outlook already have the path fo the latest edited files.

Attachements etc I do have in place, so it ws just getting tha last PDF
 
just to get the latest edited PDF

So what's the actual problem: finding PDF files on a drive or in a folder or is it comparing the created/modified times of files? Again, these are two different things. You can find files of a type without caring about when they were created/modified and you can determine which file is newer without having to find them. You need to identify where the actual problem is in order to solve it.
 
Sorry to ask Why?
Since the list already exist within Outlook - during indexing in in the background by Windows - But that works pretty weel and quick to recognize new files all over the Network, for the localuser (within msec). so why go over the bridge after water
1695975541572.png
 
So find that list of files in the Outlook object model where it exposes that "most recently used" files and just use that list of files. My gut feel is that object model has not been keeping pace with the UI features and that list is not exposed. Then you are right back where you started and need to find the files yourself.
 
This might help finding the most recently used files:
 
Enumerating the shortcuts in SpecialFolder.Recent is a good bet. I my case though, the folder is empty, I think it is because I have turned off 'show recent files' in Explorer. Instead I found Explorer MRU lists in registry in a OpenSavePidlMRU subkey. There is a subkey for each file extension and a "*" subkey for all file types combined. This code shows the MRU for pdf extension:
C#:
private static void OpenSaveMRU()
{
    var subkey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\pdf";
    using (var key = Registry.CurrentUser.OpenSubKey(subkey, false))
    {
        var order = (byte[])key.GetValue("MRUListEx");
        for (var i = 0; i < order.Length - 4; i += 4) // last 4 bytes = -1 terminator
        {
            var item = BitConverter.ToInt32(order, i);
            var data = (byte[])key.GetValue(item.ToString());                  
            var path = GetPathFromPIDL(data);
            Console.WriteLine(path);
        }
    }
}

public static string GetPathFromPIDL(byte[] byteCode)
{
    const int MAX_PATH = 260;
    var builder = new StringBuilder(MAX_PATH);
    var h = GCHandle.Alloc(byteCode, GCHandleType.Pinned);
    try
    {
        SHGetPathFromIDListW(h.AddrOfPinnedObject(), builder);
    }
    finally
    {
        h.Free();
    }
    return builder.ToString();
}

[DllImport("shell32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SHGetPathFromIDListW(IntPtr pidl, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder pszPath);
 

Latest posts

Back
Top Bottom