Custom user properties field in Search folder view - vba

I created a custom user property field. I am able to add this field in all folders, except a Search folder.
Sub AddStatusProperties()
Dim objNamespace As NameSpace
Dim objFolder As Folder
Dim objProperty As UserDefinedProperty
Dim oNameSpace As Object 'Outlook.Namespace
Dim oStores As Object 'Outlook.Stores
Dim oStore As Object 'Outlook.Store
Dim oFolder As Object 'Outlook.folder
Set objNamespace = Application.GetNamespace("MAPI")
For Each objFolder In objNamespace.Folders
With objFolder.UserDefinedProperties
Set objProperty = .Add("MyNotes1", olText, True)
End With
Next
End Sub
This is the error message:
"Run-time error 440: you don't have appropriate permission to perform this operation"
when I added below line.
Set oFolder = Session.Stores.Item("xxxxx#xxx.com").GetSearchFolders("Inbox 2")
With oFolder.UserDefinedProperties
Set objProperty = .Add("MyNotes1", olText, True)
End With
Do you know what I am doing wrong or if there is a workaround?

This is to be expected - folder properties are stored in a hidden (associated) message in the folder. But search folders cannot contains any messages - they only point to the search results from other folders, a message cannot be created there.

Related

I mistakenly hid my calendar how to recover

I ran the following script to hide folders in outlook and I mistakenly hide my calendar
Option Explicit
Public Sub HideFolders()
Dim oFolder As Outlook.Folder
Dim oPA As Outlook.PropertyAccessor
Dim PropName, Value, FolderType As String
PropName = "http://schemas.microsoft.com/mapi/proptag/0x10F4000B"
Value = True
Set oFolder = Application.ActiveExplorer.CurrentFolder
Set oPA = oFolder.PropertyAccessor
oPA.SetProperty PropName, Value
Set oFolder = Nothing
Set oPA = Nothing
End Sub
I then tried the following script to recover my outlook calendar but it did not work
Option Explicit
Public Sub FindFolders()
Dim oFolder As Outlook.Folder
Dim oPA As Outlook.PropertyAccessor
Dim PropName, Value, FolderType As String
PropName = "http://schemas.microsoft.com/mapi/proptag/0x10F4000B"
Value = False
Set oFolder = Session.GetDefaultFolder(olFolderCalendar)
Set oPA = oFolder.PropertyAccessor
oPA.SetProperty PropName, Value
Set oFolder = Nothing
Set oPA = Nothing
End Sub
any assistance will be greatly appreciated
You can fix a problem like that without a script using OutlookSpy (I am its author) - click IMsgStore button, click "Open Folder", select the folder to open, double click the PR_ATTR_HIDDEN property to edit.

save PDF attachment with filename &domain name

I would like to run a macro to do follow steps:
- save PDF only attachment to hard drive
- save it with a revise name filename & domain name.
Here is the code I search from open source and mix it together. any help is appreciated. thanks
Public Sub Download_Attachments()
Dim ns As NameSpace
Dim olFolder_Inbox As Folder
Dim olMail As MailItem
Dim olAttachment As Attachment
Dim strFolderPath As String
Dim strFileName As String
Dim strSenderAddress As String
Dim strSenderDomain As String
Dim fso As Object
strFolderPath = "C:\"
Set ns = GetNamespace("MAPI")
Set fso = CreateObject("Scripting.FileSystemObject")
Set olFolder_Inbox = Application.ActiveExplorer.CurrentFolder
Set olMail = Application.ActiveWindow.CurrentItem
'Get sender domain
strSenderAddress = olMail.SenderEmailAddress
strSenderDomain = Right(strSenderAddress, Len(strSenderAddress) - InStr(strSenderAddress, "#"))
For Each olMail In olFolder_Inbox.Items
If TypeName(olMail) = "MailItem" And olMail.Attachments.Count > 0 Then
For Each olAttachment In olMail.Attachments
Select Case UCase(fso.GetExtensionName(olAttachment.FileName))
Case "PDF", "pdf"
olAttachment.SaveAsFile strFolderPath & strFileName
Case Else
'skip
End Select
Next olAttachment
End If
Next olMail
Set olFolder_Inbox = Nothing
Set fso = Nothing
Set ns = Nothing
End Sub
The following line of code retrieves the active folder in the Explorer window, not the Inbox one. Outlook can be started with any active folder, you can specify the folder name to the Outlook.exe file. To get the default folders (Inbox) you need to use the NameSpace.GetDefaultFolder method which returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Calendar folder for the user who is currently logged on. For example, the following sample code uses the CurrentFolder property to change the displayed folder to the user's default Inbox folder.
Sub ChangeCurrentFolder()
Dim myNamespace As Outlook.NameSpace
Set myNamespace = Application.GetNamespace("MAPI")
Set Application.ActiveExplorer.CurrentFolder = myNamespace.GetDefaultFolder(olFolderInbox)
End Sub
Then it is not really recommended to iterate over all items in the folder.
For Each olMail In olFolder_Inbox.Items
Instead, you need to use the Find/FindNext or Restrict methods of the Items class to get only items that correspond to your conditions. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder
Finally, the part you are interested in is the SaveAsFile method of the Attachment class which saves the attachment to the specified path:
olAttachment.SaveAsFile strFolderPath & domainName & strFileName
Make sure a qualified file path is passed as a parameter. I'd recommend running the code under the debugger and see what values are passed.

Is it possible to select a folder based on it's name rather then on it's path? VBA - Outlook

Recently I am trying to create a function which selects specific folders.
I tested this at one user and it works. The problem however is that I also want to use this function at other users but not having to rewrite the function based on their folder structure.
The function works with a few folder names which all of them have (same names).
It consists of 1 main folder: #MemoScan and 4 sub-folders.
Based on these folder names I want to count how many mail items are in them.
I created the following function to do this:
Function HowManyEmails() As Integer
Dim objOutlook As Object, objnSpace As Object, MyCurrentFolder As MAPIFolder
Dim EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
Set MyCurrentFolder = objnSpace.folders("William").folders("#MemoScan")
sFolder = MyCurrentFolder
For Each Folder In MyCurrentFolder.folders
sFolder = Folder
sSubmap = Right(sFolder, Len(sFolder) - 1)
For Each Item In Folder.Items
If TypeName(Item) = "MailItem" Then
EmailCount = EmailCount + 1
End If
Next Item
Next Folder
HowManyEmails = EmailCount
End Function
As you can see the folder that needs to be checked is hard-coded (needs to be since it runs on a close outlook event and nothing is selected).
The path now is: objnSpace.folders("William").folders("#MemoScan")
The thing is however that the main account/folder William wont be there at other users. My question is, how can I adjust it in such way that it will just look for the #MemoScan folder which is of the same at every user? Is this even possible?
If I leave the main William namespace out then it won't be able to find the #MemoScan folder.
The folder structure at this particular user is as follows:
The Namespace class provides the Stores property which returns a Stores collection object that represents all the Store objects in the current profile. The Store class provides the GetRootFolder method which 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
End Sub
Also you can run the code against the currently selected folder in Outlook. The CurrentFolder property of the Explorer class returns a Folder object that represents the current folder displayed in the explorer.

Outlook VBA 2013 Access Parent Folders

Okay, I was reading a tutorial on how to access parent folders outside of the Inbox and noticed that it was using "Set". To my knowledge, this command is deprecated and seems so whenever I have tried to use it in my code.
http://blogs.technet.com/b/heyscriptingguy/archive/2006/08/03/how-can-i-get-access-to-a-mail-folder-that-isn-t-a-subfolder-of-my-outlook-inbox.aspx
' Set Outlook parameters
objOutlook = CreateObject("Outlook.Application")
iNameSpace = myOlApp.GetNamespace("MAPI") ' Set current NameSpace
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection
oExp = objOutlook.ActiveExplorer
oSel = oExp.Selection
Dim strFolderName As Object
Dim objInbox As Outlook.Folder
Dim objMailbox As Outlook.Folder
Dim objFolder As Outlook.Folder
Const olFolderInbox = 6
objInbox = iNameSpace.GetDefaultFolder(olFolderInbox)
strFolderName = objInbox.Folders.Parent()
objMailbox = iNameSpace.Folders(strFolderName)
objFolder = objMailbox.Folders("Europe")
When trying the code above, I get a type error: Additional information: Type mismatch, on this line:
objMailbox = iNameSpace.Folders(strFolderName)
When I change this to an "Object", I get the same error.
Any Idea what I am doing wrong?
To access the parent of the Inbox folder, try iNameSpace.GetDefaultFolder(olFolderInbox).Parent
To access a folder on the same level as the Inbox, try iNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders.Item("The Folder name")
The following code will not work.
strFolderName = objInbox.Folders.Parent()
The Folders collection does't provide the Parent method.
objMailbox = iNameSpace.Folders(strFolderName)
The Folders property doesn't accept an object arguments.

Outlook 2010 custom VBA script to move incoming mail message to a specific folder

I'm trying to create a custom rule for Outlook 2010 that inspect the subject of the email and if it makes a regular expression it's moved into a specific folder.
However when I run the script I get the following error when I try and get an Outlook.Folder object for the folder I want to move the message to:
Run-time error '91':
Object variable or With block variable not set
Below is the VBA script that I am using to check the email subject and move the message to the specified folder if it matches.
Sub MoveToETS(Item As Outlook.MailItem)
Dim Subject As String
Subject = Item.Subject
Dim FolderToMoveTo As Outlook.Folder
Set FolderToMoveTo = GetFolder("ETS")
If (CheckSubject(Subject, "^[Project|Bug] (\d+?) - \[[UPDATE|NEW|RESOLVED]\]")) Then
Item.Move (FolderToMoveTo)
End If
End Sub
Function CheckSubject(Subject As String, PatternToCheck As String)
Dim ObjRegExp As RegExp
Dim ObjMatch As Match
Set ObjRegExp = New RegExp
ObjRegExp.Pattern = PatternToCheck
If (ObjRegExp.Text(Subject) = True) Then
CheckSubject = True
End If
End Function
Function GetFolder(ByVal FolderName As String) As Outlook.Folder
Dim ObjFolder As Outlook.Folder
Set ObjFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("ETS")
GetFolder = ObjFolder
End Function
Your last but one line needs to be
Set GetFolder = ObjFolder
In your GetFolder function, you also hard-coded the folder name.
Line reads:
Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("ETS")
Should read:
Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders(FolderName)