Add Hyperlink to Task

Status
Not open for further replies.

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I'm trying to use VBA to add a hyperlink to a file on a server into an Outlook task body but can't get this to work. It seems the .htmlbody which works in messages doesn't work in tasks. Any ideas?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Try this to add the hyperlink

Code:
Dim objInsp As Inspector
Dim objDoc As Word.Document
Dim objSel As Word.Selection

Set objInsp = objTask.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
objDoc.Hyperlinks.Add objSel.Range, strLink, "", "", strLinkText, ""
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Thanks Diane. I tried your code but get a compile error for the following line of code

Code:
Dim objDoc As word.document

What I am trying to do is task.body = hyperlink
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I managed to progress slightly further by enabling Microsoft Word 15.0 Object Library. However, I now get a cpile error at the following line of code

Code:
Set objInsp = objTask.GetInspector

I am using Outlook VBA to create the task and add the hyperlink rather than having an existing task and adding the hyperlink to that. Not sure whether this would make any difference or not?
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Thought it would be easier to post the full code I'm using.

I declared obJTask as Outlook.TaskItem as I think that was one of the issues I was having.

The code below works fine and creates a new task if the chknewtask value is true. However when I uncomment the lines of code you have suggested it doesn't work at all.

Code:
Sub MsgSaver(strPath As String, msgItem As Outlook.MailItem, lngCounter As Long, blnMulti As Boolean)

  Dim intC As Integer
  Dim intD As Integer
  Dim strMsgSubj As String
  Dim strMsgTo As String
  Dim arrMsgTo() As String ' Added by Alan McGowan 6-11-15
  Dim msgItemTo As String ' Added by Alan McGowan 6-11-15
  Dim arrcount As Long ' Added by Alan McGowan 6-11-15
  Dim msglink As String ' Added by Alan McGowan 17-11-15
  Dim msg As Outlook.MailItem ' Added by Alan McGowan 17-11-15
  Dim objTask As Outlook.TaskItem ' Added by Alan McGowan 20-11-15
  Dim Signature As String ' Added by Alan McGowan 17-11-15
'  Dim objInsp As Inspector ' Added by Alan McGowan 17-11-15
'  Dim objDoc As Word.document ' Added by Alan McGowan 17-11-15
'  Dim objSel As Word.Selection ' Added by Alan McGowan 17-11-15
   
'  Set objInsp = objTask.GetInspector ' Added by Alan McGowan 20-11-15
'  Set objDoc = objInsp.WordEditor ' Added by Alan McGowan 20-11-15
'  Set objSel = objDoc.Windows(1).Selection ' Added by Alan McGowan 20-11-15
  
' Set msgitem.to to the first recipient if multiple recipients in to field
' Added by Alan McGowan 6-11-15
  arrMsgTo = Split(msgItem.To, ";")
  arrcount = UBound(arrMsgTo) - LBound(arrMsgTo) + 1
  If arrcount = 1 Then
     msgItemTo = msgItem.To
     Else
     msgItemTo = arrMsgTo(0) & " et al" ' uses the first recipient where there is multiple recipients
  End If
'Set name to save message to
If UserForm1.CheckBox1.Value = True Then 'multiple emails being saved using a user specified name
     If UserForm1.g_blnOutgoing = True Then
        strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[To " & msgItemTo & "]" & " " & UserForm1.TextBox9.Value & "_0" & lngCounter & ".msg"
        Else 'incoming mail so use from field
        strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[From " & msgItem.SenderName & "]" & " " & UserForm1.TextBox9.Value & "_0" & lngCounter & ".msg"
    End If

ElseIf blnMulti = True Then 'multiple emails being saved using the default name
    strMsgSubj = msgItem.Subject
    If UserForm1.g_blnOutgoing = True Then
        strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[To " & msgItemTo & "]" & " " & strMsgSubj & ".msg"
    Else 'incoming mail so use from field
        strMsgSubj = Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[From " & msgItem.SenderName & "]" & " " & strMsgSubj & ".msg"
    End If
Else 'single email using name given in textbox8
   strMsgSubj = UserForm1.TextBox8.Value & ".msg"
End If
   ' cleans illegal characters from strMsgSubj. Added by Alan McGowan 6-11-15
clean_subj strMsgSubj
  
   'Save new msg file to defined filename and location
  msgItem.SaveAs strPath & strMsgSubj
    
UserForm1.Hide

' create notification email if option to do is selected. Added by Alan McGowan 17-11-15
If UserForm1.chkmarknotif.Value = True Then
    Set msg = Application.CreateItem(olMailItem)
    msg.Subject = "*** A Saved Email Requires Your Attention ***"
    msg.Importance = olImportanceHigh

    'get default signature
        Signature = Environ("appdata") & "\Microsoft\Signatures\"
        If Dir(Signature, vbDirectory) <> vbNullString Then
        Signature = Signature & Dir$(Signature & "*.htm")
        Else:
        Signature = ""
        End If
        Signature = CreateObject("Scripting.FileSystemObject").GetFile(Signature).OpenAsTextStream(1, -2).ReadAll
    msg.HTMLBody = "<p style='font-size:12pt; font-family:calibri;'>" & "From: " & msgItem.sender & "<br>" & "Subject: " & msgItem.Subject & "<br>" & _
    "Received: " & Format(msgItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & "<p style='font-size:12pt; font-family:calibri;'>" & "Email location: " & "<a href=""" & _
    strPath & """>" & strPath & "</a > " & "<br>" & "Link to message: " & "<a href=""" & strPath & strMsgSubj & _
    """>" & strPath & strMsgSubj & "</a > " & "<p style='font-size:12pt; font-family:calibri;'>" & _
    "An email requires your attention. Please review the message link above and action as appropriate. Thank you." & "<p>" & Signature & "</p>"
    msg.Display

End If

' create new task if option to do is selected. Added by Alan McGowan 17-11-15
If UserForm1.chknewtask.Value = True Then
    Set objTask = Application.CreateItem(olTaskItem)
    objTask.Subject = msgItem.Subject
    objTask.DueDate = Now + 3 ' sets due date to 3 days after saving email
    objTask.ReminderSet = True
    objTask.ReminderTime = Now + 2 ' sets reminder to 2 days after saving email
    objTask.body = msgItem.body
'    objDoc.Hyperlinks.Add objSel.Range, strPath & strMsgSubj, "", "", strPath & strMsgSubj, ""
    objTask.Importance = olImportanceHigh
    objTask.Display

End If

Set msg = Nothing
Set msgItem = Nothing
Set objTask = Nothing

End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
this is the sample i used to make sure it worked in tasks:

Code:
Sub ConvertMessagetoTask()
    Dim objTask As Outlook.TaskItem
    Dim objMail As Outlook.MailItem
    Dim strID As String
    Dim strLink, strLinkText As String

For Each objMail In Application.ActiveExplorer.Selection

    strID = objMail.EntryID
    strLink = "outlook:" & strID
    strLinkText = objMail.Subject

Set objTask = Application.CreateItem(olTaskItem)
With objTask
    .Subject = objMail.subject
    .DueDate = objMail.ReceivedTime + 3
    .StartDate = objMail.ReceivedTime + 2
    .Body = objMail.Body
    .Categories = "my category"
 
Dim objInsp As Inspector
Dim objDoc As Word.Document
Dim objSel As Word.Selection

Set objInsp = objTask.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
objDoc.Hyperlinks.Add objSel.Range, strLink, "", "", strLinkText, ""


    .Display
End With

    Next
    Set objTask = Nothing
    Set objMail = Nothing
End Sub


I'm not able to fully test your macro because it's complicated and I don't have some of the files, but this is the stripped version of your macro - it creates a message then a task - the hyperlink insertion can either before or after you display it.


Code:
Sub MsgSaver()

  Dim intC As Integer
  Dim intD As Integer
  Dim strMsgSubj As String
  Dim strMsgTo As String
  Dim arrMsgTo() As String ' Added by Alan McGowan 6-11-15
  Dim msgItemTo As String ' Added by Alan McGowan 6-11-15
  Dim arrcount As Long ' Added by Alan McGowan 6-11-15
  Dim msglink As String ' Added by Alan McGowan 17-11-15
  Dim msg As Outlook.MailItem ' Added by Alan McGowan 17-11-15
  Dim objTask As Outlook.TaskItem ' Added by Alan McGowan 20-11-15
  Dim Signature As String ' Added by Alan McGowan 17-11-15

' create notification email if option to do is selected. Added by Alan McGowan 17-11-15
'If UserForm1.chkmarknotif.Value = True Then
  Set msg = Application.CreateItem(olMailItem)
  msg.Subject = "*** A Saved Email Requires Your Attention ***"
  msg.Importance = olImportanceHigh

  'get default signature
  Signature = Environ("appdata") & "\Microsoft\Signatures\"
  If Dir(Signature, vbDirectory) <> vbNullString Then
  Signature = Signature & Dir$(Signature & "*.htm")
  Else:
  Signature = ""
  End If
  
  msg.Display

'End If

' create new task if option to do is selected. Added by Alan McGowan 17-11-15
'If UserForm1.chknewtask.Value = True Then

  Dim strLinkText As String
  msglink = "http://www.slipstick.com"
  strLinkText = msg.Subject
  
  
  Set objTask = Application.CreateItem(olTaskItem)
  With objTask
  .Subject = msg.Subject
  .DueDate = Now + 3 ' sets due date to 3 days after saving email
  .ReminderSet = True
  .ReminderTime = Now + 2 ' sets reminder to 2 days after saving email
  .Body = msg.Body
  
  
 Dim objInsp As Inspector
 Dim objDoc As Word.Document
 Dim objSel As Word.Selection

  Set objInsp = objTask.GetInspector
 Set objDoc = objInsp.WordEditor
 Set objSel = objDoc.Windows(1).Selection
 objDoc.Hyperlinks.Add objSel.Range, msglink, "", "", strLinkText, ""
 
  .Importance = olImportanceHigh
  .Display

End With
'End If

Set msg = Nothing
Set msgItem = Nothing
Set objTask = Nothing

End Sub
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Unfortunately I still seem to be having problems. I used your code above that creates the task which compiles fine but when I try and run the macro I still get an error.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
the error in your macro is still on this line? Set objInsp = objTask.GetInspector Try moving important and display lines right after body.
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I'm a dummy!

I've just realized that in your version of my code you were using msg.body and msg.subject whereas it should have been msgitem.body and msgitem.subject

Changing this has got it to work. Is there anyway of adding some line breaks after it inserts the hyperlink?

Thanks for your help
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Sorted the line break issue by adding vbNewLine before msgitem.body
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Heh, i hit that problem too. :) Yeah, either VBnewline (or vbcrlf) or use word's InsertParagraphBefore to add a line break before the hyperlink is added.

objSel.Range.InsertParagraphBefore
objDoc.Hyperlinks.Add objSel.Range, msglink, "", "", strLinkText, ""
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P Can't add a custom hyperlink to toolbar in OL 2010 Using Outlook 1
K Add Hyperlink in Email Body by VBA Outlook VBA and Custom Forms 1
BartH Add a string to the conditions in .Conditions.BodyOrSubject.Text Outlook VBA and Custom Forms 2
A "Get Add-Ins" - Which Version of Outlook to use Using Outlook 1
D Do I need Exchange Add-In? Using Outlook 6
C-S-R Manage Add-ins (Remove Wunderlist) Using Outlook 3
A iCloud add in problems Using Outlook 4
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 8
C Looking for feedback on new Outlook Add-in Using Outlook 0
L isn't there an OL add-on that flags addressee before sending Using Outlook 3
S Add VBA save code Using Outlook 0
P Shortcut Pane - add shortcut to Office365 group mailbox Using Outlook 1
B Add ComboBox Value to Body of Email Outlook VBA and Custom Forms 1
G How to add a folder shortcut to outlook quick access toolbar? Using Outlook 6
G Add to Outlook Contacts - Point to non-default contacts folder Using Outlook 0
M Automatically add senders first name to a greeting Outlook VBA and Custom Forms 1
C Add Form to Appointments Received, Automatically Outlook VBA and Custom Forms 6
O Outlook tasks - Add text column with multiple lines Using Outlook 3
W April 2020 Office 365 Update - Add-Ons fail after Office 365 Update Using Outlook 6
B Task Filter Not Working When I add too many criteria Using Outlook 0
D Add date next to day name in Outlook Today calendar view Using Outlook 1
D iCloud Add-in not working in Outlook 2013 and Outlook 2016 After Windows Upgrade & iCloud Upgrade Using Outlook 2
P Add Paste Unformatted to QAT Using Outlook 1
M Ignore slow add-ins Using Outlook 0
B Add Prefix text to Subject Line Using Outlook 1
Jennifer Murphy Add birthdays to calendar Using Outlook 7
Z Add text to auto-forwarded e-mail Outlook VBA and Custom Forms 4
P Add, remove, & reorder folder pane Using Outlook 6
L Tired of Outlook disabling Adobe PDF Creator add-in Using Outlook 1
R Add 'Company' to Select Names Form Using Outlook 1
A Outlook 2016 Web add-in missing on some machines Using Outlook 9
P Syncing problems with add-ins Using Outlook 3
Mark Foley Cannot enable add-in in outlook 2010 Using Outlook 0
BretAB Is it possible to add a lookup field to a Message form? Outlook VBA and Custom Forms 4
J Add an Attachment Using an Array and Match first 17 Letters to Matching Template .oft to Send eMail Outlook VBA and Custom Forms 2
iwshim outlook 2013 - I cannot see the "Manage Add-ins" Using Outlook 2
A Add multiple servers "on behalf of" email to "safe senders" list. Using Outlook 1
S Add Exchange Account as Secondary to Existing PST? Exchange Server Administration 1
C Macro to add multiple recipients to message Outlook VBA and Custom Forms 3
A Add to Outlook Contacts from email - default view Outlook VBA and Custom Forms 1
P Add a contact to the New Task in Outlook 2016 Using Outlook 2
e_a_g_l_e_p_i Is there a way to add something that is in the "Format Text" tab to the "basic Text" on the message tab Using Outlook 1
B When working on emails in a certain folder, when I hit reply or reply all, I would like it re always reply all and add an email address to send to Outlook VBA and Custom Forms 3
M Macro to add date/time stamp to subject Outlook VBA and Custom Forms 4
O Outlook on Android - add BCC Using Outlook 2
J Execute Add-In Button from VBA Outlook 2016 Outlook VBA and Custom Forms 1
S Example VBA Macro - To Conditionally Change the From Account and Add a BCC Address on Emails Outlook VBA and Custom Forms 11
B When I add more search strings to RULES, it is not processing them Using Outlook 3
D Add Tetxbox at form open Outlook VBA and Custom Forms 1
Andrew Quirl Open attachment, manipulate without add-on program? Outlook VBA and Custom Forms 5

Similar threads

Top