问题
I am trying to write some VB code within Excel 2007 that will automatically send out meeting invites from the Outlook 2007 Calendar, to a list of addressees listed in the excel spreadsheet, on the dates specified within the spreadsheet. This is useful because I can send out hundreds of meeting requests to different people on different dates with one click of a button. I can do this fine when sending from my own user account with the following code:
' Create the Outlook session
Set myoutlook = CreateObject("Outlook.Application")
' Create the AppointmentItem
Set myapt = myoutlook.CreateItem(olAppointmentItem) ' Set the appointment properties
With myapt
.Subject = " Assessment Centre "
.Location = "conference room A"
.Start = Cells(5, 24 + j) & " 17:00:00 PM"
.Duration = 120
.Recipients.Add Cells(i, 1).Value
.MeetingStatus = olMeeting
' not necessary if recipients are email addresses
'myapt.Recipients.ResolveAll
.AllDayEvent = "False"
.BusyStatus = "2"
.ReminderSet = False
.Body = L1 & vbCrLf & vbCrLf & L2 & vbCrLf & vbCrLf & L3 & vbCrLf & vbCrLf & L4
.Save
.send
End With
But now I want to send the meeting request from a dummy user account for which I am a delegate using something like ’sendonbehalfof’ so that the dummy calendar stores all the meeting invites, and other delegates can operate the system as well using the same dummy user account. This works fine when sending an email with the following code:
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = " John.Smith@John_Smith.com "
.Subject = "Subject"
.Body = "Body"
.SentOnBehalfOfName = "Fred.bloggs@fred_blogs.com"
.send
End With
The email will say from ’me on behalf of Fred Bloggs’
But I can’t get it to work with Calendar appointments.
I’ve searched high and low with words like ‘appointment’, ‘meeting request’, sendonbehalfof’ etc, and it seems you should be able to do this for appointments with ’sendusingaccount’ but this doesn’t seem to work (it doesn’t fail, just ignores the instruction and sends from my own user account as before).
Can anyone tell me how to do this? Thanks very much.
回答1:
If you have delegate access to another user's mailbox, use GetSharedDefaultFolder
to obtain a reference to the user's shared calendar, then use Folders.Items.Add
to add the meeting to their calendar.
Ex:
Dim fldr As Outlook.Folder
Dim appt As Outlook.AppointmentItem
Set fldr = Session.GetSharedDefaultFolder(_
Outlook.CreateRecipient("Fred.bloggs@fred_blogs.com"), _
olFolderCalendar)
Set appt = fldr.Items.Add
' set up your appointment here
' i.e.:
' With appt
' .Start = Cells(5, 24 + j) & " 17:00:00 PM"
' .Duration = 120
' End With
' make sure you call the appt.Save method to save the appt!
Adapted from: http://www.outlookcode.com/codedetail.aspx?id=43
来源:https://stackoverflow.com/questions/11678324/how-can-i-use-vb-code-in-outlook2007-to-send-a-series-of-meeting-invites-using-s