Unzipping attachment - Run-time error '91' - vba

I found this code on another website. It should take an email found in an Outlook folder and unzip the attachment. It uses a temporary location to do this.
I am using Outlook 2013 and the references I am using are: Visual Basic for Application, Microsoft Outlook 15.0 Object Library, OLE Automation, Microsoft Office 15.0 Object Library. I am running this code currently in a module.
Option Explicit
Sub Unzip1()
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Atchmt As Attachment
Dim FileName As String
Dim msg As Outlook.MailItem
Dim ns As Outlook.NameSpace '
Dim FSO As Object 'variables for unzipping
Dim oApp As Object
Dim FileNameFolder As Variant
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("ASE")
For Each msg In SubFolder.Items
For Each Atchmt In msg.Attachments
If (Right(Atchmt.FileName, 3) = "zip") Then
FileNameFolder = Environ("USERPROFILE") & "Documents\"
Set oApp = CreateObject("Shell.Application")
oApp.NameSpace(FileNameFolder).CopyHere oApp.NameSpace(Atchmt.FileName).Items
End If
Next
Next
End Sub
I am getting an error "Object variable or With block variable not set" on this line.
oApp.NameSpace(FileNameFolder).CopyHere oApp.NameSpace(Atchmt.FileName).Items

I had to change
DIm oAPP as Object
to
Dim oApp as Shell
Early-binding using 'Microsoft Shell Controls and Automation'. Not sure why it was complaining about the late-binding

If we are talking about the Namespace class from the Outlook object mode, it doesn't provide the CopyHere method. The object itself provides methods for logging in and out, accessing storage objects directly by ID, accessing certain special default folders directly, and accessing data sources owned by other users. Use GetNamespace("MAPI") to return the Outlook NameSpace object from the Application object.
The CopyHere method copies an item or items to a folder. The item or items to copy should be passed as a parameter. This can be a string that represents a file name, a FolderItem object, or a FolderItems object. But not the attachment filename. You need to save the attachment on the disk first.

Make sure Sub Folder ("Books") in your Email Inbox Exist & "\Documents\Files\" that's where unzip files will be saved
Option Explicit
Sub Unzip1()
Dim Inbox As MAPIFolder
Dim SubFolder As MAPIFolder
Dim Atchmt As Attachment
Dim FileName As String
Dim msg As Outlook.MailItem
Dim ns As Outlook.NameSpace '
Dim FSO As Object 'variables for unzipping
Dim oApp As Object
Dim FileNameFolder As Variant
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("Books")
For Each msg In SubFolder.Items
For Each Atchmt In msg.Attachments
If (Right(Atchmt.FileName, 3) = "zip") Then
FileNameFolder = Environ("USERPROFILE") & "\Documents\Files\"
Atchmt.SaveAsFile FileNameFolder
Set oApp = CreateObject("Shell.Application")
oApp.NameSpace(FileNameFolder).CopyHere oApp.NameSpace(Atchmt.FileName).Items
End If
Next
Next
End Sub

Related

Save attachments using mail subject in file name

I want to save all attachments from my Outlook 365 inbox.
Copying this tutorial I wrote:
Sub Download_Attachments()
Dim ns As NameSpace
Dim olFolder_Inbox As Folder
Dim olMail As MailItem
Dim olAttachment As Attachment
Dim fso As Object
Dim File_Saved_Folder As String
File_Saved_Folder_Path = "C:\GIS\temp\mails"
Set ns = GetNamespace("MAPI")
Set olFolder_Inbox = ns.GetDefaultFolder(olFolderInbox)
Set fso = CreateObject("Scripting.FileSystemObject")
For Each olMail In olFolder_Inbox.Items
If TypeName(olMail) = "MailItem" And olMail.Attachments.Count > 0 Then
fso.CreateFolder (fso.BuildPath(File_Saved_Folder_Path, Trim(olMail.Subject)))
For Each olAttachment In olMail.Attachments
olAttachment.SaveAsFile fso.BuildPath(File_Saved_Folder_Path, Trim(olMail.Subject)) & "\" & olAttachment.FileName
Next olAttachment
End If
Next olMail
Set olFolder_Inbox = Nothing
Set ns = Nothing
Set fso = Nothing
End Sub
When I execute the macro I get roughly (translated from Swedish):
Error 76, Cant find the path
The Subject property of the MailItem class and the FileName property of the Attachment class may contain forbidden symbols that can't be used for filenames. So. before calling the SaveAsFile method of the Attachment class you need to check the file path whether such folder exists and the path doesn't contain forbidden symbols. See What characters are forbidden in Windows and Linux directory names? for more information.

VBA accessing subfolder in Outlook shared Mailbox

I am having an issue with accessing a subfolder from a shared Outlook email box using VBA. The goal of this code is to download attachments from emails located in a subfolder called "Example_Subfolder". The code below results in an error message; "Run-time error '-2147221233 (8004010f)': The attempted operation failed. An object could not be found.".
Sub foo()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim destFolder As Outlook.MAPIFolder
Dim srcFolder As Outlook.MAPIFolder
Dim olItem As Object
Dim subFolder As Object
Dim mailitem As Outlook.mailitem
Dim olAtt As Outlook.Attachment
Dim objOwner As Outlook.Recipient
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
'set object to shared email inbox
Set objOwner = olNS.CreateRecipient("Shared_Mailbox#companyname.com")
objOwner.Resolve
'check object resolved
If Not objOwner.Resolved Then
Debug.Print objOwner.Name
MsgBox "Failed to connect to shared email. Contact XXX."
End If
Set olFolder = olNS.GetSharedDefaultFolder(objOwner, olFolderInbox)
'error on next line.
Set subFolder = olFolder.Folders("Example_Subfolder")
'download email attachments
'etc
'etc
End Sub
The only way I've been able to access the emails inside "Example_Subfolder" is by using Set subFolder = olNS.PickFolder. I would rather not use this method in my macro. Can anyone point me in the right direction as to why my code doesn't work?
Given the folder is visible in the navigation pane there is an alternative.
Sub foo()
Dim olNS As namespace
Dim olMailbox As Folder
Dim olInbox As Folder
Dim subFolder As Folder
Set olNS = GetNamespace("MAPI")
' If the folder is in the navigation pane
Set olMailbox = olNS.Folders("Shared_Mailbox#companyname.com")
Set olInbox = olMailbox.Folders("Inbox")
Set subFolder = olInbox.Folders("Example_Subfolder")
subFolder.Display
End Sub

Save outlook email to my internal drive as .msg file

I'm trying to save Outlook emails into my H:Drive. I want it as a run a script rule but I can't get it to work. There are no attachments involved and all I need it is to save it as a .msg file. Please lmk if you find a different way to tackle this problem.
Thanks
Sub ExtractEmailToFolder2(itm As Outlook.MailItem)
Dim OlApp As Outlook.Application
Set OlApp = New Outlook.Application
Dim Mailobject As Object
Dim Email As String
Dim NS As NameSpace
Dim Folder As MAPIFolder
Set OlApp = CreateObject("Outlook.Application")
Dim fso As Object
Dim fldrname As String
Dim fldrpath As String
' Setup Namespace
Set NS = ThisOutlookSession.Session
' Display select folder dialog
Set Folder = NS.PickFolder
' Create Folder File
Set fso = CreateObject("Scripting.FileSystemObject")
' loop to read email address from mail items.
For Each Mailobject In Folder.Items
fldrpath = "H:\Backup stuff\"
If Not fso.folderexists(fldrpath) Then
fso.createfolder (fldrpath)
End If
Set objCopy = Mailobject.Copy
objCopy.SaveAs fldrpath & "\" & objCopy.Subject, olMSG
Next
Set OlApp = Nothing
Set Mailobject = Nothing
End Sub
First of all, there is no need to create a new Outlook Application instance (twice in your sample code!) if your VBA macro is run by the rule. Instead, you can use the global Application property:
Sub ExtractEmailToFolder2(itm As Outlook.MailItem)
Dim fso As Object
Dim fldrname As String
Dim fldrpath As String
' Create Folder if required
Set fso = CreateObject("Scripting.FileSystemObject")
fldrpath = "H:\Backup stuff\"
If Not fso.folderexists(fldrpath) Then
fso.createfolder (fldrpath)
End If
itm.SaveAs fldrpath & "\" & "your_unique_filename.msg", olMSG
Set OlApp = Nothing
Set Mailobject = Nothing
End Sub
The sample code which is shown above saves the item against which the rule is run to the folder specified/hardcoded.
Problem:
Folder Check was included in the Loop
FileName had Subject in it. That always creates problem unless some kind of manipulation is done. Because it contains various characters that are not permitted in the Name of a File in Windows.
Note:
Put it in any Module in Outlook and Run using F5 or by Creating a Shortcut.
Try:
Sub ExtractEmailToFolder2()
Dim OlApp As Outlook.Application
Set OlApp = New Outlook.Application
Dim Mailobject As Object
Dim Email As String
Dim NS As NameSpace
Dim Folder As MAPIFolder
Set OlApp = CreateObject("Outlook.Application")
Dim fso As Object
Dim fldrname As String
Dim fldrpath As String
' Setup Namespace
Set NS = ThisOutlookSession.Session
' Display select folder dialog
Set Folder = NS.PickFolder
' Create Folder File
Set fso = CreateObject("Scripting.FileSystemObject")
fldrpath = "H:\Backup stuff\"
If Not fso.folderexists(fldrpath) Then
fso.createfolder (fldrpath)
End If
' loop to read email address from mail items.
i = 1
For Each Mailobject In Folder.Items
Mailobject.SaveAs fldrpath & "\mail" & i & ".msg", olMsg
i = i + 1
Next
Set OlApp = Nothing
Set Mailobject = Nothing
End Sub

Search the mails containing one email id in all inbox subfolders

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
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.

How to create a top-level folder in my Outlook using vb.net - VB.NET, Outlook 2013

So as the title says, I am trying to create a top-level folder in my Outlook but I haven't got any success with it. I've read several tutorials and code snippets but non of them seem to be a success.
So now i Have this piece of code which creates a folder under the Inbox folder:
Dim objFolder As Outlook.MAPIFolder
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
objFolder.Folders.Add("Some folder", Outlook.OlDefaultFolders.olFolderInbox)
The question is, how can I create the same folder but then as a top-level folder instead as a sub-folder of the inbox folder.
I already tried to do it like this:
objFolder.Folders.Add("Some folder") but this didn't work.
Top folders (root nodes in the navigation pane) are store. If you need to add a new store in the profile you can use the AddStoreEx method of the Namesapace class which adds a Personal Folders file (.pst) in the specified format to the current profile. See How to: Add or Remove a Store for more information.
In case if you need to create a top-level folder (at the same level with standard folders like Inbox and etc.) you can get the Parent folder object of the Inbox or any other default folder and add a new folder there. For example:
Dim objFolder As Outlook.MAPIFolder
Dim parentFolder as Outlook.MAPIFolder
Dim objOutlook As Outlook._Application
objOutlook = New Outlook.Application()
myNamespace = objOutlook.GetNamespace("MAPI")
objFolder = myNamespace.GetDefaultFolder(olFolderInbox)
parentFolder = objFolder.Parent
parentFolder.Folders.Add("Some folder", Outlook.OlDefaultFolders.olFolderInbox)
Also you may find the GetRootFolder method of the Store class helpful. It returns a Folder object representing the root-level folder of the Store. You can use the GetRootFolder method to enumerate the subfolders of the root folder of the Store. Unlike NameSpace.Folders which contains all folders for all stores in the current profile, Store.GetRootFolder.Folders allows you to enumerate all folders for a given Store object in the current profile.
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
Private Sub CreateNewFolder()
Dim oApp As Outlook.Application = New Outlook.Application
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim InboxFolder As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim customFolder As Outlook.MAPIFolder
Try
customFolder = InboxFolder.Folders.Add("Vellaichamy", Outlook _
.OlDefaultFolders.olFolderInbox)
InboxFolder.Folders("Authorcode").Display()
Catch ex As Exception
MessageBox.Show("The following error occurred: " & ex.Message)
End Try
End Sub