Email to task without changin formatting

Status
Not open for further replies.

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Hello Slipstick forum!

I got a little macro running which turns specific mails into tasks basically working as a tiny supportsystem for me to keep track of stuff at my company.
The thing is that it won't keep the formatting. I tried 3 way to do this:

Right now I just use
Code:
newTask.Attachments.Add NewItem
but I don't really like it that way as it's a mail in a task.

The second one is the 'open in word, copy, paste' method.
Code:
    Set objDoc = sourceItem.GetInspector.WordEditor
    If Not objDoc Is Nothing Then
        Set objSel = objDoc.Windows(1).Selection
        objSel.WholeStory
        objSel.Copy
        Set objDoc2 = targetItem.GetInspector.WordEditor
        If Not objDoc2 Is Nothing Then
            Set objSel2 = objDoc2.Windows(1).Selection
            objSel2.PasteAndFormat wdPasteDefault
        End If
    End If
newTask.Display
this one looks lovely, but keeps crashing every once in a while.

And simply using
Code:
 newTask.Body = NewItem.Body
resulsts in missing images, double spacing (which I have taken care of), and just odd formatting in general.

Is there any other way, or maybe some ideas to fix the crashes ?

Outlook 2010 H&B running on Server 2012R2.


If needed, I will provide further details and information.

Best wishes and thanks in advance,
Chris
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Part of the problem is that tasks doesn't support HTML, so you can't use HTML body. I haven't tried converting the message body to RTF then pasting... not sure it will work. (MailItem.RTFBody Property (Outlook) I haven't had crash problems with the word paste method - but it could be something different between 2016 and 2010.

So basically... you are stuck unless RTF body works better than using word.
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
I tried experimenting with RTF/HTML convertion, no luck with that.
But I'll give it another shot tomorrow.

Yeah it's kinda weird. Most of the time it works just fine, then all of a sudden it crashes and disables the rules. I created a rule to check if the other rule is active, if not, activate, but when it crashes both rules are disabled. So I would need a rule to check the rule to check the rule.

I guess I'll look into it again toomorrow maybe I'll get it to work without crashing.


Oh by the way, I read many of your posts about VBA thank you really much, I appreaciate your work!
Got some pretty neat things to work because of you!

Im gonna check back tomorrow with some updates.


With kind regards,
Chris
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Heh... it works here. I changed the macro i use to use RTF - in my case, i save the new tasks to one of two folders and use a stub macro to set the folder (and the GetCurrentItem function so it works for open or selected messages) - the task looks nice - downloaded images won't copy (even if i download the blocked content first), embedded images are not kept. Using .Body = objMail.RTFBody won't keep them either.

This screenshot is of the original message (the pink formatting was another macro test for someone last week :)) - using word to paste is in the middle and using RTF is on the right. Word gets external content.

formatting.png

This message has 3 images inserted - RTF drops them but my macro adds them as attachments.

embedded-images.png

Code:
Sub TaskRTF()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set tasksFolder = Ns.GetDefaultFolder(olFolderTasks)
ConvertRTFToTask
End Sub


Private Sub ConvertRTFToTask()
    Dim objTask As Outlook.TaskItem
    Dim objMail As Outlook.MailItem
   
    On Error Resume Next
   Set objMail = GetCurrentItem()
   Set objTask = tasksFolder.Items.Add(olTaskItem)
 
   strRTF = StrConv(objMail.RTFBody, vbUnicode)

Debug.Print strRTF
 
With objTask
If objMail.Attachments.Count > 0 Then
     CopyAttachments objMail, objTask
End If

    .Subject = objMail.Subject
    .DueDate = Now + 3
    .StartDate = Now + 2
    .ReminderSet = True
    .ReminderTime = .DueDate + "2:00:00 PM"
    .Body = strRTF
   ' .Save
    .Display
End With

    Set objTask = Nothing
    Set objMail = Nothing
End Sub
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Yeah when I first tried to do it, I just copied the body, removed the double spaces and reattached all images. The thing is, when someone get a mail marked as spam by our firewall, they get a mail, forward it to support and we check it and release or block it.

But the mail contains a huge amount of images. If I remove those the mail becomes cryptic and you need to take a close look just to get the information you need.

I had a workaround of not taking those mails a tasks and just forward them but well. It would be really nice if I could just get it to work properly somehow without changing the body or it's contents in any way.

Guess I won't get around the word method.


Thanks again.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
BTW, the macro i use to copy using word and insert into tasks is here - Create Task or Appointment and Insert Selected Text - our codes are slightly different but i have no idea if mine might work better than yours.

to use it in a rule, remove the set objmail line and change the top lines:
Sub ConvertSelectionToTask(objMail As Outlook.MailItem)

Dim objTask As Outlook.TaskItem


one other thought - if you are getting multiple messages in at once that trigger the rule, this could cause failure because the macro is busy - use the stub method - the rule grabs the message and hands it off to the macro so the rule is ready to process the next one. This is especially helpful when you do stuff that can take longer to process.

Sub TaskRTF(objMail As Outlook.MailItem)
ConvertRTFToTask objMail
End Sub

private Sub ConvertRTFToTask (objMail As Outlook.MailItem)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
one other thought - itemadd instead of rules - you'll need something to use an if statement on -
if sender = quarantine address should work.

ETA: you could use rules to move the messages to a folder, then use itemadd to watch the folder and process the mail. If you do this, i'd mark the mail read after it's processed so you know if the macro fails.
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Heya Diane,

sorry for the late feedback. I changed it so the mail is an attachment in the task. It is the safest way to keep it running imo.

And still it crashes after a while. The whole thing runs on a Server 2012 at the moment, the user is not logged off but locked. It runs for a while and after a while the rule crashes. So I'll just post the whole code for it here maybe you can see something that causes problems.

I edited out some parts as for sensitive information.
The code is really messy since I changed large chunks of it and didn't have the time to clean it up just yet.
There was, well, still is a function to check if certain computers are online to send them a mail to notify them about the incoming ticket, if they're offline, don't send notification. But that function just wasn't necessary so I hid it.

Again, it works fine for a while and then the rule just crashes after a bit.
I used a second rule to check if the first rule is active everytime a mail comes in, but if the first rule crashes the second one does too.

Well maybe you, or someone else finds something that I just don't see or don't know.

Many thanks in advance!

Code:
Sub ChangeSubjectForward(NewItem As Outlook.MailItem)

    On Error Resume Next

'Global Items
IncSubject = NewItem.Subject
Sender = NewItem.Sender
Dim newTask As Outlook.TaskItem
'Dim WordApp As Word.Application
'Dim wdDoc As Word.Document
Set newTask = Application.CreateItem(olTaskItem)

'logs
Dim rStats As String: rStats = Now & "--- INCOMING TICKET '" & IncSubject & "' FROM " & Sender
strFile_path = "C:\Tickets\logs\logs.txt"
Open strFile_path For Append As #1
Write #1, rStats
Close #1

'get ticket ID, +1, write new ID
Dim path As String: path = "C:\Tickets\config\lidn.txt"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(path, 1)
        filereader = file.ReadLine
        NewNumber = CInt(filereader)
             NewNumber = NewNumber + 1
                fso.CreateTextFile(path, True).Write (NewNumber)

    newTask.Attachments.Add NewItem
  
With newTask
    .DueDate = Now
    .Subject = "Ticket#" & NewNumber & " - " & Sender & " - " & Now & " - " & " ' " & IncSubject & " ' "
    .Save
End With

'Save task in right folder
Set olNS = NewItem.Application.GetNamespace("MAPI")
Set accs = olNS.Accounts
For Each Account In accs
    If Account.AccountType = olExchange And Account.SmtpAddress = olNS.Session.DefaultStore Then
        Set pub = olNS.Folders("Public Folders" & " - " & Account.SmtpAddress).Folders("All Public Folders").Folders("Support").Folders("Tickets")
    End If
Next

Dim outObj As Outlook.Application
Dim objFolder As Outlook.MAPIFolder

Set outObj = CreateObject("outlook.application")
Set objFolder = pub
newTask.Move objFolder

With NewItem
    .Subject = Sender & " sent new support request titled: '" & IncSubject & "' at " & Now
    .Save
End With

Set Forward = NewItem.Forward
Forward.Recipients.Add me@test.com

rStats = Now & "--- SUCCESFULLY RECEIVED AND CONVERTED TICKET" & vbNewLine
strFile_path = "C:\Tickets\logs\logs.txt"
Open strFile_path For Append As #1
Write #1, rStats
Close #1
   
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Again, it works fine for a while and then the rule just crashes after a bit.
i wonder if it's a memory issue - since you declare the variables in the macro - set them to nothing at the end.
set newTask = nothing
Set outObj = nothing ' are you even using this variable?
Set objFolder = nothing

Does this work?
Set olNS = NewItem.Application.GetNamespace("MAPI")
Set accs = olNS.Accounts

Have you tried removing on error resume next and seeing if it hangs?

If any of the variables are used every time, it might be better to set them globally at startup - you'd definitely need to restart outlook after the rules crash though as that will kill the variable.
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Thanks for the reply.

Do the variables persist even when the macro is done?
If so a memory issue could be the case. Didn't think of that.

I put the = nothing statement at the end of the code, let's see if it changes anything.
And yeah I use the outObj but just once to move the task to the folder.

I just replaced the on error resume next with a on error goto errorhandler I used in another macro which comes in handy but I didn't think about using it here for some reason.

Code:
ErrorHandler:
  If Err.Number <> 0 Then
     msg = "Error# " & Str(Err.Number) & " generated by " _
         & Err.Source & Chr(13) & "Line: " & Erl & Chr(13) & Err.Description
    
     rStats = Now & " - ERROR - " & msg
        strFile_path = "C:\Tickets\logs\logs.txt"
        Open strFile_path For Append As #1
        Write #1, rStats
        Close #1
            Resume Next
     End If

This should give me at least a proper error log.

And I already set up a task schedule to restart outlook twice a day - just to make sure.


I'll test it with the nothing statements and check back once I got some results.

Thanks again.

cheers
Chris
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Oooooh and I just realized what you mean.

Code:
Dim outObj As Outlook.Application
Dim objFolder As Outlook.MAPIFolder

Set outObj = CreateObject("outlook.application")
Set objFolder = pub
newTask.Move objFolder

I just scrapped that whole part and simply use
newTask.Move pub
Don't know why I didn't do that beforehand :D
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Okay, so we had a long weekend here and guess what, it crashed. At some point between the 24. and this morning it crashed and deactivated the rules.

And I didnt get any logs about it.

So I assume that it's not the code but Outlook as the code would give me at least some kind of error.

Eventviewer doesn't show anything but "Addins where loaded".


I just tested it and got an "Error running the operation" for both rules.
Logs still won't show anything. So the script doesn't even run.
It just crashes without doing anything. But why?

The rule just says "If receiving mail, run script". Why would it crash before even running the script ?

I'm all out of ideas here.



Kind Regards,
Chris
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Was the logging working before the 24th?

You aren't using 2016 on to run the scripts, correct? They are disabling run a script rules in 2016 - not sure if it will be back ported to earlier versions. Scripts need to be enabled using a reg key. Run-a-Script Rules Missing in Outlook 2016
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
If I do an error on purpose it works and logs it.
No logs between 24th and today though.

The Server is 2012 R2 and is running Outlook 2010 H&B.

This is getting more and more frustrating.

I send a test mail every now and then, remote log on the server and there it is the shiny blinking window "The operation failed" and the rules are disabled again after working fine for hours or sometimes even a few days.

No logs though.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
(correction) What is the error handler? I'm wondering if it fails on something and isn't handling the error.
 

LaoFly

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Pretty much this at the very end of the code:

Code:
ErrorHandler:
  If Err.number <> 0 Then
     msg = "Error # " & str(Err.number) & " generated by " _
         & Err.Source & Chr(13) & "Line: " & Erl & Chr(13) & Err.Description
     MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
   
     Dim rStats As String: rStats = Now & " - ERROR - " & msg
        strFile_path = "C:\Tickets\logs\logs.txt"
        Open strFile_path For Append As #1
        Write #1, rStats
        Close #1
            Resume Next
     End If

But then again if I force an error by for example NewItems.XYZ it gives me a proper log about it.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Remove MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext - if you aren't in front of the console to dismiss the message box, outlook will basically hang.

resume next in the write routine should push it on without forcing the rule to die - as long as it gets triggered by the code.

Also, use a stub macro to test the code manually on a selected message using the step into command so you can watch it run.


Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
OpenLinks objItem

End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
D Using a VBA Custom Form to Send Reoccurring Email Upon Task Completion Outlook VBA and Custom Forms 4
W Deleting Sent Task Email, Deletes the task from my list Using Outlook 1
S Task Update coming as email and not updating task Using Outlook 3
Diane Poremsky Create a Task from an Email using a Rule New Slipstick.com Articles 0
Bachelle Macro to Update Existing Task from New Email Outlook VBA and Custom Forms 3
E Send a Reminder/Task to certain Email Recipient Using Outlook 5
Diane Poremsky Create Tasks from Email and move to different Task folders New Slipstick.com Articles 0
D Script to parse email and set a task Outlook VBA and Custom Forms 1
S Create task with email URL instead of attachment Outlook VBA and Custom Forms 4
J Format change when dragging from email to Task Using Outlook 1
A Create Task from Email and put body & email as attachment into task notes Outlook VBA and Custom Forms 17
H Create a Task by Draft-EMail > Task+Reminder not Working Outlook VBA and Custom Forms 1
L Creating a Task from Email and Attaching Original Email Outlook VBA and Custom Forms 6
I VBA Recurring Task Converted From Email Using Outlook 2
S Recurring Task / Delayed Email in Outlook 2010 Outlook Wishlist 10
C Visual Basic auto create task from email including attachments Using Outlook 9
M Send email on task creation? Using Outlook 2
T Email to Task Rule Using Outlook 2
S Tasks are handled through email but deleted email removes task history? Exchange Server Administration 3
B Error Code - Task 'Email removed for privacy' (0x80004005) : 'There is an error synchronizing your m Using Outlook 2
R Way to correct the window in focus when creating a task from email? Using Outlook 1
B Task 'Email removed for privacy - Sending and Receiving' reported error (0x8DE00006) : 'The operatio Using Outlook 1
S Task Email Attachment Shows Incorrect Email Subject, Opens Correct Email w/ Correct Subject Using Outlook 1
J __Task 'Email removed for privacy - Receiving' reported error (0x80048002) : 'This task was cancelle Using Outlook 2
M Flagged email messages don't show in task list Using Outlook 5
A Send Email to Task while sending to Email address Outlook VBA and Custom Forms 1
G Tasks - convert email to task & attach email Outlook VBA and Custom Forms 6
K send email when task changes Outlook VBA and Custom Forms 3
K Copy Entire Email Content - Paste into new Task Outlook VBA and Custom Forms 2
F Junk Email does not get added to the Blocked Sender List Using Outlook 0
B disappearing original when attach to email Using Outlook 1
W September 2020 - No Default Email Client message after Office Update Using Outlook 1
glnz O365 - How to combine the Inboxes for four email accounts into a single Inbox Using Outlook 7
glnz O365 - How to send from acct 2 but showing email name from acct 1 as From - alias? Using Outlook 1
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
D Move Email with Attachment to Folder Outlook VBA and Custom Forms 3
E Asking user to select multiple options in a list in an email Outlook VBA and Custom Forms 0
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
B Add ComboBox Value to Body of Email Outlook VBA and Custom Forms 1
A Backup Email Accounts On OutLook For Mac 2016 (Microsoft 365 subscription version) Using Outlook 0
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
G Inbox shows old email Using Outlook 3
L Email with correct To address but displaying name of a related person Using Outlook 0
A Two Inboxes, Same Email? Using Outlook 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
J To delete the draft email Using Outlook 2
B VBScript doesn't run on Recipient Email Outlook VBA and Custom Forms 2
J Autoreply email recieved from specific sender after deleting some text from body. Using Outlook 0
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2

Similar threads

Top