Use VBA to insert hyperlink in Task Body

  • Thread starter Thread starter Guest
  • Start date Start date
I can't duplicate the problem here. Since this statement has two objects in it:

TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink, _
SubAddress:="", ScreenTip:="", TextToDisplay:=FolderpathText, Target:=""

we can try to get the Range object in a separate statement to see if that's the problem:

Set rng = TaskSel.Range
TaskDoc.Hyperlinks.Add Anchor:=rng, Address:=FolderHyperlink, _
TextToDisplay:=FolderpathText
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


showme1946 said:
Sure. First of all, I got the text of the error backwards. It actually says
that the object referenced has become disconnected from its clients. It
doesn't say which object it is talking about. Here is the code:

'Create folder
Set NewFolder = Tasks.Folders.Add(TaskSubj, olFolderInbox)
FolderpathText = NewFolder.Application & ":" & NewFolder.Folderpath
'Create task
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display
'Create hyperlink
FolderHyperlink = Replace(FolderpathText, " ", "%20")
Set TaskInsp = NewTask.GetInspector
Set TaskDoc = TaskInsp.WordEditor
Set TaskSel = TaskDoc.Windows(1).Selection
TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink, _
SubAddress:="", ScreenTip:="", TextToDisplay:=FolderpathText, Target:=""
'All done

thanks again.

George.

Sue Mosher said:
I know nothing about that error and have never seen it. Could you show a little more code so we can try to reproduce the problem? It's necessary to know exactly how you're returning TaskDoc and TaskSel and where you've put NewTask.DIsplay.

showme1946 said:
Hi, Sue -
Yes, TaskSel and TAskDoc show as valid objects in the locals window.
However, the error has changed.

I relocated the NewTask.Display statement and got rid of the early binding
error. Now, this statement:

TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink, _
SubAddress:="", ScreenTip:="", TextToDisplay:=FolderpathText, Target:=""

Produces the following error: -2147417848; the text associated with this
error states that the client has become disconnected from its object. Also,
I have to reboot my computer, as it appears that memory gets assigned that is
not released and a bunch of things quit working (like, nothing happens when I
right-click on something). When I look up this error in the KB, I find
articles describing similar errors in Excel VB and statements that it is a
bug. But those articles reference older versions, so I am unclear about
whether those articles are pertinent to what I am experiencing with Outlook
2007.

Thanks for your help.

George.

:

If you look in the Locals window, do TaskSel and TaskDoc show as valid objects?

Hi, Sue
Oh, I guess I thoughe Dim obj as object was declaring them. The statement
that gets the error is:

TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink,
TexttoDisplay:=FolderpathText

:

Which statement raises the error?

Hi, Sue -
Thanks so much for your reply. I tried adding the reference, as I had not
done that; it did not fix the problem. I got your book, which is excellent
by the way, and it was very helpful, especially in confirming and clarifying
what you've said in this thread. However, using the code you suggested still
does not work. I get an error, with a long number preceded by a "-".

:

What do you mean by "won't recognize"? Did you add a reference to the Microsoft Word library to your project?

Hi, Sue -
[I've ordered your book, by the way] but until it comes I hope you can
help me further with this project. I have been trying the method you
describe below, but Outlook 2007 won't recognize the elements from the Word
object model. I assume there is something I need to do, but I havenot been
able to figure out what that is.

My code is:
Sub Main()
On Error GoTo TaskFolderCreate_err
'Declare variables
Dim ns As NameSpace
Dim Tasks As Folder
Dim NewTask As TaskItem
Dim NewFolder As Folder
Dim TaskInsp As Object
Dim TaskDoc As Object
Dim TaskSel As Object
Dim TaskSubj As String
Dim TaskDue As Date
Dim TaskCategory As String
Dim FolderpathText As String
Dim FolderHyperlink As String
'Initialize variables
Set ns = GetNamespace("MAPI")
Set Tasks = ns.GetDefaultFolder(olFolderTasks)
TaskSubj = ""
FolderpathText = ""
FolderHyperlink = ""
TaskDue = Now
'Get Name and Due Date Of Task and Folder
TaskSubj = InputBox(Prompt:="Enter Subject for Task and Folder:",
Title:="ENTER SUBJECT", Default:="")
If TaskSubj = "" Then GoTo TaskFolderCreate_err
TaskDue = InputBox(Prompt:="Enter Due Date for Task:", Title:="ENTER DUE
DATE", Default:=Now)
'Create folder
Set NewFolder = Tasks.Folders.Add(TaskSubj, olFolderInbox)
FolderpathText = NewFolder.Application & ":" & NewFolder.Folderpath
'Create task
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = FolderpathText
.Save
End With
NewTask.ShowCategoriesDialog
'Create hyperlink
FolderHyperlink = Replace(FolderpathText, " ", "%20")
Set TaskInsp = NewTask.GetInspector
Set TaskDoc = TaskInsp.WordEditor
Set TaskSel = TaskDoc.Windows(1).Selection
TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink,
TexttoDisplay:=FolderpathText
NewTask.Display
'All done


:

Outlook 2007 uses Word as the email editor for all items. Therefore, you can use the Document.Hyperlinks.Add method from the Word object model to insert a link in the body of a task, something along these lines:

strLink = Replace("outlook://Mailbox - Rickerson, George/Tasks/Testfolder", " ", "%20")
strLinkText = "George's mailbox"
Set objInsp = NewTask.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel - objDoc.Windows(1).Selection
objDoc.Hyperlinks.Add objSel.Range, strLink, _
"", "", strLinkText, ""

Pardon me for not noting earlier that I am using OL2007. I need a
way to accomplish this in OL07.
 
Durn it, I knew that would happen (I'm in IT - an exec, not a programmer -
but guess how often Oracle Support reports that they can't duplicate a bug?).
I do know the error is thrown by the "TaskDoc" statement, as I stepped
through each of the preceding statements one at a time with no error.

I will try your suggestion and see what happens. I also wondered if I
needed to specify a "target", although the documentation says it is optional.
I did already do something similar to what you suggest with the "Window"
object; didn't help.

Thanks, I will let you know what happens.

George.

Sue Mosher said:
I can't duplicate the problem here. Since this statement has two objects in it:

TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink, _
SubAddress:="", ScreenTip:="", TextToDisplay:=FolderpathText, Target:=""

we can try to get the Range object in a separate statement to see if that's the problem:

Set rng = TaskSel.Range
TaskDoc.Hyperlinks.Add Anchor:=rng, Address:=FolderHyperlink, _
TextToDisplay:=FolderpathText
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


showme1946 said:
Sure. First of all, I got the text of the error backwards. It actually says
that the object referenced has become disconnected from its clients. It
doesn't say which object it is talking about. Here is the code:

'Create folder
Set NewFolder = Tasks.Folders.Add(TaskSubj, olFolderInbox)
FolderpathText = NewFolder.Application & ":" & NewFolder.Folderpath
'Create task
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display
'Create hyperlink
FolderHyperlink = Replace(FolderpathText, " ", "%20")
Set TaskInsp = NewTask.GetInspector
Set TaskDoc = TaskInsp.WordEditor
Set TaskSel = TaskDoc.Windows(1).Selection
TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink, _
SubAddress:="", ScreenTip:="", TextToDisplay:=FolderpathText, Target:=""
'All done

thanks again.

George.

Sue Mosher said:
I know nothing about that error and have never seen it. Could you show a little more code so we can try to reproduce the problem? It's necessary to know exactly how you're returning TaskDoc and TaskSel and where you've put NewTask.DIsplay.

Hi, Sue -
Yes, TaskSel and TAskDoc show as valid objects in the locals window.
However, the error has changed.

I relocated the NewTask.Display statement and got rid of the early binding
error. Now, this statement:

TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink, _
SubAddress:="", ScreenTip:="", TextToDisplay:=FolderpathText, Target:=""

Produces the following error: -2147417848; the text associated with this
error states that the client has become disconnected from its object. Also,
I have to reboot my computer, as it appears that memory gets assigned that is
not released and a bunch of things quit working (like, nothing happens when I
right-click on something). When I look up this error in the KB, I find
articles describing similar errors in Excel VB and statements that it is a
bug. But those articles reference older versions, so I am unclear about
whether those articles are pertinent to what I am experiencing with Outlook
2007.

Thanks for your help.

George.

:

If you look in the Locals window, do TaskSel and TaskDoc show as valid objects?

Hi, Sue
Oh, I guess I thoughe Dim obj as object was declaring them. The statement
that gets the error is:

TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink,
TexttoDisplay:=FolderpathText

:

Which statement raises the error?

Hi, Sue -
Thanks so much for your reply. I tried adding the reference, as I had not
done that; it did not fix the problem. I got your book, which is excellent
by the way, and it was very helpful, especially in confirming and clarifying
what you've said in this thread. However, using the code you suggested still
does not work. I get an error, with a long number preceded by a "-".

:

What do you mean by "won't recognize"? Did you add a reference to the Microsoft Word library to your project?

Hi, Sue -
[I've ordered your book, by the way] but until it comes I hope you can
help me further with this project. I have been trying the method you
describe below, but Outlook 2007 won't recognize the elements from the Word
object model. I assume there is something I need to do, but I havenot been
able to figure out what that is.

My code is:
Sub Main()
On Error GoTo TaskFolderCreate_err
'Declare variables
Dim ns As NameSpace
Dim Tasks As Folder
Dim NewTask As TaskItem
Dim NewFolder As Folder
Dim TaskInsp As Object
Dim TaskDoc As Object
Dim TaskSel As Object
Dim TaskSubj As String
Dim TaskDue As Date
Dim TaskCategory As String
Dim FolderpathText As String
Dim FolderHyperlink As String
'Initialize variables
Set ns = GetNamespace("MAPI")
Set Tasks = ns.GetDefaultFolder(olFolderTasks)
TaskSubj = ""
FolderpathText = ""
FolderHyperlink = ""
TaskDue = Now
'Get Name and Due Date Of Task and Folder
TaskSubj = InputBox(Prompt:="Enter Subject for Task and Folder:",
Title:="ENTER SUBJECT", Default:="")
If TaskSubj = "" Then GoTo TaskFolderCreate_err
TaskDue = InputBox(Prompt:="Enter Due Date for Task:", Title:="ENTER DUE
DATE", Default:=Now)
'Create folder
Set NewFolder = Tasks.Folders.Add(TaskSubj, olFolderInbox)
FolderpathText = NewFolder.Application & ":" & NewFolder.Folderpath
'Create task
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = FolderpathText
.Save
End With
NewTask.ShowCategoriesDialog
'Create hyperlink
FolderHyperlink = Replace(FolderpathText, " ", "%20")
Set TaskInsp = NewTask.GetInspector
Set TaskDoc = TaskInsp.WordEditor
Set TaskSel = TaskDoc.Windows(1).Selection
TaskDoc.Hyperlinks.Add Anchor:=TaskSel.Range, Address:=FolderHyperlink,
TexttoDisplay:=FolderpathText
NewTask.Display
'All done


:

Outlook 2007 uses Word as the email editor for all items. Therefore, you can use the Document.Hyperlinks.Add method from the Word object model to insert a link in the body of a task, something along these lines:

strLink = Replace("outlook://Mailbox - Rickerson, George/Tasks/Testfolder", " ", "%20")
strLinkText = "George's mailbox"
Set objInsp = NewTask.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel - objDoc.Windows(1).Selection
objDoc.Hyperlinks.Add objSel.Range, strLink, _
"", "", strLinkText, ""

Pardon me for not noting earlier that I am using OL2007. I need a
way to accomplish this in OL07.
 
Hi, Sue -
The suggestion to isolate the range object did not work - the same error
was thrown when the hyperlink.add statement is executed.

Some additional info that might help: the task is created, and the body
of the task does contain the folderhyperlink string, and the string looks
like a hyperlink, i.e., it is blue and underlined. However, the body of the
task is inert - that is, one cannot edit it or do anything to it. One cannot
type in it, one gets no response to clicking on the hyperlink string or
right-clicking the string. Again however, if one selects "hyperlink" from
the ribbon, the hyperlink information appears in the hyperlink dialog box and
looks completely normal. Obviously the error prevents completion of some
process that makes the body of the text editable or actionable.

I am going to experiment with the hyperlink.add method in MS Word to see
if the same error is thrown there.

By the way, here is my complete code - I just can't believe you don't get
the same error, and I'm hoping you see something in the declarations or
somewhere that pertains to this: (in this code the hyperlink.add statement is
commented out)

Sub Main()
On Error GoTo TaskFolderCreate_err
'Declare variables
Dim ns As NameSpace
Dim Tasks As Folder
Dim NewTask As TaskItem
Dim NewFolder As Folder
Dim TaskSubj As String
Dim TaskDue As Date
Dim FolderpathText As String
' Dim FolderHyperlink As String
Dim TaskInsp As Object
Dim TaskDoc As Object
Dim TaskSel As Object
Dim TaskWindow As Object
Dim TaskRange As Object
'Initialize variables
Set ns = GetNamespace("MAPI")
Set Tasks = ns.GetDefaultFolder(olFolderTasks)
TaskSubj = ""
FolderpathText = ""
' FolderHyperlink = ""
TaskDue = Now
'Get Name and Due Date Of Task and Folder
TaskSubj = InputBox(Prompt:="Enter Subject for Task and Folder:",
Title:="ENTER SUBJECT", Default:="")
If TaskSubj = "" Then GoTo TaskFolderCreate_err
TaskDue = InputBox(Prompt:="Enter Due Date for Task:", Title:="ENTER DUE
DATE", Default:=Now)
'Create folder
Set NewFolder = Tasks.Folders.Add(TaskSubj, olFolderInbox)
FolderpathText = NewFolder.Application & ":" & NewFolder.Folderpath
'Create task
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = FolderpathText
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display
'Create hyperlink
' FolderHyperlink = Replace(FolderpathText, " ", "%20")
Set TaskInsp = NewTask.GetInspector
Set TaskDoc = TaskInsp.WordEditor
Set TaskWindow = TaskDoc.Windows(1)
Set TaskSel = TaskWindow.Selection
Set TaskRange = TaskSel.Range
' TaskDoc.Hyperlinks.Add Anchor:=TaskRange, Address:=FolderpathText, _
' SubAddress:="", ScreenTip:="", TextToDisplay:=FolderpathText, Target:=""
'All done
TaskFolderCreate_exit:
Set ns = Nothing
Set Tasks = Nothing
Set NewFolder = Nothing
Set NewTask = Nothing
Set TaskInsp = Nothing
Set TaskDoc = Nothing
Set TaskSel = Nothing
Set TaskRange = Nothing
Exit Sub
TaskFolderCreate_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: eMailtoTask" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume TaskFolderCreate_exit
End Sub
 
Your inserted links aren't working because you're using FolderpathText instead of FolderpathHyperlink for the Address parameter in your Hyperlinks.Add statement.

As for the error, this is too weird: Running your code, I saw the error you described just a couple of times, but every other time, I got an out-of-memory or disk space error. The main difference between your code and my test code, which worked fine 100% of the time, is that I was lazy and didn't declare all my variables, only Tasks. When I commented all your Dim statements except Dim Tasks, your code worked fine. So I worked my way back through all the Dim statements and found the surprising culprit:

Dim FolderpathText As String

Commenting out that statement made your code work without error every time. It also worked with just Dim FolderpathText without the As String modifer. I can't explain it. Try it and see if it works for you.

You also don't need this statement:

.Body = FolderpathText

because the text is inserted by the Hyperlinks.Add statement.
 
Hi, Sue -
Your discovery that declaring the Folderpathtext as String was the root
cause of the error fixed my program. In the course of trying to fix this I
also figured out that it was not necessary to declare a variable for the
Range object. In working on this I used your book (chapters 15 and
especially 17), and I used Word to record a macro in which I created a
hyperlink and then examined the VBA code that was generated.

It is safe to say, however, that it would never have occurred to me in a
million years that declaring the variable to be used as the address as string
would cause this problem.

Thanks for sticking with me through this, I really appreciate it.

George.
 
Hi, Sue -
One more thing: your first observation below is not actually correct. It
is not necessary to replace the spaces in the path of the folder with %20. I
am simply using the path of the folder as returned by Outlook as the address,
and it works fine - just like in your book!!

Thanks,
George.
 
Back
Top