R
ram1
I am probably making this way more complicated than it needs to be. I know
little about vba.
I am trying to piece together snippets of code to get the result.
In a nutshell, I have created a custom view and assigned it to a particular
public folder.
I want to be able to create a macro that a user can run, to copy that view in
the public folder into their outlook and let the view be used for all mail
folders.
The name of the Public Folder is "Public Folders\All Public Folders\
VorysViews"
The ViewDriver sub and CreateView Function work if I use the original
Set objViews = objName.GetDefaultFolder(FolderType:=cnstFolderName).Views
but not with what I have been trying
Set objViews = GetFolder("Public Folders\All Public Folders\VorysViews").
Views
I have been unsuccessful getting objViews to be the views in "Public Folders\
All Public Folders\VorysViews"
The other Function "GetFolder" is where I went off on a tangent because I
don't think I need to return just the final folder name.
I just need to copy the views from "Public Folders\All Public Folders\
VorysViews" and then save the view for all mail and post folders.
I'm sure I'm overlooking the obvious. Leading me in the right direction
would be really helpful.
I bought "Microsoft Outlook Programming" but don't have the 2007 version yet.
Thanks
Sub ViewDriver()
'Calls the CreateView function to return a view.
Dim objView As View
Set objView = CreateView(strName:="Main Inbox Table_bymacro",
cnstFolderName:=olFolderInbox, cnstViewType:=olTableView)
'MsgBox "cnstFolderName is " & cnstFolderName
If objView Is Nothing Then
MsgBox "The view was not created."
'MsgBox "The view: " & objFolder.Name & " is failing badly."
'MsgBox "objview is " & objView.Name
Else
MsgBox "The view: " & objView.Name & " was created sucessfully."
End If
End Sub
Function CreateView(ByVal strName As String, ByVal cnstFolderName As
OlDefaultFolders, ByVal cnstViewType As OlViewType) As View
'If succesful, returns a new view.
Dim olApp As Outlook.Application
Dim objName As NameSpace
Dim objViews As Views
MsgBox "cnstFolderName is " & cnstFolderName
On Error Resume Next
Set olApp = Outlook.Application
Set objName = olApp.GetNamespace(Type:="MAPI")
'Set objViews = objName.GetDefaultFolder(FolderType:=cnstFolderName).
Views
Set objViews = GetFolder("Public Folders\All Public Folders\VorysViews").
Views
'Set objFolder = GetFolder("Public Folders\All Public Folders\Company\
Sales")
'Create the new view.
Set CreateView = objViews.Add(Name:=strName, ViewType:=cnstViewType,
SaveOption:=olViewSaveOptionAllFoldersOfType)
End Function
'SaveOption:=olViewSaveOptionAllFoldersOfType)
'The above SaveOption will save the view to all folders
of this type
Public Function GetFolder(strFolderPath As String) As MAPIFolder
' strFolderPath needs to be something like
' "Public Folders\All Public Folders\Company\Sales" or
' "Personal Folders\Inbox\My Folder"
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = Application
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.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 GetFolder = objFolder
MsgBox "GetFolder is " & objFolder.Name
MsgBox "objFolder is " & GetFolder.Name
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function
little about vba.
I am trying to piece together snippets of code to get the result.
In a nutshell, I have created a custom view and assigned it to a particular
public folder.
I want to be able to create a macro that a user can run, to copy that view in
the public folder into their outlook and let the view be used for all mail
folders.
The name of the Public Folder is "Public Folders\All Public Folders\
VorysViews"
The ViewDriver sub and CreateView Function work if I use the original
Set objViews = objName.GetDefaultFolder(FolderType:=cnstFolderName).Views
but not with what I have been trying
Set objViews = GetFolder("Public Folders\All Public Folders\VorysViews").
Views
I have been unsuccessful getting objViews to be the views in "Public Folders\
All Public Folders\VorysViews"
The other Function "GetFolder" is where I went off on a tangent because I
don't think I need to return just the final folder name.
I just need to copy the views from "Public Folders\All Public Folders\
VorysViews" and then save the view for all mail and post folders.
I'm sure I'm overlooking the obvious. Leading me in the right direction
would be really helpful.
I bought "Microsoft Outlook Programming" but don't have the 2007 version yet.
Thanks
Sub ViewDriver()
'Calls the CreateView function to return a view.
Dim objView As View
Set objView = CreateView(strName:="Main Inbox Table_bymacro",
cnstFolderName:=olFolderInbox, cnstViewType:=olTableView)
'MsgBox "cnstFolderName is " & cnstFolderName
If objView Is Nothing Then
MsgBox "The view was not created."
'MsgBox "The view: " & objFolder.Name & " is failing badly."
'MsgBox "objview is " & objView.Name
Else
MsgBox "The view: " & objView.Name & " was created sucessfully."
End If
End Sub
Function CreateView(ByVal strName As String, ByVal cnstFolderName As
OlDefaultFolders, ByVal cnstViewType As OlViewType) As View
'If succesful, returns a new view.
Dim olApp As Outlook.Application
Dim objName As NameSpace
Dim objViews As Views
MsgBox "cnstFolderName is " & cnstFolderName
On Error Resume Next
Set olApp = Outlook.Application
Set objName = olApp.GetNamespace(Type:="MAPI")
'Set objViews = objName.GetDefaultFolder(FolderType:=cnstFolderName).
Views
Set objViews = GetFolder("Public Folders\All Public Folders\VorysViews").
Views
'Set objFolder = GetFolder("Public Folders\All Public Folders\Company\
Sales")
'Create the new view.
Set CreateView = objViews.Add(Name:=strName, ViewType:=cnstViewType,
SaveOption:=olViewSaveOptionAllFoldersOfType)
End Function
'SaveOption:=olViewSaveOptionAllFoldersOfType)
'The above SaveOption will save the view to all folders
of this type
Public Function GetFolder(strFolderPath As String) As MAPIFolder
' strFolderPath needs to be something like
' "Public Folders\All Public Folders\Company\Sales" or
' "Personal Folders\Inbox\My Folder"
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arrFolders() As String
Dim I As Long
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")
Set objApp = Application
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.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 GetFolder = objFolder
MsgBox "GetFolder is " & objFolder.Name
MsgBox "objFolder is " & GetFolder.Name
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function