moving e-mail to different folder help.

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

moving e-mail to different folder help.

09 Jan 2024, 09:56

Hi Guys.

I'm trying to get a code out of an e-mail, move it to another place and then move the mail to a different folder. I don't have any issues with getting the data out. However: Once I return to outlook, things go wrong.

THis is my code:

Code: Select all

WinActivate "RS MST CRM - CVent Alerts - RS MST CRM"

olApp := ComObject("Outlook.Application")
olNameSpace := olApp.GetNamespace("MAPI")
olFolderInbox := olNameSpace.GetDefaultFolder(6)
olDestFolder := olFolderInbox.Folders("Alerts").Folders("Done")
	
olEmails := olApp.ActiveExplorer.Selection
olEmails.Item(1).Move(olDestFolder)
So: When the WinActivate is triggered, the mail is already selected with a reading pane on the right.
The error I get is:

Error: (0x80041200)
The attempted operation failed. An object could not be found.
Source: Microsoft Outlook


The thing is: I might not have a very standard e-mail setup (I don't know if this is relevant). In my outlook 4 mailboxes are open and this is just one of them. So maybe the error comes from there? Since it's a company system the other mailboxes haven't been added in the regular way via the "Add another account" option, but rather via this route:
File > Account Settings > Account Settings > Double Click on my main account > More Settings > Advanced > and there the other accounts are added.

Hope Somebody can help me out here.
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: moving e-mail to different folder help.

09 Jan 2024, 16:39

Coasterfrenzy wrote:
09 Jan 2024, 09:56
THis is my code:

Code: Select all

WinActivate "RS MST CRM - CVent Alerts - RS MST CRM"

olApp := ComObject("Outlook.Application")
olNameSpace := olApp.GetNamespace("MAPI")
olFolderInbox := olNameSpace.GetDefaultFolder(6)
olDestFolder := olFolderInbox.Folders("Alerts").Folders("Done")
	
olEmails := olApp.ActiveExplorer.Selection
olEmails.Item(1).Move(olDestFolder)
So: When the WinActivate is triggered, the mail is already selected with a reading pane on the right.
The error I get is:

Error: (0x80041200)
The attempted operation failed. An object could not be found.
Source: Microsoft Outlook

Item should be Items

Instead of activating a window and using selection, you can normally just directly specify the folder and email you want to move or all the emails in a folder.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: moving e-mail to different folder help.

09 Jan 2024, 22:13

Here's some of the things I found. You might want to read my edit at the bottom first.
  • FanaticGuru wrote:
    09 Jan 2024, 16:39
    Item should be Items
    Item worked when I tested it, so I am not sure that is the issue here.
    :arrow: Selection.Item
  • Coasterfrenzy wrote:
    09 Jan 2024, 09:56
    The thing is: I might not have a very standard e-mail setup (I don't know if this is relevant). In my outlook 4 mailboxes are open and this is just one of them. So maybe the error comes from there? Since it's a company system the other mailboxes haven't been added in the regular way via the "Add another account" option, but rather via this route:
    File > Account Settings > Account Settings > Double Click on my main account > More Settings > Advanced > and there the other accounts are added.
    If you have multiple mailboxes / accounts, then this may not be getting the correct inbox: .GetDefaultFolder(6)
    I'm not sure if the following will work with your setup. I tested it on my Outlook that has two mailboxes and was able to get emails from both of them.

    Code: Select all

    olApp := ComObjActive("Outlook.Application")
    ; Something like this, but replace the first folder with your account name
    destFolder := olApp.Session.Folders("The name of the mailbox").Folders("Inbox").Folders("test")
    ; or
    destFolder := olApp.Session.Folders("example_email.xyz").Folders("Inbox").Folders("test")
    
  • Coasterfrenzy wrote:
    09 Jan 2024, 09:56
    Error: (0x80041200)
    The attempted operation failed. An object could not be found.
    Source: Microsoft Outlook
    Is there more information in the error? Like what line it is on. Does it say which object it is referring to? It sometimes says "Specifically: XXXX" where XXXX is the object in question. You can copy the whole error message with Ctrl+A and Ctrl+C if you don't want to type it all out.
  • I agree with FanaticGuru that you may not need to bother activating windows, unless your other program requires it. Does your script only fail when the WInActivate part is added? If you run only the part with the error message, and remove the rest of your script, does it still give you the same error?
  • Does your script change the Selection? If so, then you may have trouble with olApp.ActiveExplorer.Selection.
    If I don't have anything selected I can get the same error code. For example:

    Code: Select all

    olApp := ComObjActive("Outlook.Application")
    MsgBox olApp.ActiveExplorer.Selection.Item(1).Subject ; If nothing is selected then error

    Code: Select all

    Error: (0x80041200) 
    Array index out of bounds.
    Source:	Microsoft Outlook
    
    Specifically: Item
    
    	001: olApp := ComObjActive("Outlook.Application")
    ▶	002: MsgBox(olApp.ActiveExplorer.Selection.Item(1).Subject)
    	003: Exit
    
This worked for me:

Code: Select all

olApp := ComObjActive("Outlook.Application")
destFolder := olApp.Session.Folders("MyMailbox").Folders("Inbox").Folders("test")
;MsgBox destFolder.Name ; check if it got a folder
;MsgBox olApp.ActiveExplorer.Selection.Item(1).Subject ; Check the selceted mailitem works
emails := olApp.ActiveExplorer.Selection
;MsgBox emails.Item(1).Subject
emails.Item(1).Move(destFolder)
Quick edit:
Are you sure that "Alerts" is sub-folder of the inbox and not just another folder. And "Done" is a sub-folder of "Alerts"? If I get the folder path wrong I can get your exact error.

Code: Select all

olDestFolder := olFolderInbox.Folders("Alerts").Folders("Done")

Code: Select all

destFolder := olApp.Session.Folders("SomeFolder").Folders("FolderThatDoesNotExist")
/*
Error: (0x80041200) 
The attempted operation failed.  An object could not be found.
Source:	Microsoft Outlook
Last edited by Datapoint on 09 Jan 2024, 22:37, edited 1 time in total.
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

10 Jan 2024, 07:33

Thanks for your help, guys.

I updated my code to this:

Code: Select all

olApp := ComObjActive("Outlook.Application")
destFolder := olApp.Session.Folders("RS MST CRM").Folders("Inbox").Folders("CVent").Folders("Done")
;MsgBox destFolder.Name ; check if it got a folder
;MsgBox olApp.ActiveExplorer.Selection.Item(1).Subject ; Check the selceted mailitem works
emails := olApp.ActiveExplorer.Selection
;MsgBox emails.Item(1).Subject
emails.Item(1).Move(destFolder)
THis is still the Error I get.
image.png
image.png (19.66 KiB) Viewed 892 times
My mailbox looks like this, so I think the hierarchy is correct, right?
image.png
image.png (8.74 KiB) Viewed 892 times
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

10 Jan 2024, 07:54

Sorry for the double post.. the screenshot was incorrect:
image.png
image.png (18.99 KiB) Viewed 881 times
I also removed all code so that only this part is left:

Code: Select all

#c::
{
olApp := ComObjActive("Outlook.Application")
destFolder := olApp.Session.Folders("RS MST CRM").Folders("Inbox").Folders("CVent").Folders("Done")
;MsgBox destFolder.Name ; check if it got a folder
;MsgBox olApp.ActiveExplorer.Selection.Item(1).Subject ; Check the selceted mailitem works
emails := olApp.ActiveExplorer.Selection
;MsgBox emails.Item(1).Subject
emails.Item(1).Move(destFolder)

Sleep(5000)
MsgBox "Finished execution" 
}
but the issue remains the same.
Attachments
image.png
image.png (18.76 KiB) Viewed 881 times
User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: moving e-mail to different folder help.

10 Jan 2024, 11:26

Shouldn't it be "CVent Alerts" not just "CVent"? You could try this to see where it is failing to get a folder:

Code: Select all

olApp := ComObject("Outlook.Application")

RS := olApp.Session.Folders("RS MST CRM")
MsgBox RS.Name  ; check

IB := RS.Folders("Inbox")
MsgBox IB.Name  ; check

CV := IB.Folders("CVent Alerts")
MsgBox CV.Name  ; check

destFolder := CV.Folders("Done")
MsgBox destFolder.Name  ; check
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

10 Jan 2024, 12:23

Datapoint wrote:
10 Jan 2024, 11:26
Shouldn't it be "CVent Alerts" not just "CVent"? You could try this to see where it is failing to get a folder:
Good catch.. I changed it, but forgot to update it.

Nevertheless: Even after fixing things go wrong.

THe mailbox can be found. Then Inbox can be found as well. As soon as the MessageBox for CVent Alerts is supposed to pop up, the error occurs again:
image.png
image.png (30.68 KiB) Viewed 853 times
Is there a possibility that Outlook has another name for this folder that needs to be used for calls like this??
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

10 Jan 2024, 12:33

As an extra addition: It does work with the folder "Apex" so it seems that the space in the folder name is the issue. However: I cannot change that as it would mess up some rules and other processes which I cannot update for a changed Folder Name.
User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: moving e-mail to different folder help.

10 Jan 2024, 13:33

I don't think that the space is the issue. I tried it on a folder with a space in the name and it seemed to work. Maybe there is an extra space or an invisible character in there? You could copy and paste the folder name directly from outlook.

You can also loop through the folder:

Code: Select all

...
for k, v in IB.Folders() {
	MsgBox k.name "`n" v
	;if k.name = "Cvent Alerts" {
	;	CV := k
	;	break
	;}
}
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

11 Jan 2024, 05:25

Did some testing again. At first the Apex folder could no longer be found. Nor could any other folder. I did a reboot and then all worked again. I updated the script to actually move the email: That again worked but after a bit it stopped working again and now no other folders can be found again.

Is it possible that something is messing with the COM interface?? Are there any known applications that cause this??
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: moving e-mail to different folder help.

11 Jan 2024, 21:49

This code might be useful to some to display all your Outlook folders and paths.

Code: Select all

olApp := ComObjActive("Outlook.Application")
olNameSpace := olApp.GetNamespace("MAPI")

List := SubFolders(olNameSpace)

MsgBox List

; Function to recursively loop through all folders and subfolders and build List
SubFolders(olFolder, &List := '', Indent?)
{
	Indent .= '    '
	for olSubfolder in olFolder.Folders
	{
		List .= Indent olSubfolder.Name '`t> ' olSubfolder.FolderPath '`n' 
		SubFolders(olSubfolder, &List, Indent)
	}
	Indent := ''
	return List
}
It is a tool but also demonstrates the recursive technique that can be used for other purposes.

I have multiple email accounts and pst open in one Outlook setup and this seems to transverse all the folders correctly. It is important to realize this goes through in the order stored internally and not the order displayed in Outlook. So, within the same level, items can be in a somewhat random order having to do with when they were created.

Probably will not help in your case where things work but then stop working. I have heard that COM can stop working if not using a genuine copy of Outlook or there is a credentials problem, but I have never actually experienced this problem myself.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

15 Jan 2024, 13:44

FanaticGuru wrote:
11 Jan 2024, 21:49

Probably will not help in your case where things work but then stop working. I have heard that COM can stop working if not using a genuine copy of Outlook or there is a credentials problem, but I have never actually experienced this problem myself.
Seems like it's related though. I did have some issues with credentials.. however: Even now it seems like it's completely random to what folders can be found and which one not. Sometimes the script runs fine. Then I try again, and it ends with the error that the folder cannot be found anymore. Really confusing this.

There also doesn't seem to be much information online about COM, how it works, bugs and maybe even how to restart it.. Or my Google isn't used to looking for it maybe.

Maybe I will opt to make the mouse drag and drop the email into the right folder. It's shoddy design, but it would be better than running errors all the time.
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

16 Jan 2024, 06:25

One thing I'm specifically noting:

If I restart outlook, it often works. Then I run the script. The script works. After executing the script once: It no longer works.

That means: Every part of the script works, until I get to the fuction where I use the outlook COM connection.

The function currently looks like this:

Code: Select all

ProcMail(DestFolder)
{
	WinActivate "RS MST CRM - CVent-Alerts - RS MST CRM"
	Sleep(100)

	; MsgBox DestFolder.Name ; check whether it got a folder
	; MsgBox olApp.ActiveExplorer.Selection.Item(1).Subject ; Check the selected mailitem works

	emails := olApp.ActiveExplorer.Selection

	; MsgBox emails.Item(1).Subject

	emails.Item(1).Move(DestFolder)

	Sleep(5000)
	MsgBox "Finished execution"
	Exit 
}
The code starts like this way before calling the function:

Code: Select all

#c::
{
olApp := ComObjActive("Outlook.Application")
StartFolder := olApp.Session.Folders("RS MST CRM").Folders("Inbox").Folders("CVent-Alerts")

this park usually seems to work. But as soon as I add a folder that goes deeper. The error pops up again. Like for example this part:

Code: Select all

	DestFolder := StartFolder.Folders("Todo").Folders("No Campaign") ; Set Destination Folder to Process E-Mail
	ProcMail (DestFolder)
Coasterfrenzy
Posts: 14
Joined: 21 Nov 2023, 08:09

Re: moving e-mail to different folder help.

16 Jan 2024, 07:54

I made a test script so I can check whether things are working or not.. Generally they rarely work. IF it works, it still stops after one go:

Code: Select all

#t::
{
olApp := ComObjActive("Outlook.Application")
Mailbox := olApp.Session.Folders("RS MST CRM")
MsgBox Mailbox.Name
Inbox := Mailbox.Folders("Inbox")
MsgBox Inbox.Name
StartFolder := Inbox.Folders("CVent-Alerts")
MsgBox StartFolder.Name
Done := StartFolder.Folders("Done")
MsgBox Done.Name
Todo := StartFolder.Folders("Todo")
MsgBox Todo.Name
Locked := Todo.Folders("Locked for editing")
MsgBox Locked.Name
Campaign := Todo.Folders("No Campaign")
MsgBox Campaign.Name
}

And guess what: If it fails: It's ALWAYS on the sub folders of "CVent-Alerts". The Inbox and the folder "CVent-Alerts" can always be found (only not on extremely rare occasions. WHich I believe are actually caused by Credential Issues).

Maybe I should try and find an expert in this COM thing.. but can't seem to find anything about it online. Anybody knows where to ask?
User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: moving e-mail to different folder help.

16 Jan 2024, 11:27

Coasterfrenzy wrote:
16 Jan 2024, 07:54
Maybe I should try and find an expert in this COM thing.. but can't seem to find anything about it online. Anybody knows where to ask?
I would suggest looking for info about Outlook credentials specifically, not COM in general.

COM is just a standard way for programs to communicate with each other. If you think of each program as a black box that could do anything, COM is the input/output port on the black box. Each program that uses COM is written completely differently. So in your case, you would have better luck looking into Outlook-specific solutions.
User avatar
Datapoint
Posts: 311
Joined: 18 Mar 2018, 17:06

Re: moving e-mail to different folder help.

16 Jan 2024, 11:58

I wonder if using .PickFolder would help. This will prompt you to pick the destination folder to move an email to.

Code: Select all

F3:: 
OutlookMove(*) {
	static olMailFolder := ""
	olApp := ComObjActive("Outlook.Application")
	try
		olApp.ActiveExplorer.Selection.Item(1).Move(olMailFolder)
	catch {
		olMailFolder := olApp.Session.PickFolder
		OutlookMove()
	}
}
Edit:
I did more searching and found some interesting results (1 | 2). As with most Microsoft issues; if you have an issue, there are many other people out there who have been in your shoes. Unfortunately, I couldn't find a definitive solution yet. The best I can tell is that it may be because this is a sub-folder of Inbox (link). Or you might need to use GetSharedDefaultFolder and the "mailbox owner's display name, alias, or email address when resolving the recipient." (link) Or there might be some folder setting you could change. (link)

The following post, for example, had similar symptoms as you are describing, although it is related to Rules, not VBA. (link) (Same error, it is a shared mailbox and the issue is intermittent.)

I'm again leaning towards saying this is an Outlook setup issue or bug, and not AHK specific or related.

Your original post included the following. Not sure if this is contributing, but seems like a good place to investigate further.
The thing is: I might not have a very standard e-mail setup (I don't know if this is relevant). In my outlook 4 mailboxes are open and this is just one of them. So maybe the error comes from there? Since it's a company system the other mailboxes haven't been added in the regular way via the "Add another account" option

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 75 guests