2

I have a script that creates tasks from emails in Outlook - currently it just takes the email subject and makes it the task title, which is fine except I want to get rid of the first portion of the subject line up to a certain point.

The email subjects always start the same (although the ticket number is different), an example subject would be:

New Ticket: 3416 - School: School1 - Room: 158 - Desc: Broken PC

The ticket number changes - but I would like to remove everything up to and including "School:"

So we would be left with something like this:

School1 - Room: 158 - Desc: Broken PC

The script I am currently using is this:

    Sub MakeTaskFromMail2(MyMail As Outlook.MailItem)
    Dim strID As String
    Dim olNS As Outlook.NameSpace
    Dim olMail As Outlook.MailItem
    Dim objTask As Outlook.TaskItem

    strID = MyMail.EntryID
    Set olNS = Application.GetNamespace("MAPI")
    Set olMail = olNS.GetItemFromID(strID)
    Set objTask = Application.CreateItem(olTaskItem)
    With objTask
        .subject = olMail.subject
        .DueDate = olMail.SentOn
        .Body = olMail.Body
    End With
    Call CopyAttachments(olMail, objTask)
    objTask.Save

    Set objTask = Nothing
    Set olMail = Nothing
    Set olNS = Nothing
End Sub

Sub CopyAttachments(objSourceItem, objTargetItem)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
   strPath = fldTemp.Path & "\"
   For Each objAtt In objSourceItem.Attachments
      strFile = strPath & objAtt.FileName
      objAtt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
      fso.DeleteFile strFile
   Next

   Set fldTemp = Nothing
   Set fso = Nothing
End Sub

Any help on how to separate this out and remove the first portion would be very helpful.

I don't believe it will be too hard to do - but my knowledge of VB is pretty limited.

*Edit - removed vb.net as it is not appropriate here.

Hanny
  • 2,078
  • 6
  • 24
  • 52

2 Answers2

3

You can use combination of Instr/Mid functions

Dim sInput As String
Dim sOutput As String

sInput = "New Ticket: 3416 - School: School1 - Room: 158 - Desc: Broken PC"
sOutput = Mid(sInput, InStr(sInput, "School:") + 8)
Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
0

In VB.NET, you can accomplish this easily with RegEx, like this:

Dim output As String = RegEx.Replace(input, "New Ticket: \d+ - School: ", "") 

The \d means any digit and the + means one or more times. So, \d+ together means that it will match any number with one or more digits.

In VB Script or VBA, however, you'll need to resort to the string parsing functions like Mid and InStr.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105