c# - Retrieve user's Outlook status -
i found vbscript code retrieve user's outlook status , display in list box. need c# , there no conversion tools online:
set objoutlook = createobject("outlook.application") set objnamespace = objoutlook.getnamespace("mapi") set objrecipient = objnamespace.createrecipient("kenmyer") strfreebusydata = objrecipient.freebusy(#11/11/2014#, 60) dtmstartdate = #11/11/2014# = 1 len(strfreebusydata) step 24 wscript.echo dtmstartdate strday = mid(strfreebusydata, i, 24) x = 1 12 if x = 1 strtime = "12 am: " else strtime = x - 1 & " am: " end if intfreebusy = mid(strday, x, 1) if intfreebusy = 1 strfreebusy = "busy" else strfreebusy = "free" end if wscript.echo strtime & strfreebusy next x = 13 24 if x = 13 strtime = "12 pm: " else strtime = x - 13 & " pm: " end if intfreebusy = mid(strday, x, 1) if intfreebusy = 1 strfreebusy = "busy" else strfreebusy = "free" end if wscript.echo strtime & strfreebusy next wscript.echo dtmstartdate = dtmstartdate + 1 if dtmstartdate > #11/12/2014# exit end if next
the end result in c# application under appointments list box:
11/11/2014 8 am: free 9 am: free 10 am: free 11 am: free 12 pm: free 1 pm: free 2 pm: free 3 pm: free 4 pm: free 5 pm: busy 6 pm: free
what have far:
private void userschedule() { outlook.application oapp = new outlook.application(); microsoft.office.interop.outlook.namespace ns = oapp.application.session; outlook.recipient recipient = ns.createrecipient(username.text); datetime datetime = datetime.now; string freebusy = recipient.addressentry.getfreebusy(datetime, 60, true); string status = freebusy.substring(0, 1); textbox1.text = status; }
how can convert c#? other set of code similar:
for = 1 len(strfreebusydata) step 24 wscript.echo dtmstartdate strday = mid(strfreebusydata, i, 24) x = 1 12 if x = 1 strtime = "12 am: " else strtime = x - 1 & " am: " end if intfreebusy = mid(strday, x, 1) if intfreebusy = 1 strfreebusy = "busy" else strfreebusy = "free" end if wscript.echo strtime & strfreebusy next
newly edited code ( final answer question)
private void userstatus() { { { outlook.application oapp = new outlook.application(); microsoft.office.interop.outlook.namespace ns = oapp.application.session; outlook.recipient recipient = ns.createrecipient(username.text); datetime datetime = datetime.now; // gets current date , time datetime startdate = datetime.today; //gets todays date startdate.addhours(8); //skip 8 string freebusy = recipient.addressentry.getfreebusy(startdate, 60, true); textbox1.text = freebusy; foreach (char c in freebusy) //iteration process { if (startdate.hour == 0) //start @ 12 contacts.items.add(startdate.tostring("dd/mm/yyyy")); if (8 <= startdate.hour && startdate.hour <= 18) // 8am 6pm inclusive { listbox1.items.add( string.format( "{0}: {1}", startdate.tostring("hh tt"), c == '0' ? "free" : "busy")); } startdate = startdate.addhours(1); if (startdate.date > datetime.today) break; // stop once tomorrow. } } } private void button5_click(object sender, eventargs e) { userstatus(); }
the freebusy
string looks user getting free/busy status for.
i got below doing this:
string freebusy = recipient.addressentry.getfreebusy(datetime, 60, true); textbox1.text = freebusy;
000000002200000000000000000000002200000000000000333333333333333333333333000000000000000000000000000000000000000000000000000000002000001100000000000000002000001000000000000000002000000000000000000000002022200000000000000000002000001000000000000000000000000000000000000000000000000000000000333333333333333333333333000000002001101000000000000000002000000000000000000000002000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000002000001100000000000000002000000000000000000000002000000000000000000000002020000000000000000000002000001000000000000000000000000000000000000000000000000000000000000000002000001100000000000000002000000000000000000000002000000000000000000000002000000000000000
the following loop , logic in vbscript
for = 1 len(strfreebusydata) step 24 wscript.echo dtmstartdate strday = mid(strfreebusydata, i, 24) ... next
would equivalent following in c#
for(int = 0; < strfreebusydata.length; i+= 24) { console.writeline(dtmstartdate); var strday = strfreebusydata.substring(i, 24); ... }
one big difference between vbscript , c# vbscript tends 1 - based when dealing strings, whereas c# 0 based. mid
function's first parameter wants index string 1-based (1 mean start @ first character) whereas string.substring
s first parameter should 0-based (0 mean start @ first character).
edit
after sleeping on better way of dealing in c# following.
datetime startdate = datetime.today; string freebusy = recipient.addressentry.getfreebusy(datetime, 60, true); foreach(char c in freebusy) { if(startdate.hour == 0) console.writeline(startdate.tostring("dd/mm/yyyy")); console.writeline( "{0}: {1}", startdate.tostring("hh tt"), c == '1' ? "busy" : "free"); startdate = startdate.addhours(1); }
this datetime
has today's date, time of 12 am. iterate on each character in freebusy
string output startdate
formatted dd/mm/yyyy when hour 0 or 12 am. write out hour in 12 hour time (hh) , am/pm designator (tt) , word busy if character "1" or free otherwise. increment startdate
1 hour. avoids unnecessary inner loops vbscript code has.
edit
based on updated code should change this
listbox1.items.add("{0}: {1}") ; listbox1.items.add(startdate.tostring("hh tt") + " " + (c == '1' ? "busy" : "free")); listbox1.items.add(c == '1' ? "busy" : "free"); startdate.addhours(1);
to this
listbox1.items.add( string.format( "{0}: {1}", startdate.tostring("hh tt"), c == '1' ? "busy" : "free"); startdate = startdate.addhours(1);
the "{0}: {1}" formatting , console.writeline
has overload takes format string. date wasn't changing because datetime
immuteable , have capture return value of addhours
, bad.
edit
here's code show items today , between 8am , 6pm
foreach (char c in freebusy) //iteration process { if (startdate.hour == 0 ) //start @ 12 contacts.items.add(startdate.tostring("dd/mm/yyyy")); if(8 <= startdate.hour && startdate.hour <= 18) // 8am 6pm inclusive { listbox1.items.add( string.format( "{0}: {1}", startdate.tostring("hh tt"), c == '1' ? "busy" : "free")); } startdate = startdate.addhours(1); if(startdate.date > datetime.today) break; // stop once tomorrow. }
you can optimize break after hit 6pm if want. shortened following
datetime startdate = datetime.today; //gets todays date string freebusy = recipient.addressentry.getfreebusy(datetime, 60, true); contacts.items.add(startdate.tostring("dd/mm/yyyy")); startdate = startdate.addhours(8); //skip 8 foreach (char c in freebusy.skip(8).take(11)) // skip 8am , go 6pm { listbox1.items.add( string.format( "{0}: {1}", startdate.tostring("hh tt"), c == '0' ? "free" : "busy")); startdate = startdate.addhours(1); }
edit
changed
c == '1' ? "busy" : "free"
to
c == '0' ? "free" : "busy"
Comments
Post a Comment