More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

forum 18-Dec-2018 14:28

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

    Page [ 1 2 3 4 5 6 7 Next >> ]  
 Basic Outlook Printer Friendly Version
Working with Outlook items, folders, recipients; dealing with security; writing event handlers
Topic
Using VBScript to move items in a Public Folder to another Public Folder
I am attempting to write a VBScript to clean up a Public Folder after another script has extracted the information out of the items stored in it (and put it into an Access database). I'd like to have this new script move or copy all the messages/notes in the Public Folder into a subfolder. I am unsure how to do this. Can anyone help?

  04-Dec-2007  23:54
  05-Dec-2007  09:11   
Have you looked at the MailItem.Move and Copy methods?
  05-Dec-2007  19:02   
I'm having problems getting my script to even acknowledge that the Public Folders exist, let alone move or copy the items contained therein! :(

What I have thus far is (curtesy of the "Scripting Guys", I must acknowledge):

=================
'Now we try and access the public folder we want.

Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)

strFolderName = objInbox.Parent

Set objMailbox = objNamespace.Folders(strFolderName)

Set objFolder = objMailbox.Folders("Public Folders\All Public Folders\Sys Admins\Server_Change_Reports")

Set colItems = objFolder.Items

For Each objItem in colItems
    Wscript.Echo objItem.Subject
Next
===========================

I can get this script to read any of the folders in my own inbox but nothing else. I suspect I'm barking up the wrong tree.
  05-Dec-2007  20:56   
To get a non-default folder, you need to walk the folder hierarchy using the Folders collections or use a function that does that for you. See http://www.outlookcode.com/d/code/getfolder.htm and, especially for public folders, http://www.outlookcode.com/codedetail.aspx?id=1164
 
  05-Dec-2007  21:07   
Thanks. Thats cool. My code (if I use Simon's code) should look like this?

=========================
' GetFolder - Gets a Public folder based on a string path - e.g.
'If Folder name in English is
'Public Folders\All Public Folders\Europeen Workflow
'Then just paste in "European Workflow'

strFolderPath = "Server_Change_Reports"

Public Function GetPublicFolder(strFolderPath)
   
    Dim colFolders
    Dim objFolder
    Dim arrFolders
    Dim i
    On Error Resume Next
    strFolderPath = Replace(strFolderPath, "/", "\")
    arrFolders = Split(strFolderPath, "\")
    
    Set objFolder = Application.Session.GetDefaultFolder(18)
    Set objFolder = objFolder.Folders.Item(arrFolders(0))
    If Not objFolder Is Nothing Then
        For i = 1 To UBound(arrFolders)
            Set colFolders = objFolder.Folders
            Set objFolder = Nothing
            Set objFolder = colFolders.Item(arrFolders(i))
            If objFolder Is Nothing Then
                Exit For
            End If
        Next
    End If
    Set GetPublicFolder = objFolder
    Set colFolders = Nothing
    Set objApp = Nothing
    Set objFolder = Nothing
End Function

Wscript.Echo GetPublicFolder
=============================

I've added the first and last lines. The first to give a value to strFolderPath and the second to display what the function GetPublicFolder is. Will that work? I am but a neophyte at VBScript at the moment but improving (I hope!).
  05-Dec-2007  21:08   
Ooops, sorry, forgot to add, it doesn't display to the screen any value. I can't figure out what I'm doing wrong. :(
  05-Dec-2007  22:24   
You need to call GetPublicFolder with the string path and return a string property of the folder for Wscript to Echo, e.g.:

strFolder = "Sys Admins\Server_Change_Reports"
Wscript.Echo GetPublicFolder(strFolderPath).Name
 
  05-Dec-2007  22:54   
I'm not sure where those lines of code go. Do they go at the end? I'm not sure where the variable "strFolder" is used and I'm unsure why the second command produces an error:- "Object required: 'GetPublicFolder(...)'

I thought the function GetPublicFolder supplied that?

I'm getting lost here. Sorry.
  05-Dec-2007  23:41   
They go at the beginning, which would be the "main" portion of a standalone script. If GetPublicFolder() doesn't return an object, you'll get an error. You can simply ignore it by adding an On Error Resume Next statement or more elegantly, test for the returned folder:

On Error Resume Next
strFolderPath = "Sys Admins\Server_Change_Reports"
Set myFolder = GetPublicFolder(strFolderPath)
Wscript.Echo myFolder.Name

Note that the variable called as the GetPublicFolder argument needs to actually exist! It didn't in my earlier example. Sorry about that.
  06-Dec-2007  00:09   
Thanks. I'm not sure if thats working or not as I get nothing echoed to the screen.

If I leave the "On Error" statement off, I get the error:- "Object required: 'myFolder'. Does that mean tha the value of myFolder is empty or that the function hasn't passed a value to it?
  06-Dec-2007  08:04   
Sorry, I forgot to put in the If:

On Error Resume Next
strFolderPath = "Sys Admins\Server_Change_Reports"
Set myFolder = GetPublicFolder(strFolderPath)
If Not myFolder Is Nothing Then
    Wscript.Echo myFolder.Name
Else
    Wscript.Echo "no folder found"
End If

Do not omit the On Error Resume Next statement. It allows code execution to flow to the If block even if GetPublicFolder returns Nothing because the function couldn't find the folder.
    Page [ 1 2 3 4 5 6 7 Next >> ]