Selected date on calendar

M

Marc Weichhold

Hello,

I am trying to get the selected date from the calendar month view. I already
learned that the calendar view object itself does not provide any means to
get that data, however, I found Sue Moshers solution on

http://www.outlookcode.com/codedetail.aspx?id=616

Basically, it fetches the command for a new appointment, executes it (so the
new appointment form would open), reads the start and end date from the form
and closes it again. The problem here is that the execute command does not
"work" - there is no new appointment form that opens. No error message, no
exception, just nothing is happening.

Several other readers had that problem, too, and the solution seemed to be
that the command bars had to be reset. But I did not change any command bars
here (no custom commands yet), so I can not reset them.

Can anyone help me?

Marc
 
K

Ken Slovak - [MVP - Outlook]

The command being executed is the New, Appointment command on the Standard
toolbar. Is that being displayed and are you displaying a calendar folder
when you execute your code?

What if you switch the command button and try using 1992 as the button ID to
execute, does that work? That's the Actions, New Appointment button.
 
M

Marc Weichhold

Hello Ken.
The command being executed is the New, Appointment command on the Standard
toolbar. Is that being displayed and are you displaying a calendar folder
when you execute your code?
Yes, I am displaying the calendar month view when the code is executed and
the command (New, Appointment) is displayed on the standard toolbar.
What if you switch the command button and try using 1992 as the button ID
to execute, does that work? That's the Actions, New Appointment button.
I tried it and the behavior is the same as before. The execute command does
not open the appointment form and I get no error or exception.

The "funny" part is, for testing purposes I changed the caption of the
command right before I call the execute method - and that works. The caption
is changed, but the execute method is still without effect.

Marc

PS: Btw, if that is important, I am using Outlook 2007 with Exchange Server
2007 and a Visual Studio 2008 Developer Edition. The AddIn language is C#.
 
M

Marc Weichhold

Hello,

one more information. I tried the code from Sue as a VBA macro - and it
worked.

Strange...

Marc
 
K

Ken Slovak - [MVP - Outlook]

Now I'm confused.

The code Sue had up is an Outlook VBA macro. What were you trying it as
previously?

If the code wasn't running inside the Outlook VBA project it would never
work as written, the Application object referenced wouldn't be an
Outlook.Application object.
 
M

Marc Weichhold

Hello Ken,
The code Sue had up is an Outlook VBA macro. What were you trying it as
previously?

If the code wasn't running inside the Outlook VBA project it would never
work as written, the Application object referenced wouldn't be an
Outlook.Application object.

I used the code in a VSTO-AddIn written in C#. Of course, I made some
changes so it would compile and run. It is not that hard, after all I GET
the right button. I can read its properties, even change them. All that does
not work - in the C#-AddIn - is the Execute-method.

For C# the code changes to:

// Application is of type Microsoft.Office.Interop.Outlook.Application
Outlook.Explorer objExpl = Application.ActiveExplorer();
if(objExpl != null)
{
Outlook.Folder objFolder = (Outlook.Folder)objExpl.CurrentFolder;
if(objFolder.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
{
Office.CommandBarButton objCB =
(Office.CommandBarButton)objExpl.CommandBars.FindControl(Type.Missing, 1106,
Type.Missing, Type.Missing);
if(objCB != null)
{
objCB.Execute();
Outlook.AppointmentItem objAppt =
(Outlook.AppointmentItem)Application.ActiveInspector().CurrentItem;
DateTime dtStart = objAppt.Start;
DateTime dtEnd = objAppt.End;
((Outlook._AppointmentItem)objAppt).Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
}
}
}

And as I said, if I place

objCB.Caption = "BlahBlah";

before

objCB.Execute();

then the caption of the button is changed.

Marc
 
K

Ken Slovak - [MVP - Outlook]

If you were to breakpoint on the line:

DateTime dtStart = objAppt.Start;

does an Inspector ever get opened with the appointment?

I just tried your code and it works here with no problems with Outlook 2007
and a VSTO addin written in C#. I fired the code from a button click in an
Explorer window, the code ran from my Explorer wrapper class. About the only
change I made was to just use the class level Explorer object in the wrapper
and not use an Application object to get ActiveExplorer(). Other than that
my code was a copy and paste of yours. So I'm not sure why it's not working
for you.
 
M

Marc Weichhold

Hello Ken,
If you were to breakpoint on the line:

DateTime dtStart = objAppt.Start;

does an Inspector ever get opened with the appointment?
No, there is no Inspector opened at all. The code does throw an exception at
the line

Outlook.AppointmentItem objAppt =
(Outlook.AppointmentItem)Application.ActiveInspector().CurrentItem;

because the method ActiveInspector() returns null. And that's why it would
never reach a breakpoint at the line with dtStart. If I understood it right,
the button's Execute-method is supposed to open a new Inspector object
(which then does open the appointment form). But that never happens, so
there is no ActiveInspector at all after the Execute command. The container
Application.Inspectors is also empty at the point (Count = 0).
I just tried your code and it works here with no problems with Outlook
2007 and a VSTO addin written in C#. I fired the code from a button click
in an Explorer window, the code ran from my Explorer wrapper class. About
the only change I made was to just use the class level Explorer object in
the wrapper and not use an Application object to get ActiveExplorer().
Other than that my code was a copy and paste of yours. So I'm not sure why
it's not working for you.
Beats me, too. From your experience, would you really think that the
Explorer object is the "problem"? I mean, after all I can manipulate the
button object that I get from the Explorer's command bars...

Marc

PS: Happy Holidays.
 
K

Ken Slovak - [MVP - Outlook]

I have no idea what the problem is since the same code worked perfectly
here. I tested it in Outlook 2007, with a VSTO addin written in C# with your
code. That's about as close as I can get to whatever your setup is.

Are you running any Outlook addins at all? I'd disable any that might be
running just on the off chance that some other addin is messing up yours.
But I wouldn't put money on that being the problem.

Happy holidays to you too, and to everyone here.
 

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