【发布时间】:2015-01-08 12:00:16
【问题描述】:
我找到了 VBScript 代码来检索用户的 Outlook 状态并显示在列表框中。我需要它是 C# 并且没有在线转换工具:
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#
For i = 1 to Len(strFreeBusyData) Step 24
Wscript.Echo dtmStartDate
strDay = Mid(strFreeBusyData, i, 24)
For x = 1 to 12
If x = 1 Then
strTime = "12 AM: "
Else
strTime = x - 1 & " AM: "
End If
intFreeBusy = Mid(strDay, x, 1)
If intFreeBusy = 1 Then
strFreeBusy = "Busy"
Else
strFreeBusy = "Free"
End If
Wscript.Echo strTime & strFreeBusy
Next
For x = 13 to 24
If x = 13 Then
strTime = "12 PM: "
Else
strTime = x - 13 & " PM: "
End If
intFreeBusy = Mid(strDay, x, 1)
If intFreeBusy = 1 Then
strFreeBusy = "Busy"
Else
strFreeBusy = "Free"
End If
Wscript.Echo strTime & strFreeBusy
Next
Wscript.Echo
dtmStartDate = dtmStartDate + 1
If dtmStartDate > #11/12/2014# Then
Exit For
End If
Next
在我的 C# 应用程序中,约会列表框下的最终结果如下所示:
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
到目前为止我所拥有的:
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;
}
如何将其转换为 C#?另一组代码类似:
For i = 1 to Len(strFreeBusyData) Step 24
Wscript.Echo dtmStartDate
strDay = Mid(strFreeBusyData, i, 24)
For x = 1 to 12
If x = 1 Then
strTime = "12 AM: "
Else
strTime = x - 1 & " AM: "
End If
intFreeBusy = Mid(strDay, x, 1)
If intFreeBusy = 1 Then
strFreeBusy = "Busy"
Else
strFreeBusy = "Free"
End If
Wscript.Echo strTime & strFreeBusy
Next
新编辑的代码(我的问题的最终答案)
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 and time
DateTime startDate = DateTime.Today; //gets todays date
startDate.AddHours(8); //Skip to 8 am
string freeBusy = recipient.AddressEntry.GetFreeBusy(startDate, 60, true);
textBox1.Text = freeBusy;
foreach (char c in freeBusy) //iteration process
{
if (startDate.Hour == 0) //start at 12 AM
Contacts.Items.Add(startDate.ToString("dd/MM/yyyy"));
if (8 <= startDate.Hour && startDate.Hour <= 18) // 8AM to 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 we get to tomorrow.
}
}
}
private void button5_Click(object sender, EventArgs e)
{
userstatus();
}
freeBusy 字符串对于我正在为其获取忙/闲状态的用户看起来像这样。
我通过这样做得到了以下信息:
string freeBusy = recipient.AddressEntry.GetFreeBusy(datetime, 60, true);
textBox1.Text = freeBusy;
000000002200000000000000000000002200000000000000333333333333333333333333000000000000000000000000000000000000000000000000000000002000001100000000000000002000001000000000000000002000000000000000000000002022200000000000000000002000001000000000000000000000000000000000000000000000000000000000333333333333333333333333000000002001101000000000000000002000000000000000000000002000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000002000001100000000000000002000000000000000000000002000000000000000000000002020000000000000000000002000001000000000000000000000000000000000000000000000000000000000000000002000001100000000000000002000000000000000000000002000000000000000000000002000000000000000 P>
【问题讨论】:
-
如果您对此进行过任何尝试,您应该发布它,并附上您不了解的部分的详细信息。
-
要开始使用,您需要参考
Microsoft.Office.Interop.OutlookCOM 库。 -
@juharr 我编辑了这个并添加了我尝试过的内容,但是从 for 开始的逻辑真的让我很困惑。
-
@juharr 我引用了所有这些 Microsoft.Office.Interop.Outlook.RecurrencePatternrecurrencePattern; Microsoft.Office.Interop.Outlook.NameSpace onNS; Microsoft.Office.Interop.Outlook.MAPIFolder oCalendar;Microsoft.Office.Interop.Outlook.Items oItems; Microsoft.Office.Interop.Outlook.AppointmentItem oAppt; Microsoft.Office.Interop.Outlook.Application _OutlookApplication;
-
基本上,for循环将
strFreeBusyData字符串拆分为24个字符。它会从字面上翻译为for(int i = 1; i <= strFreeBusyData.Length; i += 24),但您可能希望在 C# 中使用基于零的方法。
标签: c# vbscript outlook-2010