Every day I get 5 to 30 Appointment emails with almost the same content from which I need to create the Tasks in Atlassian Jira. I wrote a macro to automate this process. I've never written anything in VBA before and would be grateful for correcting my code. This macro works.
There is a function Base64Encode that I copied from this documentation
I searched for a way to create a JSON object from the Appointment email, in the end, I decided to create it myself, looks scary but works.
Public Sub AcceptMeeting(ActiveFolder, Inbox As String)
'Parameter: Postfach / Ordner im Postfach
Dim myNamespace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim Subfolder As Outlook.Folder
Dim Folder As Outlook.Folder
Dim Change As Outlook.Folder
Dim Item As Object
Dim myAppt As Outlook.AppointmentItem
Dim myMtg As Outlook.MeetingItem
'Counter to return how many Events was accepted
Dim counter As Integer
counter = 0
Dim Forward As Outlook.MeetingItem
Dim Accept As Boolean
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.Folders(ActiveFolder)
Set Folders = myFolder.Folders
Set Subfolder = Folders.Item(Inbox)
For Each Item In Subfolder.Items
DoEvents
Accept = False
If Item.MessageClass = "IPM.Schedule.Meeting.Request" Then
If ActiveFolder = "Application Management Linux1, I351" Then
Accept = True
End If
'Label for JIRA task
Dim jiraLabel As String
If InStr(1, LCase(Item.Subject), "change") > 0 And Item.UnRead = True And Accept = True Then
If InStr(1, LCase(Item.Subject), "produktion") > 0 Then
Item.Categories = "Change Produktion" 'Kategorie setzen PROD
jiraLabel = """Produktion"""
ElseIf InStr(1, LCase(Item.Subject), "integration") > 0 Then
Item.Categories = "Change Integration" 'Kategorie setzen INT
jiraLabel = """Integration"""
ElseIf InStr(1, LCase(Item.Subject), "test") > 0 Then
Item.Categories = "Change Integration" 'Kategorie setzen INT
jiraLabel = """Testing"""
Else
Item.Categories = "Change Info" 'Kategorie setzen Info
jiraLabel = """Info"""
End If
'Accept Appointment
Set myAppt = Item.GetAssociatedAppointment(True)
Set myMtg = myAppt.Respond(olResponseAccepted, True)
Item.UnRead = False
If ActiveFolder = "Application Management Linux1, I351" Then
'Parse Email to JSON and send
Dim Msg As Outlook.MeetingItem
Set Msg = Item
Set recips = Msg.Recipients
Dim recip As Outlook.Recipient
Dim customBody As String
customBody = Replace(Msg.Body, """", "'")
customBody = Replace(customBody, vbCr & vbLf, "\n")
customBody = Replace(customBody, vbCr, "\n")
customBody = Replace(customBody, vbLf, "\n")
Dim customSubject As String
customSubject = Replace(Msg.Subject, """", "'")
customSubject = Replace(customSubject, vbCr & vbLf, "\n")
customSubject = Replace(customSubject, vbCr, "\n")
customSubject = Replace(customSubject, vbLf, "\n")
Dim regEx As New RegExp
regEx.Pattern = "^\w+\s\w+,\sI351$"
For Each recip In recips
If regEx.Test(recip.AddressEntry) And recip.AddressEntry <> "Application Management Linux1, I351" Then
'Values to create JSON
Dim flds, prt, id, asgn, smry, descrp, issu, name, lfbrkt, rtbrkt, cma, _
dbdots, JSON, issuName, label, startAt, endDate, sqLfBrkt, sqRtBrkt As String
flds = """fields"""
prt = """project"""
id = """id"""
asgn = """assignee"""
smry = """summary"""
descrp = """description"""
issu = """issuetype"""
label = """labels"""
issuName = """Test"""
startAt = """customfield_10021"""
endDate = """customfield_12760"""
name = """name"""
lfbrkt = "{"
rtbrkt = "}"
cma = ","
dbdots = ":"
sqLfBrkt = "["
sqRtBrkt = "]"
'Custom Date Formatting
Dim appStartDate, appStartTime, appEndDate, appEndTime As Date
appStartDate = myAppt.Start
appStartTime = myAppt.Start
appEndDate = myAppt.End
appEndTime = myAppt.End
'JIRA Rest requears specific format, so we have to format out date and time
Dim startDateString, endDateString As String
startDateString = Format(appStartDate, "yyyy-mm-yy") + "T" + Format(appStartTime, "hh:mm") + ":00.000+0200"
endDateString = Format(appEndDate, "yyyy-mm-dd") + "T" + Format(appEndTime, "hh:mm") + ":00.000+0200"
'Creating JSON - It looks scary but works
JSON = lfbrkt + flds + dbdots + " " + lfbrkt + _
vbCrLf + vbTab + prt + dbdots + " " + lfbrkt + _
vbCrLf + vbTab + vbTab + id + dbdots + " " + "30611" + _
vbCrLf + vbTab + rtbrkt + cma + _
vbCrLf + vbTab + smry + dbdots + " " + """" + customSubject + """" + cma + _
vbCrLf + vbTab + descrp + dbdots + " " + """" + customBody + """" + cma + _
vbCrLf + vbTab + issu + dbdots + " " + lfbrkt + _
vbCrLf + vbTab + vbTab + name + dbdots + " " + issuName + _
vbCrLf + vbTab + rtbrkt + cma + _
vbCrLf + vbTab + asgn + dbdots + " " + lfbrkt + _
vbCrLf + vbTab + vbTab + name + dbdots + " " + """" + recip.AddressEntry.GetExchangeUser().Alias + """" + _
vbCrLf + vbTab + rtbrkt + cma + _
vbCrLf + vbTab + startAt + dbdots + " " + """" + startDateString + """" + cma + _
vbCrLf + vbTab + endDate + dbdots + " " + """" + endDateString + """" + cma + _
vbCrLf + vbTab + label + dbdots + " " + sqLfBrkt + jiraLabel + sqRtBrkt + _
vbCrLf + rtbrkt + _
vbCrLf + rtbrkt
'JIRA user
user = "username"
Password = "password"
'Sending request to JIRA
Dim URL As String
URL = "https://jira.app.com/rest/api/2/issue/"
Set xhr = CreateObject("MSXML2.XMLHTTP.6.0")
xhr.Open "POST", URL, False
xhr.setRequestHeader "Content-Type", "application/json"
xhr.setRequestHeader "User-Agent", "Outlook"
xhr.setRequestHeader "Authorization", "Basic " + Base64Encode(user + ":" + Password)
xhr.Send JSON
End If
Next
Set Change = myFolder.Folders("*** SPAM")
Item.Move Change
End If
counter = counter + 1
End If
End If
Next
MsgBox Inbox & ": " & counter & " Meetings accepted", vbOKOnly, ActiveFolder 'Infofeld
End Sub