2017-09-26 37 views
2

以下是我編寫的用於自動發送會議邀請的代碼。涉及Outlook VBA中的Excel單元格的應用程序定義或對象定義的錯誤

代碼從工作表中的單元格中選取內容:Final_List。

我強調了在那裏我得到一個錯誤,當我嘗試從Excel

應用-defined或對象獲取收件人地址 - 定義的錯誤。

Dim outlookApp As Outlook.Application 
Dim outlookmeet As AppointmentItem 
Dim myRequiredAttendee As Recipient 

Dim sh As Worksheet 
Dim RowCount As Long 

RowCount = 2 
'row 1 has headers 

With Worksheets("Final_List") 

    Do While IsEmpty(Cells(RowCount, 1).Value) = False 

     Set outlookApp = CreateObject("Outlook.Application") 
     Set outlookmeet = outlookApp.CreateItem(olAppointmentItem) 
     With outlookmeet 

      .MeetingStatus = olMeeting 

      .Subject = Cells(RowCount, 1).Value & " - " & Cells(RowCount, 2).Value 
      .Location = Cells(RowCount, 3).Value 
      .Start = Cells(RowCount, 5).Value 
      .Duration = Cells(RowCount, 7).Value 

      'getting errors on this line      
      .Recipients.Add (Cells(RowCount, 6).Value) 

      .Recipients.ResolveAll 

      .Body = Cells(RowCount, 4).Value 
      .Send 
     End With 

     RowCount = RowCount + 1 

    Loop 
End With 

Set outlookmeet = Nothing 
Set outlookApp = Nothing 
MsgBox "All invites sent!" 
+0

你有兩個嵌套的'With'報表,這意味着所有的單元格地址在內'With'聲明是在當前活動工作表,可能會或可能不會「Final_List」。 – Variatus

+0

@ Variatus:我可以激活「Final_List」工作表。但我無法在邀請中添加收件人?對這一點有什麼幫助? –

+0

AppointmentItem對象沒有「收件人」屬性。 https://msdn.microsoft.com/en-us/library/office/aa210899(v=office.11​​).aspx – Variatus

回答

0

我得到了這個解決方案:

Sub ScheduleMeeting() 

    Dim outlookApp As Outlook.Application 
    Dim outlookmeet As Outlook.AppointmentItem 

    Dim RowCount As Long 
    Dim Name1 As Variant 

    RowCount = 2 
    'row 1 has headers 
    Worksheets("MeetingInvite").Activate 

    With Worksheets("MeetingInvite") 

     Do While IsEmpty(Cells(RowCount, 1).Value) = False 

      Set outlookApp = CreateObject("Outlook.Application") 
      Set outlookmeet = outlookApp.CreateItem(olAppointmentItem) 

      With outlookmeet 

       .MeetingStatus = olMeeting 
       .Subject = Cells(RowCount, 1).Value 
       .Location = Cells(RowCount, 2).Value 
       .Start = Cells(RowCount, 4).Value 
       .Duration = Cells(RowCount, 6).Value 

       .RequiredAttendees = Cells(RowCount, 5).Value 

       .Body = Cells(RowCount, 3).Value 

       .Display 

      End With 

      RowCount = RowCount + 1 

     Loop 
    End With 

    Set outlookmeet = Nothing 
    Set outlookApp = Nothing 
    'MsgBox "All invites sent!" 

End Sub 
相關問題