Link to home
Start Free TrialLog in
Avatar of DigitBoy
DigitBoyFlag for Netherlands

asked on

get outlook folder IDs

Dear all,


is it possible to select a folder in outlook and to export with all subfolders their IDs to a txt file using VB script?
Also from Public folders in outlook.

K rgs
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Don't know about public folders ... but select a folder in the outlook client and run getIDs from the below code.  Modify the line:

    Set txtFile = fso.CreateTextFile(("C:\deleteme\folderIDs.txt"), True)

to point to the file you want to contain the output ... I have added the folder path and some additional characters but if you do want just the ids then delete the surplus.

Sub getIDs()
Dim fso
Dim txtFile

    Set fso = CreateObject("scripting.filesystemobject")
    Set txtFile = fso.CreateTextFile(("C:\deleteme\folderIDs.txt"), True)
    writeids txtFile, Application.ActiveExplorer.CurrentFolder
    txtFile.Close

End Sub

Sub writeids(txtFile, fldr)
Dim subfolder

    txtFile.Write fldr.FolderPath & "    <<< " & fldr.EntryID & " >>>" & vbCrLf
    For Each subfolder In fldr.Folders
        writeids txtFile, subfolder
    Next

End Sub

Open in new window


Chris
Avatar of DigitBoy

ASKER

Hi Chris,

the code doesn't work.
No error message.

K rgs
I want to select a folder and export the entryID's.
Did you set the DOS folder path to a valid folder?
yes I did
Thecode was tested . Can you step through and see what happens?
I've seleced a outlook folder and ran your script. The output folder is also fine.
Sorry My mistake. this is a vba code.
Is there a way in vbs please? Because this was our orginal question.
Bother!

I knew that and though I tested in Outlook I tried to make it VBS but missed a critical bit!

Dim fso
Dim txtFile

    Set fso = CreateObject("scripting.filesystemobject")
    Set txtFile = fso.CreateTextFile(("C:\deleteme\folderIDs.txt"), True)
    writeids txtFile, createobject("outlook.Application").ActiveExplorer.CurrentFolder
    txtFile.Close

Sub writeids(txtFile, fldr)
Dim subfolder

    txtFile.Write fldr.FolderPath & "    <<< " & fldr.EntryID & " >>>" & vbCrLf
    For Each subfolder In fldr.Folders
        writeids txtFile, subfolder
    Next

End Sub

Open in new window


Chris
Tested as a VBS this time rather than tried to be clever ;o)

Chris
Hi Chris,

It works :D. Really great.

I have just a small question, is there a possibility to use inputbox "Set txtFile = fso.CreateTextFile(("C:\deleteme\folderIDs.txt"), True)" instead of hardcoded please?

K rgs
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmmm
That took a bit of finding, (fell on a function from Rob van der Woude that seems to do the job).  This is based on the pickfolder change I posted last:

Dim fso
Dim txtFile
Dim strFile

    Set fso = CreateObject("scripting.filesystemobject")
'    Set txtFile = fso.CreateTextFile(("C:\deleteme\folderIDs.txt"), True)
    strFile = GetFileName( "", "Text files|*.txt" )
'    If not fso.FileExists(strFile) Then
'    	MsgBox "OOPS File NOT found"
'    	WScript.quit
'    End If
    Set txtFile = fso.OpenTextFile(strFile, 2, true)
    With createobject("outlook.Application")
    	writeids txtFile, .session.pickfolder
    End With
    txtFile.Close

Sub writeids(txtFile, fldr)
Dim subfolder

    txtFile.Write fldr.FolderPath & "    <<< " & fldr.EntryID & " >>>" & vbCrLf
    For Each subfolder In fldr.Folders
        writeids txtFile, subfolder
    Next

End Sub

Function GetFileName( myDir, myFilter )
' http://www.robvanderwoude.com/vbstech_ui_fileopen.php
' This function opens a File Open Dialog and returns the
' fully qualified path of the selected file as a string.
'
' Arguments:
' myDir is the initial directory; if no directory is
' specified "My Documents" is used;
' NOTE: this default requires the WScript.Shell
' object, and works only in WSH, not in HTAs!
' myFilter is the file type filter; format "File type description|*.ext"
' ALL arguments MUST get A value (use "" for defaults), OR otherwise you must
' use "On Error Resume Next" to prevent error messages.
'
' Dependencies:
' Requires NUSRMGRLib (nusrmgr.cpl), available in Windows XP and later.
' To use the default "My Documents" WScript.Shell is used, which isn't
' available in HTAs.
'
' Written by Rob van der Woude
' http://www.robvanderwoude.com

    ' Standard housekeeping
    Dim objDialog

    ' Create a dialog object
    Set objDialog = CreateObject( "UserAccounts.CommonDialog" )

    ' Check arguments and use defaults when necessary
    If myDir = "" Then
        ' Default initial folder is "My Documents"
        objDialog.InitialDir = CreateObject( "WScript.Shell" ).SpecialFolders( "MyDocuments" )
    Else
        ' Use the specified initial folder
        objDialog.InitialDir = myDir
    End If
    If myFilter = "" Then
        ' Default file filter is "All files"
        objDialog.Filter = "All files|*.*"
    Else
        ' Use the specified file filter
        objDialog.Filter = myFilter
    End If

    ' Open the dialog and return the selected file name
    If objDialog.ShowOpen Then
        GetFileName = objDialog.FileName
    Else
        GetFileName = ""
    End If
End Function

Open in new window


Chris
Hi Chris,

I have just one small issue and that is I have to rename each time the exported txt file. Is there way to create each time a new txt file please?

K rgs
You rename the file in the dialog as required.

Chris
Hi Chris,

thanks for you quick response.

I have an error "Set objDialog = CreateObject( "UserAccounts.CommonDialog" )" cannot create object.

K rgs
it's xp and works there ... which os do you have?
I have win7
very nice coding skills.