Help on saving attachment

Are you using relative paths? Do any of the paths in the log file start with, say, C:\?

It’s always better to be explicit and use absolute paths.

It might be that your script is making paths relative to wherever the current working directory happens to be, but when you tell Outlook to save a file with a relative path, it’s trying to save relative to its default directory.

For example, suppose your current working directory is C:\MyStuff. You make a relative path MyFolder\MyFile and, as far as you’re concerned, the full path is C:\MyStuff\MyFolder\MyFile, but Outlook’s default directory is C:\OutlookStuff, so when you tell it to save as MyFolder\MyFile, it tries to save as C:\OutlookStuff\MyFolder\MyFile, but C:\OutlookStuff\MyFolder doesn’t exist.

actually it works with absolute path and i tried writing simpler one it dis download the attchamnets but the thing here is i wanted everything to be dynamiclly generated not hard codes even if a user used this it can run smoothly thats my goal to adapt without hardcoding path

Well, if you print base_path, as soon as you create it, it is not generating what you think it is. Go ahead and try it.

i feel i get what you mean yet it make since somehow though i am not quit understanding but can you please explain more or with another example and how would that be resolved ?

it generates the main folder name which would be Nov_follow_up

That is a desired folder name but NOT a path. You need an actual valid directory path.

Using an analogy, you’re giving them your house number only but not the state, city, and zip code. You need all elements in order for someone to send you a letter.

would not path(base_path) create it as path object and fix that as we proceed? correct me please if i am misunderstanding that

It does not “create” it for you. You tell it what you want it to be and then saves it as a reference.

I understand now but i want to understand something is that only regarding saving the attachment it self right ? cuz the main folder and sub folders are created fine even the subfolders of subfolders with names of the attachments are created so we only talk about attachments alone right ?

and what do you recommend other than absolute path i want to learn make things dynamic adapting any position

pardon me so now what do you recommend to adjust ? even if there is a different approach but maintaining same logic of making things dynamic

Look at your computer. There must be hundreds of folders (thousands). You’re the developer. You define the special directory where you want these modules/files to be saved to. It can’t be random.

Probably the simplest fix to ensure that you give Outlook the full absolute path is:

attachment.SaveAsFile(str(attachment_file_path.absolute()))

you are right i dont see any of the main partitions c,d
only the start lf the main folder and goes down to the attachment name

how can i fix that

cant i just make it so whenever the use would want to place the exe file and run ? didnt want to fix destination or so i heard for better experience

i am just thinking for providing more options thats the main goal

You can prompt the user to enter a desired path (directory) to wherever they wish to save it to. You can try that.

how to implement and what to remove then for the relative paths ? i know its silly to ask but i really dont want to mess up and come up with new issue :sweat_smile:

You can try something like this where the user is prompted for a directory to save the file to:

from tkinter.filedialog import askdirectory
path = askdirectory(title = 'Select Folder')

base_path = Path(path)
print(base_path)    # Printing for test purposes only

Alright. I am out.

Good luck.

Cheers!

oh no i didnt learn building a GUI yet so didnt build it to be so only knew to convert it to exe and run it with cmd but much gratitude for this

You’re not building a GUI. You’re just defining a directory on the fly since as you stated you wanted it to be dynamic (user selectable).

Thank you so much for helping God bless you and bless your knowledge