Set Custom View

J

Jeremy

Hi,

I'm using Outlook 2007 and that version of VB.

I have an awful lot of folders and I wanted to set each to a particular
custom view. I have written the code below to go through each folder and set
the custom view. However, it does not do this. I think I may need to "save"
after Application.ActiveExplorer.CurrentView = "J VIEW" however, I do not
know how to do this and keep getting an object error. Could someone help.

(As you might realise I want to set the custom view for all folders. I have
not found a macro to do this. I have tried cleaning the custom views using
"outlook.exe /cleanviews" and then changing the "messages" the default view -
this does not work and in fact "outlook.exe /cleanviews" does not properly
reset the "messages" view - different folders have different views despite
being set to the "messages" view.)

My code (pieced together as I can't program):

Sub ProcessAllFolders()
ProcessSubFolder Application.ActiveExplorer.CurrentFolder
MsgBox "All Done!", vbInformation + vbOKOnly, "Process All Folders Macro"
End Sub

Sub ProcessSubFolder(olkFolder As Outlook.MAPIFolder)
Dim olkSubFolder As Outlook.MAPIFolder


'Replace the following line with the code you want to run for each folder
'Debug.Print olkFolder.FolderPath
Application.ActiveExplorer.CurrentView = "J VIEW"


For Each olkSubFolder In olkFolder.Folders
ProcessSubFolder olkSubFolder
Next
Set olkSubFolder = Nothing
End Sub
 
K

Ken Slovak - [MVP - Outlook]

You have to both Apply the setting and Save it.

Get Application.ActiveExplorer.CurrentView as a View object and then use the
View object Apply and then Save methods.
 
J

Jeremy

Hi.

I understand the principle but I can't code very well. Could you tell me
precisely the code?

If I could get the object I think I could code the apply and save method as
it's just object.apply & object.save (i think). Would you mind telling me the
line(s) of code that will give me / get me an object though, I tried:

Get Application.ActiveExplorer.CurrentView as a View object

and as I thought, that doesn't work!

Thanks - J
 
K

Ken Slovak - [MVP - Outlook]

Dim oView As Outlook.View

Set oView = Application.ActiveExplorer.CurrentView

' other code

oView.Apply
oView.Save
 
R

Rafael

I tried all these methods without success and I decided to go the hard way
about it but it works just like I want it.

Here is what I did:

1. Configure the view they way I want it (fields, formating, etc.)
2. Run a script to extract the XML settings from the current view and save
it to a file
3. Use a form to list all current forms and add a new view of my own
4. If I select my custom view from the list, I update its XML property with
the xml settings from step 2, save it and then apply it to the current
explorer.
5. Done. I have my view with all the fields I need and if I need to modify
it, I can always update the text file containing my xml code.

Here is the code to extract the XML setting:

Dim myOLapp1 As Outlook.Application = Globals.ThisAddIn.Application
Dim myExpl As Outlook.Explorer = myOLapp1.ActiveExplorer
Dim myView1 As TableView = myExpl.CurrentView
Dim myfol As Outlook.MAPIFolder = myExpl.CurrentFolder
Dim myfields As ViewFields = myView1.ViewFields
'Dim myxmlItem As ViewField
Dim file As System.IO.StreamWriter
file =
My.Computer.FileSystem.OpenTextFileWriter("C:\ViewXMLSettings.txt", False)
If myfol.DefaultItemType = OlItemType.olContactItem Then
file.WriteLine(myView1.XML)
file.Close()
End If
myOLapp1 = Nothing
myExpl = Nothing
myfol = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top