Huan-Lin 學習筆記 on DotBlogs

主站及最新文章請至 http://huan-lin.blogspot.com/

  點部落 :: Home :: Syndication :: 我的收藏 ::  :: Login
  文章數 - 171, 回應數 - 96, 引用數 - 0

文章標籤

全部標籤

每月文章

文章分類

以下程式碼可以在 Outlook 中建立一個約會:

using Outlook = Microsoft.Office.Interop.Outlook;
......

namespace AppointmentDemo
{
    public partial class Form1 : Form
    {
        Outlook.Application m_Outlook = new Outlook.Application();

        private void btnCreateAppointment_Click(object sender, EventArgs e)
        {
            Outlook.NameSpace ns;
            Outlook.MAPIFolder contactsFolder;
            Outlook.AppointmentItem apptItem;

            ns = m_Outlook.GetNamespace("mapi");
            contactsFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

            // Create appointment
            apptItem = (Outlook.AppointmentItem) m_Outlook.CreateItem(Outlook.OlItemType.olAppointmentItem);

            apptItem.Subject = "與達文西的約會";
            apptItem.Location = "羅浮宮";
            apptItem.AllDayEvent = true;
            apptItem.Start = DateTime.Now.AddHours(1);
            apptItem.End = DateTime.Now.AddHours(2);

            apptItem.Save();

            MessageBox.Show("新增約會完成!");
        }
    }
}

以下 VBA 範例取自 http://www.outlookcode.com/codedetail.aspx?id=788),可以從 Excel 檔案中匯入約會項目:

Sub ImportAppointments()
    Dim exlApp As Excel.Application
    Dim exlWkb As Workbook
    Dim exlSht As Worksheet
    Dim rng As Range
    
    Dim itmAppt As Outlook.AppointmentItem
    Dim aptPtrn As Outlook.RecurrencePattern
    
    Dim fso As FileSystemObject
    Dim fl As File
    
    Set exlApp = New Excel.Application
    
    strFilepath = exlApp.GetOpenFilename
    If strFilepath = False Then
        exlApp.Quit
        Set exlApp = Nothing
        Exit Sub
    End If
     
    Set exlSht = Excel.Application.Workbooks.Open(strFilepath).Worksheets(1)
    Dim iRow As Integer
    Dim iCol As Integer
    
    Dim tmpItm As Outlook.Link
    Dim mpiFolder As MAPIFolder
    Dim oNs As NameSpace
    
    Set oNs = Outlook.GetNamespace("MAPI")
    
    Set mpiFolder = oNs.GetDefaultFolder(olFolderContacts)
    
    iRow = 2
    iCol = 1
    
    While exlSht.Cells(iRow, 1) <> ""
        Dim cnct As ContactItem
        Set itmAppt = Outlook.CreateItem(olAppointmentItem)
        itmAppt.Subject = exlSht.Cells(iRow, 1)
        Set cnct = mpiFolder.Items.Find("[FullName] = " & exlSht.Cells(iRow, 2))
        If cnct Is Nothing Then
            Set cnct = Outlook.CreateItem(olContactItem)
            cnct.FullName = exlSht.Cells(iRow, 2)
            cnct.Save
        End If
        itmAppt.Categories = exlSht.Cells(iRow, 3)
        itmAppt.Start = exlSht.Cells(iRow, 4)
        itmAppt.AllDayEvent = True
        
        itmAppt.Links.Add cnct
        Set aptPtrn = itmAppt.GetRecurrencePattern
        aptPtrn.StartTime = exlSht.Cells(iRow, 5)
        aptPtrn.EndTime = exlSht.Cells(iRow, 6)
        aptPtrn.RecurrenceType = olRecursYearly
        aptPtrn.NoEndDate = True
        If aptPtrn.Duration > 1440 Then aptPtrn.Duration = aptPtrn.Duration - 1440
        Select Case exlSht.Cells(iRow, 7)
        Case "No Reminder"
            itmAppt.ReminderSet = False
        Case "0 minutes"
            itmAppt.ReminderSet = True
            itmAppt.ReminderMinutesBeforeStart = 0
        Case "1 day"
            itmAppt.ReminderSet = True
            itmAppt.ReminderMinutesBeforeStart = 1440
        Case "2 days"
            itmAppt.ReminderSet = True
            itmAppt.ReminderMinutesBeforeStart = 2880
        Case "1 week"
            itmAppt.ReminderSet = True
            itmAppt.ReminderMinutesBeforeStart = 10080
        End Select
        itmAppt.Save
        iRow = iRow + 1
    Wend
    Excel.Application.Workbooks.Close
    exlApp.Quit
    Set exlApp = Nothing
End Sub

posted on 2008/4/23 08:31 | 1 人推薦 我要推薦 | 閱讀數 : 1401 | 文章分類 [ Office Programming ] 訂閱

Feedback

# re: 用程式建立 Outlook 約會項目 2010/2/4 下午 06:03 simon 回覆
請問大大
我發行到server 後執行會出現下列訊息, 有改善方法嗎?


'/aspx9' 應用程式中發生伺服器錯誤。
--------------------------------------------------------------------------------

由於發生下列錯誤,為具有 CLSID {0006F03A-0000-0000-C000-000000000046} 的元件擷取 COM Class Factory 失敗: 80070005。
描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。

例外詳細資訊: System.UnauthorizedAccessException: 由於發生下列錯誤,為具有 CLSID {0006F03A-0000-0000-C000-000000000046} 的元件擷取 COM Class Factory 失敗: 80070005。

沒有授權 ASP.NET 存取要求的資源。請考慮將資源存取權授與 ASP.NET 要求識別。ASP.NET 有一個基本處理序識別 (通常在 IIS 5 上為 {MACHINE}\ASPNET,在 IIS 6 上為 Network Service),會在應用程式未模擬的情況下使用。如果應用程式是透過 <identity impersonate="true"/> 模擬,這個識別將會是匿名使用者 (通常為 IUSR_MACHINENAME) 或經過驗證的要求使用者。

若要對檔案授與 ASP.NET 存取權,請在檔案總管中以滑鼠右鍵按一下檔案,選擇 [內容] 並選取 [安全] 索引標籤。按一下 [新增] 加入適當的使用者或群組。反白顯示 ASP.NET 帳戶,並且選取所需存取權限的核取方塊。

原始程式錯誤:


行 16: public partial class _Default : System.Web.UI.Page
行 17: {
行 18: Outlook.Application m_Outlook = new Outlook.Application();
行 19:
行 20: protected void Page_Load(object sender, EventArgs e)


原始程式檔: E:\merp 程式開發\ASPX9\ASPX9TEST\Default.aspx.cs 行: 18



# re: 用程式建立 Outlook 約會項目 2010/2/4 下午 10:36 Michael Tsai 回覆

請參考這篇:

http://www.dotblogs.com.tw/darren.net/archive/2009/11/02/darren.net_asp.net_com_vsto_excel_word.aspx



# re: 用程式建立 Outlook 約會項目 2010/2/5 上午 08:49 simon 回覆
大大 您好
小弟仍有一個疑問, 目前 local 端可以執行產生沒有問題, 但是我能發行到 server 在執行時將特定的項目以郵件的方式產生到某一位 同事的行事曆中嗎?

請您解疑一下!

# re: 用程式建立 Outlook 約會項目 2010/2/5 上午 11:08 Michael Tsai 回覆

你可以產生一個 .ics 檔案,附在 email 裡面寄給對方。

參考這篇:

http://social.msdn.microsoft.com/Forums/zh-TW/236/thread/9dca2c46-a428-4918-b21d-790f9a581971



# re: 用程式建立 Outlook 約會項目 2010/2/5 上午 11:45 simon 回覆
大大 您好
對不起沒有說清楚我的問題, 必須是全部由系統程式去完成這一整個流程 , 不再有人員自行操作的過程.
有辦法從 server 端產生郵件直接給指定使用者嗎?
之前有試過 mail to 的方式, 但無法將附件一併發出.


# re: 用程式建立 Outlook 約會項目 2010/2/5 下午 04:45 Michael Tsai 回覆

你可以直接寄送 email 並夾帶附件,但還是要收件人自己點開 .ics 檔案,才有辦法加入他的 Outlook 行事曆。目前我還不知道是否有辦法可以直接把某個 event  "強迫" 加入別人的 Outlook 行事曆。以網路環境來看,我想這似乎不太可能達成(不然我們可以隨便把別人的行事曆灌爆)。



回應

標題:
姓名:
電子郵件:(將不會被顯示)
個人網頁:
內容: 

登入後使用進階評論
Please add 2 and 1 and type the answer here: