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?
 
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, ""
 
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
 
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?
 
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
 
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
 
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.
 
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
 
Sorted the line break issue by adding vbNewLine before msgitem.body
 
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
T Add Account Using Outlook 1
G Add Map It button to Custom Contacts Form in Outlook Outlook VBA and Custom Forms 1
G Outlook 2021 Add Picture to Custom Contact Form Outlook VBA and Custom Forms 2
P Can't add custom field to custom Outlook form, it always adds to the Folder instead Outlook VBA and Custom Forms 2
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
S Outlook 2019 Custom outlook Add-in using Visual Studio Outlook VBA and Custom Forms 0
G automatically choosing "add to autocorrect" option Using Outlook 0
F Want to add second email to Outlook for business use Using Outlook 4
K Add an entry to a specific calendar Using Outlook 1
F Add a category before "Send an Email When You Add an Appointment to Your Calendar" Outlook VBA and Custom Forms 0
J Want to create a button on the nav bar (module add-in) to run code Outlook VBA and Custom Forms 2
M "Attachment Detacher for Outlook" add in, does it update the server copy of the email? Using Outlook 1
J Outlook 365 Add keywords from Listbox to the message body where cursor is placed Outlook VBA and Custom Forms 0
O Add Day Number of the year for 2023-2033 Outlook VBA and Custom Forms 5
J GoDaddy migrated to Office365 - Outlook Wont Add Account Exchange Server Administration 21
F Outlook 2019 Outlook 2019 Add and Sync to New computer Comcast server Using Outlook 2
Witzker Add a text line at the end of the note field in all selected Contacts Outlook VBA and Custom Forms 7
A iCloud Outlook Add In is causing Outlook 2021 to crash and got disabled Using Outlook 10
N How to add or delete items to Move dropdown Menu Using Outlook 0
G Add contacts birthday to calendar Using Outlook 4
V How to add 'Previous Item' and 'Next Item' to the Quick Access Toolbar Using Outlook 1
Commodore Safe way to add or update holidays; Windows Notifications issue Using Outlook 8
kkqq1122 How would I add Search for attachment name Outlook VBA and Custom Forms 3
L did MS ever add way to text via Outlook Using Outlook 5
P How to add a column named categories when searching in Outlook Using Outlook 0
M add new attendee to existing meetings with VBA Outlook VBA and Custom Forms 5
N Can't create NEW GROUP and add/remove a member from existing Group in Outlook Using Outlook 1
Witzker Outlook 2019 Pls. add a Prefix for OUTLOOK 2019 here Using Outlook 1
P Add inanimate objects to meetings? Using Outlook 1
O Outlook 2010 Add delete button to the side of the message list Using Outlook 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 6
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 17
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
Z 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
Z Task Filter Not Working When I add too many criteria Using Outlook 0

Similar threads

Back
Top