0

In a Word document a button fires a macro that creates an Outlook Meeting with the required recipients, subject line, and boilerplate body. The sender will then tweak and send.

The issue is, if I send a meeting invite and take leave from work, a co-worker cannot adjust the meeting times.

Rather than all of us sharing our calendars with full rights, I'd like to set up the meeting from a shared calendar we all have edit rights to.

The code that currently works (for my calendar):

Private Sub CommandButton900_Click()
    Dim xOutlookObj As Object
    Dim OMail As Object
    Dim xEmail As Object
    Dim xMeeting As Object
    Dim xDoc As Object
    Dim myRequiredAttendee As Outlook.Recipient
    Dim myOptionalAttendee As Outlook.Recipient

    Set xOutlookObj = CreateObject("Outlook.Application")
'I've added the following two lines from what I've found online, but I can't figure out how to reference
'objFolder when creating the Outlook meeting.
    Set objNamespace = xOutlookObj.GetNamespace("MAPI")
    Set objFolder = objNamespace.GetDefaultFolder(9).Folders("Analytics Shared")

    Set xMeeting = xOutlookObj.CreateItem(olAppointmentItem)
    Set myRequiredAttendee = xMeeting.Recipients.Add("sample.email@address")

    myRequiredAttendee.Type = olRequired

    With xMeeting
        .MeetingStatus = olMeeting
        .Display
        .Subject = "Review TELCON, "
        .Duration = 60
        xEmail.BodyFormat = olFormatHTML
        xEmail.HTMLBody = "Meeting Body" 
        xEmail.GetInspector().WordEditor.Range.FormattedText.Copy
        xMeeting.GetInspector().WordEditor.Range.FormattedText.Paste
    End With
    
    Set xDoc = Nothing
    Set xMeeting = Nothing
    Set xOutlookObj = Nothing
    Application.ScreenUpdating = True

End Sub
JohnM
  • 2,422
  • 2
  • 8
  • 20
DakotaKid
  • 23
  • 3
  • You can `CreateItem` in a default folder then `.Move` the item to the non-default folder **or** `.Add` to items in the non-default folder. Not necessarily the code but the idea `Set olappt = CalFolder.Items.Add` is seen in [Add appointment to Someones Elses Shared Outlook Calendar Using VBA in MS Access](https://stackoverflow.com/questions/63587494/add-appointment-to-someones-elses-shared-outlook-calendar-using-vba-in-ms-access). – niton Jul 26 '23 at 00:44
  • That worked Eugene. Just needed to add {Set xMeeting.objFolder.Items.Add} to the line following the xMeeting CreateItem line. Thank you again for your help. Do I need to set the anser below as 'solved'? It was more the response above that got me to my solution. – DakotaKid Jul 27 '23 at 15:15
  • You can create an answer post with the working code and accept that. – niton Jul 27 '23 at 15:54

1 Answers1

0

Use the NameSpace.GetSharedDefaultFolder method which returns a Folder object that represents the specified default folder for the specified user. This method is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder).

The following code illustrates how to access the shared calendar folder:

Sub ResolveName() 
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 Dim CalendarFolder As Outlook.Folder 
 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("Eugene Astafiev") 
 
 myRecipient.Resolve 
 
 If myRecipient.Resolved Then 
   Call ShowCalendar(myNamespace, myRecipient) 
 End If 
End Sub 
 
Sub ShowCalendar(myNamespace, myRecipient) 
 Dim CalendarFolder As Outlook.Folder 
 
 Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)  
 CalendarFolder.Display 
 
End Sub

So, instead of using the following code for accessing the folder:

Set objFolder = objNamespace.GetDefaultFolder(9).Folders("Analytics Shared")

You can get the shared folder as shown above and then create a meeting request (or edit if required).

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thank you for the reply Eugene. I'm afraid methods are a bit above my understanding level, so I muddle through trying to figure them out. However, when I copy/pasted your code in to see what it's doing, I immediately get the following error: (hhmm... I thought I'd be able to put in a screen shot, but I'm not seeing how). I get a Compile Error: Method or Data Member not found. It's not moving beyond the Sub line (highlighted), and .GetNamespace is also highlighted in blue. Thoughts? – DakotaKid Jul 26 '23 at 14:08
  • The code in my posts just illustrates how to use the `GetSharedDefaultFolder` method to access the shared folder in Outlook. – Eugene Astafiev Jul 26 '23 at 19:25
  • Ok. I'll play around with it and see what I can do to implement it. Thank you. – DakotaKid Jul 26 '23 at 21:42