0
votes

To find all emails (by folder name i.e. email id) from subfolders of inbox present since last month and copy paste them in respective folders by their subject.

I am stuck at code NS = OlApp.GetNamespace("MAPIFolder") also with ("MAPI") as well the error of object variable not set is displaying

Dim OlApp As Outlook.Application
Set OlApp = New Outlook.Application
Set OlApp = CreateObject("Outlook.Application")
Dim fldrpath As String
Dim fldername As String
Dim oMail As Object
fldrpath = "\data\EMAILS\" & fldrname
Dim NS As Namespace
Dim Folder As MAPIFolder
Dim sName As String
Dim dtdate As String
Dim Inbox As MAPIFolder
NS = OlApp.GetNamespace("MAPIFolder")
Set Inbox = NS.GetDefaultFolder(olFolderInbox)
For Each mysubFolders In Inbox.subFolders
Set mysubfolder = Inbox.subFolders("PDI").Folders("OBU").Folders("DND")
For Each mailItems In mysubfolder
If oMail.Body = r Then
Set mailItems = oMail
sName = mailItems.Subject
dtdate = mailItems.ReceivedTime
Debug.Print fldrpath & sName
mailItems.SaveAs fldrpath & sName, olMSG
End If
 Next
Set OlApp = Nothing
Set mailItems = Nothing
Next
End Sub
1
Should be pretty simple. Update your question with the current state of your code so we know where to start.David G
I am happy to listen that its simple but currently I am stuck on this...please help. codes are updated in questionsapana
Good update! Now tell us exactly the line where you are stuck at, why you are stuck and what you have tried to fix it.David G
I am stuck at code NS = OlApp.GetNamespace("MAPI Folder"), gving me error as object variable or block no setsapana
Update your question and title with ^ this question and break down your code to a minimal example of your problem.David G

1 Answers

1
votes

The first problem is this line, missing the Set keyword:

Set NS = OlApp.GetNamespace("MAPIFolder")

Then you are accessing "Inbox.subFolders", but there is no such property on a Folder object; it would be the Folders property collection that you want.

You also aren't using the mysubFolders variable in the loop, so that entire block of code is going to fail. There is also no explicit set of the mailItems object you are trying to iterate from the mysubfolder object.

I'd continue but frankly the entire method needs to be rewritten. Focus on ensuring you are declaring and setting the correct variables to the correct properties or objects.