Outlook Macro VBA to create ticket

Helpdesk for my helpdesk software

Moderator: mkoch227

Post Reply
dr_patso
Posts: 192
Joined: Tue May 15, 2012 3:23 am

Outlook Macro VBA to create ticket

Post by dr_patso »

Hi all,

I have been working on this macro for outlook 2007 - 2010 for a while now.. it takes the selected(highlighted) email in outlook and start creating ticket in the admin panel...

function of script: (launch IE in background, login to hesk, open new ticket page, insert e-mails sender name, senders e-mail address, subject, and message (some filtering and formatting on the e-mail body).. The macro will contain your UN and PW so please be aware of the possible security risk, it wouldn't be difficult to modify the code to wait until you login yourself...

Anyone interested in this? Would love for someone to use it and provide some feedback... I'm thinking there is probably a way to modify this further to track replies.... maybe have the script ask for category and submit the ticket itself and pull the tracking ID and add it to the original message... then have another macro to add any text above that line with the tracking id to the reply section....

just tested with default outlook 2007 setup and fresh installation of hesk 2.4.1 works on my outlook 2010 at work as well.

look for "username" and "password" you must fill this out in the code for it to work. You also must modify the 2 sections with the URL to your hesk installation.

Code: Select all

Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" _
            (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Global Const SW_MAXIMIZE = 3
Global Const SW_SHOWMINIMIZED = 2
Global Const SW_SHOWNORMAL = 1

Sub HelpdeskNewTicket()
Dim helpdeskaddress As String
Dim objMail As Outlook.MailItem
Dim strbody As String
Dim oldmsg As String
Dim senderaddress As String
Dim addresstype As Integer
Dim ie         As Object
Dim sResult    As String
Dim dtTimer    As Date
Dim lAddTime   As Long






Set objItem = GetCurrentItem()

If objItem.BodyFormat = olFormatHTML Then
txt = objItem.Body
strt = InStr(txt, "HYPERLINK")
Do Until strt = 0
  nd = InStr(strt + 13, txt, """")
  txt = Left(txt, strt - 1) & Mid(txt, nd + 1)
  strt = InStr(txt, "HYPERLINK")
Loop
End If

If objItem.BodyFormat = olFormatRichText Then
txt = objItem.Body
strt = InStr(txt, " HYPERLINK")
Do Until strt = 0
  nd = InStr(strt + 13, txt, """")
  txt = Left(txt, strt - 1) & Mid(txt, nd + 2)
  strt = InStr(txt, " HYPERLINK")
Loop
End If

' Sender E=mail Address
senderaddress = objItem.SenderEmailAddress

'Searches for @ in the email address to determine if it is an exchange user
addresstype = InStr(senderaddress, "@")

' If the address is an Exchange DN use the Senders Name
If addresstype = 0 Then
senderaddress = objItem.SenderName
End If


   Const sOVIDURL As String = "http://heskurl.com/admin"
   Const lREADYSTATE_COMPLETE As Long = 4
   
      Set ie = CreateObject("InternetExplorer.Application")
      ie.navigate sOVIDURL
   
      dtTimer = Now
      lAddTime = TimeValue("00:00:20")
      
      Do Until ie.readystate = lREADYSTATE_COMPLETE And Not ie.busy
      DoEvents
      If dtTimer + lAddTime > Now Then Exit Do
      Loop
      
      ie.document.getElementById("user").Value = "username"
      ie.document.getElementById("pass").Value = "password"
      ie.document.forms(0).submit
      
      Do Until ie.readystate = lREADYSTATE_COMPLETE And Not ie.busy
      DoEvents
      If dtTimer + lAddTime > Now Then Exit Do
      Loop
      
      ie.navigate "http://heskurl.com/admin/new_ticket.php"
      
      Do Until ie.readystate = lREADYSTATE_COMPLETE And Not ie.busy
      DoEvents
      If dtTimer + lAddTime > Now Then Exit Do
      Loop
      
               While ie.busy
            DoEvents
         Wend
      
      ie.document.getElementById("name").Value = objItem.SenderName
      ie.document.getElementById("subject").Value = objItem.Subject
      ie.document.getElementById("Email").Value = objItem.SenderEmailAddress
      If objItem.BodyFormat = olFormatHTML Then
      ie.document.getElementById("message").Value = Replace(txt, vbCrLf & vbCrLf, vbCrLf)
      End If
      If objItem.BodyFormat = olFormatPlain Then
      ie.document.getElementById("message").Value = objItem.Body
      End If
      If objItem.BodyFormat = olFormatRichText Then
      ie.document.getElementById("message").Value = txt
      End If
      ie.Visible = True
      apiShowWindow ie.hwnd, SW_MAXIMIZE


      dtTimer = Now
      lAddTime = TimeValue("00:00:20")
   Set ie = Nothing ' If you want to close it.


'Dim PageNumber As Object


Set objItem = Nothing
Set objMail = Nothing
End Sub

Function GetCurrentItem() As Object
Dim objApp As Outlook.Application
Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = _
objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = _
objApp.ActiveInspector.CurrentItem
Case Else
End Select
End Function
Klemen
Site Admin
Posts: 10147
Joined: Fri Feb 11, 2005 4:04 pm

Re: Outlook Macro VBA to create ticket

Post by Klemen »

Thanks for sharing - I don't have or use Outlook myself so I can't give any feedback, but hopefully someone else can.
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
efestione
Posts: 3
Joined: Tue Mar 26, 2013 10:33 am

Re: Outlook Macro VBA to create ticket

Post by efestione »

Hi,
first of all thanks for sharing.

I am trying this script and I found that it correctly opens the "Hesk new ticket" page with many fields already filled.

I have a couple of questions?

1. Do I have to manually submit the form or is it possible to automate also this step?

2. Because I cannot use the pop3 and piping, has anyone written a rule for incoming messages in outlook (client side) for the automatic creation of tickets?

Thank you
Alex
efestione
Posts: 3
Joined: Tue Mar 26, 2013 10:33 am

Re: Outlook Macro VBA to create ticket

Post by efestione »

Ok, i have found the answer for the first question:
efestione wrote: 1. Do I have to manually submit the form or is it possible to automate also this step?
Modify:

Code: Select all

      ie.Visible = True
      apiShowWindow ie.hwnd, SW_MAXIMIZE
with:

Code: Select all

    ie.Visible = False
    ' apiShowWindow ie.hwnd, SW_MAXIMIZE
    ie.document.forms(0).submit
So, I want to reformulate my second question:

2. Has anyone written a vbscript for piping the incoming email to hesk_pipe.php?


Thank you
Alex
dr_patso
Posts: 192
Joined: Tue May 15, 2012 3:23 am

Re: Outlook Macro VBA to create ticket

Post by dr_patso »

I thought this method worked okay because you probably want to categorize the ticket and select the owner...

I made this with some googling and no VBA experience. VBA is very difficult for me. I too cannot use piping because my team is afraid to change our old method of categorizing things in a shared mailbox... I can tell you this saves about 3 copy & pastes and looking back and forth from outlook and the ticket creation page... If you make any progress please update!

One idea I had was..

outlook vba combox to populate the category and owner and then submit the form, once form is submitted it grabs the tracking ID and applies it to the top of the message... Then you could create another macro that navigates to the admin ticket of that tracking id and copies all text above the tracking id... so you have a simple button to track your replies.
efestione
Posts: 3
Joined: Tue Mar 26, 2013 10:33 am

Re: Outlook Macro VBA to create ticket

Post by efestione »

Hi dr_patso,
what I need is to create a ticket from outlook (when an email arrives) and auto-assign it.

Following your approach I have arrived very close to the goal (I can either create new ticket and append new messages to existing tickets) but the upload of the email attachments is not working.

That because, due to a security reason, it is not possible to set the value of input "file" elements programmatically. So I cannot use something like this

ie.document.getElementsByName("attachments[1]")[0].Value = pathname

Another option could be to use the hesk_pipe function, but outlook can save messages also in MSG format (and hesk_pipe accepts files in EML formats).

So, I don't know wich direction I will choice. The challenge is still open :-)
dr_patso
Posts: 192
Joined: Tue May 15, 2012 3:23 am

Re: Outlook Macro VBA to create ticket

Post by dr_patso »

I may try to make some improvements upon this at some point...

Really this is as far as I've gotten and I don't really have any great ideas... I don't have to choose the ticket owner.. in my hesk whoever is logged in becomes the owner when creating a ticket with "auto assign" option for the owner... however that breaks the actual auto assign feature and I have that turned off... probably could make a whole new option for owner on the ticket creation page like "logged in user" and have it default to that...

I can post that code to have autoassign assign the ticket to user that is logged in if you would like.
Post Reply