Check if in principal root of outlook...

User avatar
sal21
PlatinumLounger
Posts: 4417
Joined: 26 Apr 2010, 17:36

Check if in principal root of outlook...

Post by sal21 »

How to check if folder exists in Outlook, create it if not exists... Via vba for excel. Tks.
But use code also if outlook not is just opened from user.

User avatar
HansV
Administrator
Posts: 78857
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Chek if in pricipal root of outlook...

Post by HansV »

Do you want to create a folder immediately below the user's mailbox?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4417
Joined: 26 Apr 2010, 17:36

Re: Chek if in pricipal root of outlook...

Post by sal21 »

HansV wrote:Do you want to create a folder immediately below the user's mailbox?
Yes, you undestand me, as usual :laugh:

User avatar
HansV
Administrator
Posts: 78857
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Check if in pricipal root of outlook...

Post by HansV »

Try this macro:

Code: Select all

Sub CheckOutlookFolder()
    Const strFolder = "Sal21" ' change as needed
    Dim olApp As Object
    Dim olNsp As Object
    Dim olTop As Object
    Dim olFld As Object
    Set olApp = CreateObject(Class:="Outlook.Application")
    Set olNsp = olApp.GetNamespace("MAPI")
    Set olTop = olNsp.GetDefaultFolder(6).Parent ' 6 = olFolderInbox
    On Error Resume Next
    Set olFld = olTop.Folders(strFolder)
    On Error GoTo 0
    If olFld Is Nothing Then
        Set olFld = olTop.Folders.Add(strFolder)
    End If
    ' Optional: quit Outlook
    olApp.Quit
End Sub
The code uses late binding, you don't have to set a reference to the Outlook object library.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4417
Joined: 26 Apr 2010, 17:36

Re: Check if in pricipal root of outlook...

Post by sal21 »

HansV wrote:Try this macro:

Code: Select all

Sub CheckOutlookFolder()
    Const strFolder = "Sal21" ' change as needed
    Dim olApp As Object
    Dim olNsp As Object
    Dim olTop As Object
    Dim olFld As Object
    Set olApp = CreateObject(Class:="Outlook.Application")
    Set olNsp = olApp.GetNamespace("MAPI")
    Set olTop = olNsp.GetDefaultFolder(6).Parent ' 6 = olFolderInbox
    On Error Resume Next
    Set olFld = olTop.Folders(strFolder)
    On Error GoTo 0
    If olFld Is Nothing Then
        Set olFld = olTop.Folders.Add(strFolder)
    End If
    ' Optional: quit Outlook
    olApp.Quit
End Sub
The code uses late binding, you don't have to set a reference to the Outlook object library.
tks!
wowrk perfect!