2017-04-06 54 views
3

我有一個winforms應用程序,我試圖創建一個將創建一個新的Outlook約會的方法。 我正在使用以下代碼,但在創建對象newAppointment的部分中標記了一個錯誤。如何用vb在outlook中創建約會?

Private Sub AddAppointment() 
    Dim newAppointment As Outlook.AppointmentItem = Me.Application.CreateItem _ 
     (Outlook.OlItemType.olAppointmentItem) 
    Try 
     With newAppointment 
      .Start = Date.Now.AddHours(2) 
      .End = Date.Now.AddHours(3) 
      .Location = "ConferenceRoom #2345" 
      .Body = _ 
       "We will discuss progress on the group project." 
      .Subject = "Group Project" 
      .AllDayEvent = False 
      .Recipients.Add("Roger Harui") 
      Dim sentTo As Outlook.Recipients = .Recipients 
      Dim sentInvite As Outlook.Recipient 
      sentInvite = sentTo.Add("Holly Holt") 
      sentInvite.Type = Outlook.OlMeetingRecipientType.olRequired 
      sentInvite = sentTo.Add("David Junca") 
      sentInvite.Type = Outlook.OlMeetingRecipientType.olOptional 
      sentTo.ResolveAll() 
      .Save() 
      .Display(True) 
     End With 
    Catch ex As Exception 
     MessageBox.Show("The following error occurred: " & _ 
      ex.Message) 
    End Tryenter code here 
End Sub 

回答

1

訪問一個Outlook Application對象是必需的,因爲你自己Application有沒有合適的CreateItem方法:

這些方針的東西:

Imports Microsoft.Office.Interop 

.... 

Private Sub AddAppointment() 
    '--- Check if Outlook is already up and running 
    If Process.GetProcessesByName("outlook").Count > 0 Then 
     '--- all ready --- 
    Else 
     '--- start Outlook --- 
     Process.Start("outlook") 
     '--- more elaborate wait might be a good idea here --- 
     System.Threading.Thread.Sleep(500) 
    End If 

    '--- initialize required Application object --- 
    '--- assuming it is not available as variable already --- 
    Dim objOutlook As Outlook.Application 
    objOutlook = CreateObject("Outlook.Application") 

    Dim newAppointment As Outlook.AppointmentItem = 
     objOutlook.CreateItem(Outlook.OlItemType.olAppointmentItem) 
    Try 
     With newAppointment 
      .Start = Date.Now.AddHours(2) 
      .End = Date.Now.AddHours(3) 
      .Location = "ConferenceRoom #2345" 
      .Body = "We will discuss progress on the group project." 
      .Subject = "Group Project" 
      .AllDayEvent = False 
      .Recipients.Add("Roger Harui") 
      Dim sentTo As Outlook.Recipients = .Recipients 
      Dim sentInvite As Outlook.Recipient 
      sentInvite = sentTo.Add("Holly Holt") 
      sentInvite.Type = Outlook.OlMeetingRecipientType.olRequired 
      sentInvite = sentTo.Add("David Junca") 
      sentInvite.Type = Outlook.OlMeetingRecipientType.olOptional 
      sentTo.ResolveAll() 
      .Save() 
      .Display(True) 
     End With 
    Catch ex As Exception 
     MessageBox.Show("The following error occurred: " & ex.Message) 
    End Try 
End Sub 
+0

感謝您的幫助,它解決了這個問題,您只是救了我 – JoseEduardo

0

這可能是來自你是什麼一點點最初想到的,但我認爲這會幫助你開始正確的方向。

如果要使用Excel在Outlook中創建約會,請運行下面的腳本(在Excel中)。

Private Sub Add_Appointments_To_Outlook_Calendar() 

    'Include Microsoft Outlook nn.nn Object Library from Tools -> References 
    Dim oAppt As AppointmentItem 
    Dim Remind_Time As Double 

    i = 2 
    Subj = ThisWorkbook.Sheets(1).Cells(i, 1) 

    'Loop through entire list of Reminders to be added 
    While Subj <> "" 
     Set oAppt = Outlook.Application.CreateItem(olAppointmentItem) 

     oAppt.Subject = Subj 
     oAppt.Location = ThisWorkbook.Sheets(1).Cells(i, 2) 
     oAppt.Start = ThisWorkbook.Sheets(1).Cells(i, 3) 
     Remind_Time = ThisWorkbook.Sheets(1).Cells(i, 4) * 1 * 60 
     oAppt.ReminderMinutesBeforeStart = Remind_Time 
     oAppt.AllDayEvent = True 
     oAppt.Save 

     i = i + 1 
     Subj = ThisWorkbook.Sheets(1).Cells(i, 1) 
    Wend 
    MsgBox "Reminder(s) Added To Outlook Calendar" 

End Sub 

'的代碼來自此鏈接: http://officetricks.com/add-appointment-to-outlook-calendar-through-excel-macro-vba/

這裏的設置應該是什麼樣子的屏幕。

enter image description here