Its halting onRefrence to Outlook was set withCode:For intIndex = olkSource.Items.Count To 1 Step -1Code:Dim olApp As Object
Or if you mean refrence to the Outlook library then Outlook 14.0 Object library was made.
Its halting onRefrence to Outlook was set withCode:For intIndex = olkSource.Items.Count To 1 Step -1Code:Dim olApp As Object
Or if you mean refrence to the Outlook library then Outlook 14.0 Object library was made.
I looked at the functions you are using and they kinda make sense but I do not see them as necessary. I tried to get them to work but ... well, maybe the functions were built to do something other than what it is you are trying to accomplish. It seemed easier to just write some new code. I was able to move emails from an Inbox to another folder in the same account using the following.
Code:Dim account As Outlook.MAPIFolder Dim inBox As Outlook.MAPIFolder Dim destFolder As Outlook.MAPIFolder Set account = Session.Folders("NameOfPST_File") 'The name of the account's PST file. Default is Outlook Dim obj As Object For Each obj In account.Folders If obj.Name = "Inbox" Then 'The inbox I want to search Set inBox = obj End If If obj.Name = "MyDestinationFolder" Then 'The folder name I want to move stuff to Set destFolder = obj End If Next obj On Error Resume Next For Each obj In inBox.Items If InStr(obj.Subject, "Keyword or meta data to search for") Then 'Search the subject line for important stuff obj.Move destFolder End If Next Set account = Nothing Set inBox = Nothing Set destFolder = Nothing MsgBox "Complete"
Thanks very much ItsMe, after tweaking some things like changing Inbox to Postvak IN (Dutch for Inbox) and gambling on the PST files name (couldnt find it in Account settings) it finally worked !
The only thing i need to do is move the emails further down the line because the folder "Test" is in the root of the account and the folder it really needs to go is \\<my account name>\Postvak IN\Archiefkast\Storing HK.
Hopefully i can solve that one myself, allthough im open for suggestions offcourse
Anyway, im very pleased with the way help was offered by you and also June ofcourse.
Thanks a million !
Greetings, Jeroen
If you are not using Exchange server, you can get the account name by looking at the PST file. There are probably other ways but I am weird and can't remember how to get it from Outlook's various dialog boxes. The PST file resides in your User's Documents Folder>Outlook.
I created some untested pseudo code for going after subfolders. It is how I would go about it ...
Code:Dim account As Outlook.MAPIFolder Dim inBox As Outlook.MAPIFolder Dim destFolder As Outlook.MAPIFolder Dim subfolder1 As Outlook.MAPIFolder Dim subfolder2 As Outlook.MAPIFolder Set account = Session.Folders("NameOfPST_File") Dim obj As Object For Each obj In account.Folders If obj.Name = "Inbox" Then Set inBox = obj End If Next obj 'We have the inbox. Now lets look in here for subfolders For Each obj In inBox If obj.Name = "SubfolderOne" Then Set subfolder1 = obj End If Next obj 'We have the correct subfolder. Now lets look in here for sub-subfolders For Each obj In subfolder1 If obj.Name = "SubfolderTwo" Then Set subfolder2 = obj End If Next obj ''This destination folder has been orphaned ''We will need to decide where, within the ''hierarchy, to instantiate this object ' If obj.Name = "MyFolder" Then ' Set destFolder = obj ' End If On Error Resume Next For Each obj In inBox.Items 'TODO the destination folder may not be in the "InBox" If InStr(obj.Subject, "Keyword or meta data to search for") Then ' obj.Move destFolder 'TODO we need to instantiate the destFolder End If Next Set account = Nothing Set inBox = Nothing 'Set destFolder = Nothing Set subfolder1 = Nothing Set subfolder2 = Nothing MsgBox "Complete"
Hi ItsMe, and thanks again for looking into my problem.
I adjusted your code and set subfolderOne and subfolderTwo to the underlaying folders.
Unfortunately im getting an error on the line "For Each obj In inBox"
It says 'Property or method is not supported'
Code:Dim account As Outlook.MAPIfolder Dim inBox As Outlook.MAPIfolder Dim destFolder As Outlook.MAPIfolder Dim subfolder1 As Outlook.MAPIfolder Dim subfolder2 As Outlook.MAPIfolder Set account = Session.Folders("<my account name>") Dim obj As Object For Each obj In account.Folders If obj.Name = "Postvak In" Then Set inBox = obj End If Next obj 'We have the inbox. Now lets look in here for subfolders For Each obj In inBox If obj.Name = "Archiefkast" Then Set subfolder1 = obj End If Next obj 'We have the correct subfolder. Now lets look in here for sub-subfolders For Each obj In subfolder1 If obj.Name = "Storing HK" Then Set subfolder2 = obj End If Next obj ''This destination folder has been orphaned ''We will need to decide where, within the ''hierarchy, to instantiate this object ' If obj.Name = "MyFolder" Then ' Set destFolder = obj ' End If On Error Resume Next For Each obj In inBox.Items 'TODO the destination folder may not be in the "InBox" If InStr(obj.Subject, "Test") Then ' obj.Move destFolder 'TODO we need to instantiate the destFolder End If Next Set account = Nothing Set inBox = Nothing 'Set destFolder = Nothing Set subfolder1 = Nothing Set subfolder2 = Nothing MsgBox "Complete"
Oh by the way, i am using a corporate account (exchange).
Maybe thats why i coudnt find the PST file
Yah, within some of these objects there are many other objects. Different objects have different members (properties and methods). For instance, there may be an object within the Inbox Folder object that does not have a Name property.It says 'Property or method is not supported'
If you notice, I generalize and use
Dim obj as Object
Some children may be folders and other children will be something else. If you noticed on another code block I used "On Error Resume Next". This is a Poor Man's way of dealing with things. Otherwise, you would have to be explicit with the object types you are looping through.
Try this
Instead of justCode:For Each obj In subfolder1.Folders
Post your code when you get things working and I will help you refactor it. When you build code, sometimes you do not do things perfectly the first time around.Code:For Each obj In subfolder1
Thanks for the explanation, it makes me understand whats going on better
I get the same error though.
When i hover with my mouse over Obj then it says "Obj = nothing"
Tried two modules as well, one looked promising but dont really know how to implement it into your code :
Dim account As Outlook.MAPIFolder
Dim inBox As Outlook.MAPIFolder
Dim destFolder As Outlook.MAPIFolder
Set account = Session.Folders("<My Accountname>") 'The name of the account's PST file. Default is Outlook
Dim items As Object
Set items = GetFolderPath("Archiefkast\Storing HK").items
Dim obj As Object
For Each obj In account.Folders
If obj.Name = "Postvak In" Then 'The inbox I want to search
Set inBox = obj
End If
If obj.Name = GetFolderPath("Archiefkast\Storing HK").items Then 'The folder name I want to move stuff to
Set destFolder = obj
End If
Next obj
Dim sOnderwerp As String
sOnderwerp = "" & Format(Date, "dd/mm/yyyy") & " " & Me.fldOnderwerp & " " & Me.fldLocatie & ""
DoCmd.Close acForm, stFormName
On Error Resume Next
For Each obj In inBox.items
If InStr(obj.Subject, sOnderwerp) Then 'Search the subject line for important stuff
obj.Move destFolder
End If
Next
Set account = Nothing
Set inBox = Nothing
Set destFolder = Nothing
MsgBox sOnderwerp
The module :
Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
Dim oFolder As Outlook.Folder
Dim FoldersArray As Variant
Dim i As Integer
On Error GoTo GetFolderPath_Error
If Left(FolderPath, 2) = "\\" Then
FolderPath = Right(FolderPath, Len(FolderPath) - 2)
End If
'Convert folderpath to array
FoldersArray = Split(FolderPath, "\")
Set oFolder = Application.Session.Folders.item(FoldersArray(0))
If Not oFolder Is Nothing Then
For i = 1 To UBound(FoldersArray, 1)
Dim SubFolders As Outlook.Folders
Set SubFolders = oFolder.Folders
Set oFolder = SubFolders.item(FoldersArray(i))
If oFolder Is Nothing Then
Set GetFolderPath = Nothing
End If
Next
End If
'Return the oFolder
Set GetFolderPath = oFolder
Exit Function
GetFolderPath_Error:
Set GetFolderPath = Nothing
Exit Function
End Function
I was able to avoid any errors with the following code. I simply defined the Folders collection for the class.
Code:Dim account As Outlook.MAPIFolder Dim inBox As Outlook.MAPIFolder Dim destFolder As Outlook.MAPIFolder Dim subfolder1 As Outlook.MAPIFolder Dim subfolder2 As Outlook.MAPIFolder Set account = Session.Folders("AccountName") Dim obj As Object For Each obj In account.Folders If obj.Name = "Inbox" Then MsgBox "got inbox" Set inBox = obj End If Next obj 'We have the inbox. Now lets look in here for subfolders For Each obj In inBox.Folders If obj.Name = "FolderOne" Then MsgBox "Got Folder One" Set subfolder1 = obj End If Next obj 'We have the correct subfolder. Now lets look in here for sub-subfolders For Each obj In subfolder1.Folders If obj.Name = "FolderTwo" Then MsgBox "Got Folder Two" Set subfolder2 = obj End If Next obj ''This destination folder has been orphaned ''We will need to decide where, within the ''hierarchy, to instantiate this object ' If obj.Name = "MyFolder" Then ' Set destFolder = obj ' End If On Error Resume Next For Each obj In inBox.Items If InStr(obj.Subject, "some text") Then ' obj.Move destFolder 'TODO we need to instantiate the destFolder End If Next Set account = Nothing Set inBox = Nothing 'Set destFolder = Nothing Set subfolder1 = Nothing Set subfolder2 = Nothing MsgBox "Complete"
With that, I went ahead and continued to test and then move an email from a sub-subfolder within the Inbox to another folder; a subfolder to the Inbox (MovedItems).
The following code was tested with a PST file and Microsoft Exchange.
Code:Dim account As Outlook.MAPIFolder Dim inBox As Outlook.MAPIFolder Dim destFolder As Outlook.MAPIFolder Dim subfolder1 As Outlook.MAPIFolder Dim subfolder2 As Outlook.MAPIFolder Set account = Session.Folders("AccountName") Dim obj As Object For Each obj In account.Folders If obj.Name = "Inbox" Then Set inBox = obj End If Next obj 'We have the inbox. Now lets look in here for subfolders For Each obj In inBox.Folders If obj.Name = "FolderOne" Then Set subfolder1 = obj End If 'My destination folder is a subfolder of the Inbox If obj.Name = "MovedItems" Then Set destFolder = obj End If Next obj 'We have the correct subfolder. Now lets look in here for sub-subfolders For Each obj In subfolder1.Folders If obj.Name = "FolderTwo" Then Set subfolder2 = obj End If Next obj 'Each folder in Outlook is referenced by a variable 'Let's move the relevant emails On Error Resume Next For Each obj In subfolder2.Items If InStr(obj.Subject, "SomeValidText") Then obj.Move destFolder End If Next Set account = Nothing Set inBox = Nothing Set destFolder = Nothing Set subfolder1 = Nothing Set subfolder2 = Nothing MsgBox "Complete"
Finally i got things working !!! Or should i say - You got things working ?- haha
I have to implement this code in my code that actually sends the emails, but im thinking that will be peanuts.
YOU Sir are Boss, thanks for your dedication ItsMe !
Much much much appreciation.
Glad to hear things are going well. In the future you may want to revisit this thread. There are some nuances within that may allow you to glean insight in how to approach these type of problems.
Im thinking that this thread is useful for other users as well.
I visited many other forums and searched for a solution. None of them worked.
Isnt there a page where AccessForums stores tested pieces of code for further use ?
There is the Code Repository. There are some threads in there that members started that do not belong because they did not know which forum to post their questions. But that would be the place to post your code examples.
https://www.accessforums.net/code-repository/
A search of forums could return this thread as easily as a thread in the code repository.
How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.