Creating and applying a view programmatically

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an add-in that creates and applies a view for the calendar.
It works just fine in the first default explorer.
But if the user opnes another calendar explorer - I'm getting an error from
Outlook (not an exception!) when trying to apply the view saying that the
view was not found. I was told that to workaround this issue I need to switch
folders before I can apply the new view. This works well the first time
(switching to email and back to calendar) but doesn't work well the second
time (cannot switch back to calendar).
Any other ways to resolve this problem? I really need to apply the view for
the user.

Thanks!

Inbar.
 
Here is the code snippet (w/o the workaround):
OlApp.ActiveExplorer().CurrentFolder.CustomViewsOnly = true;
Outlook.View lastView = OlApp.ActiveExplorer().CurrentFolder.CurrentView;
Outlook.View newView = null;
try
{
newView =
OlApp.ActiveExplorer().CurrentFolder.Views.Add(textBoxFilterName.Text,
Outlook.OlViewType.olCalendarView,
Outlook.OlViewSaveOption.olViewSaveOptionThisFolderEveryone);
}
catch (System.ArgumentException)
{
MessageBox.Show (this, "Another view with this name already exists.
Enter a different name.", "Microsoft Office Outlook", MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
newView.XML = BuildViewXml (newView.XML);
newView.Apply();

This last line (the Apply()) is where Outlook will have an error message (no
exception is thrown and so I cannot catch this problem!)

Any ideas?
 
First, your Catch block is premature. It should go after the .Save method,
which should go after you set the XML property. Then call .Apply.
 
Eric,

There is no .Save method in this code snippet. If you mean the .Add method -
then there is no way to do what you are saying. The new view does not exist
before I add it and so I cannot assign the XML property of an object that
does not exist.
In addition, your issue with the catch block has nothing to do with the
problem I face. The catch block is merely there to make sure to catch users
who try to create a view with a name that already exists.

Inbar.
 
Back
Top