Manipulating subject in mail using VBA RRS feed

  • Question

  • Hi,

    I have the need for a VBA code for Outlook, that will take the subject line, identify a casenumber XXXX123456, and put [] on the casenumber. For example:

    Subjectline: Printerproblems XXXX123456

    Subjectline: Printerproblems [XXXX123456]

    (The casenumber can be anywhere in the subject line.)

     Dim strSubject As String
     strSubject = Item.Subject
     strSubject = Replace(strSubject, "XXXX", "[XXXX")
     Item.Subject = strSubject

    But I can not find a way to write the last ]. Maybe I am doing this wrong, or something smarter exist.

    Please help.

    Best regards,

    Mr. T

    • Moved by Steve Fan Friday, March 27, 2015 7:27 AM VBA-related
    Thursday, March 26, 2015 1:19 PM

All replies

  • Hello Mr.Thomas,

    > But I can not find a way to write the last ].

    Item.Subject = strSubject & "]"

    or if you need to remove spaces:

    Item.Subject = RTrim(strSubject) & "]"

    See String Functions (Visual Basic) in MSDN for more information.

    Friday, March 27, 2015 9:50 AM
  • Hi Eugene

    Thanks for replying, and sorry if i was not clear enough in my question.

    What i got:
    Subjectline: Printerproblems XXXX123456

    What i want to achieve
    Subjectline: Printerproblems [XXXX123456]

    Best regards.

    Mr. T

    Friday, March 27, 2015 9:55 AM
  • Yes, I understood your question correctly. Did you try to run the code and play with string functions described in MSDN?
    Friday, March 27, 2015 10:18 AM
  • Yes i did quite a bit of research and Work with the string function.

    But my issue is that I can not figure out how to get XXXX123456 changed within the string. If it was only XXXX i chould change i was home free. 

    Added: I see now the reason to your answer. My bad - the subject could also be: "XXXX123456  i have print probems". Or "With case XXXX123456 i have som information". But since the numbers can change, and the position of XXXX can change in the string, it is quite difficult for me. The only thing that is fixed is the for letteres XXXX

    • Edited by Mr. Thomas Friday, March 27, 2015 10:42 AM
    Friday, March 27, 2015 10:39 AM
  • In that case I'd suggest using regular expressions instead. Take a look at the similar forum threads:

    Replace variable string in VBA

    How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

    Friday, March 27, 2015 10:58 AM
  • Hi Mr.Thomas,

    Based on my understanding, the suggestion using Regular Expressions is a very helpful. Have you fixed the issue now?

    If you still have the issue about develop with Regular Expressions using VBA, I suggest that you reopen a new thread in Visual Basic for Application forum.

    Regards & Fei

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 6, 2015 12:16 PM