【问题标题】:Retrieve user's Outlook status检索用户的 Outlook 状态
【发布时间】: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.Outlook COM 库。
  • @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 &lt;= strFreeBusyData.Length; i += 24),但您可能希望在 C# 中使用基于零的方法。

标签: c# vbscript outlook-2010


【解决方案1】:

VBScript中的以下for循环和逻辑

For i = 1 to Len(strFreeBusyData) Step 24 
    Wscript.Echo dtmStartDate
    strDay = Mid(strFreeBusyData, i, 24)
    ...
Next

等价于 C# 中的以下内容

for(int i = 0; i < strFreeBusyData.Length; i+= 24)
{
    Console.WriteLine(dtmStartDate);
    var strDay = strFreeBusyData.Substring(i, 24);
    ...   
}

VBScript 和 C# 之间的一个很大区别是 VBScript 在处理字符串时往往是基于 1 的,而 C# 是基于 0 的。具体来说,Mid 函数的第一个参数需要一个从 1 开始的字符串索引(1 表示从第一个字符开始),而 string.Substrings 第一个参数应该从 0 开始(0 表示从第一个字符开始)。

编辑

睡在上面之后,在 C# 中处理这个问题的更好方法如下。

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);
}

这将得到一个DateTime,它有今天的日期,但时间是上午 12 点。然后,当您遍历 freeBusy 字符串中的每个字符时,您将在小时为 0 点或 12 点时输出格式为 dd/MM/yyyy 的 startDate。然后写出 12 小时制中的小时 (hh) 和 AM/PM 指示符 (tt),如果字符为“1”,则写出忙碌一词,否则写出空闲。然后将startDate 增加一小时。这避免了 VBScript 代码所具有的不必要的内部 for 循环。

编辑

根据您更新的代码,您应该更改它

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);

到这里

listBox1.Items.Add(
    string.Format(
        "{0}: {1}", 
        startDate.ToString("hh tt"),
        c == '1' ? "Busy" : "Free");
startDate = startDate.AddHours(1);

“{0}: {1}”用于格式化,Console.WriteLine 有一个采用格式字符串的重载。日期没有改变,因为DateTime 是不可变的,您必须捕获AddHours 的返回值,这是我的错。

编辑

这里的代码只显示今天的商品,并且只在上午 8 点到下午 6 点之间显示

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 == '1' ? "Busy" : "Free"));
    }

    startDate = startDate.AddHours(1);
    if(startDate.Date > DateTime.Today)
        break; // stop once we get to tomorrow.
}

如果需要,您可以优化它以在下午 6 点后中断。也可以简写如下

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 to 8 am
foreach (char c in freeBusy.Skip(8).Take(11)) // skip to 8AM and only go to 6PM
{
    listBox1.Items.Add(
        String.Format(
            "{0}: {1}", 
            startDate.ToString("hh tt"),
            c == '0' ? "Free" : "Busy"));

    startDate = startDate.AddHours(1);
}

编辑

改变了

c == '1' ? "Busy" : "Free"

c == '0' ? "Free" : "Busy"

【讨论】:

  • VBScript 中的数组是从零开始的。我认为您在这里将其与 VBA 混淆了。
  • @AnsgarWiechers 是的,我想我实际上是在考虑从 1 开始的第 3 方库中的其他索引对象。VBScript 中的技巧是数组是 UBound 函数给你最后一个索引,你必须加 1 才能得到长度。当你创建一个数组时,你给它的大小 - 1。
  • 只需在将值添加到listBox1 的代码周围添加if(8 &lt;= startDate.Hour &amp;&amp; startDate.Hour &lt;= 18),并在AddHours 调用之后添加if(startDate.Date &gt; DateTime.Today) break;
  • @HackGod555 查看我的更新,了解如何在同一天跳到上午 8 点并在下午 6 点停止。
  • @HackGod555 你看到我对我的回答所做的最后一次编辑了吗?这应该显示上午 8 点和上午 9 点对于您发布的字符串很忙。现在,如果假设是上午 10 点和 11 点,那么我最好的客人是时区偏移 2 小时。
猜你喜欢
  • 2021-10-03
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多