Use VBA to insert hyperlink in Task Body

S

showme1946

Hello -
In Outlook I can drag an Outlook folder to a task, which results in a
hyperlink to the folder being inserted in the body of the task. How can I do
this programmatically in VBA (create the hyperlink to an Outlook folder)?

I have found articles about how to do this in an e-mail message and in
Access, but Outlook Task lacks the elements those have to make this work.

Assume that I've already created the task and the folder (the folder holds
mail and post items) programmatically and know their names; and I know the
path for the folder. The folder will hold e-mail and post items pertaining
to the task, and I want a link to the folder in the task so that when the
task is displayed, the user can immediately access the relevant email/post
folder.

All help greatly appreciated.

thanks
George Rickerson
 
S

showme1946

Hi, Michael -
Yes, the link looks like that, but the way it looks is not the problem.
If I set .body equal to "outlook://Mailbox - Rickerson, George/Tasks/a test
task", all I get in the task is that text string. It is not a hyperlink that
I can click on to open that folder.

I need for the end result to be the same as it is when, in the Outlook UI,
I click and drag a folder to a task. If you do that, you will see that
Outlook puts a hyperlink in the body of the task that can be used to open the
folder; the hyperlink can also be edited using the hyperlink dialog box.
However, I am unable to find any way to mimic this behavior in a VB program.

I learned somewhere while searching for a solution that a hyperlink is 3
text strings separated by the "#" character: the display string, the address
string and the subaddress string. So I constructed such a string (basically
it is the two text strings you see if you open the hyperlink dialog box) and
set .body equal to it; but all I got was the text string - the whole thing,
all 3 parts, with the delimiters; Outlook did not recognize it or treat it as
a hyperlink.

I learned that one can get a hyperlink into an e-mail message using the
htmlbody property and constructing a html text string; the htmlbody is not
available for Tasks. One MS MVP on some forum suggested, in answer to
another person's question, using the EntryID of the folder; all I got was a
really ugly text string in the Task body consisting of the EntryID.

Hope this additional information helps me find someone who knows the answer.

thanksl
george.
 
S

showme1946

Michael - if you do a search for "email hyperlink" and read the thread dated
6/28/07 you'll see successful instructions for programmatically putting a
hyperlink in the body of an e-mail using htmlbody. I basically want to do
the same think in the body of a Task, but htmlbody isn't available for tasks.
I have tried the method described in the 6/28/07 thread (except for the
htmlbody part of course) and it doesn't work.

thanks
George.
 
S

showme1946

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink. So, when you say "works great here" are
you saying than when you execute your code you have a hyperlink in your task?

thanks
George.
 
S

showme1946

I see. Pardon me for not noting earlier that I am using OL2007. I need a
way to accomplish this in OL07.

thanks
George.
 
S

Sue Mosher [MVP-Outlook]

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, ""

--
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:
Pardon me for not noting earlier that I am using OL2007. I need a
way to accomplish this in OL07.
Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:
 
S

showme1946

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
 
S

Sue Mosher [MVP-Outlook]

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

--
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:
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


Sue Mosher said:
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, ""


Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:
 
S

showme1946

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 "-". When I
search that message number in the knowledgebase, I get results which say it
is a recognized bug and has to do with early and late binding. It says I can
fix the problem by using early binding.

Since my process does not involve a custom form or controls, I have not so
far been able to figure out how to use early binding to solve the problem.
Again, your book is great, but everything it says about binding has to do
with controls on forms.

thanks
George.

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

--
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:
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


Sue Mosher said:
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.

Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink.
 
S

Sue Mosher [MVP-Outlook]

Which statement raises the error?

"Early binding" is not an Outlook concept but a basic VBA concent. It involves declaring all your object variables explicitly, rather than just using Dim or Dim obj As Object.

--
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:
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 "-". When I
search that message number in the knowledgebase, I get results which say it
is a recognized bug and has to do with early and late binding. It says I can
fix the problem by using early binding.

Since my process does not involve a custom form or controls, I have not so
far been able to figure out how to use early binding to solve the problem.
Again, your book is great, but everything it says about binding has to do
with controls on forms.

thanks
George.

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

showme1946 said:
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.

Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink.
 
S

showme1946

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

thanks
George.

Sue Mosher said:
Which statement raises the error?

"Early binding" is not an Outlook concept but a basic VBA concent. It involves declaring all your object variables explicitly, rather than just using Dim or Dim obj As Object.

--
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:
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 "-". When I
search that message number in the knowledgebase, I get results which say it
is a recognized bug and has to do with early and late binding. It says I can
fix the problem by using early binding.

Since my process does not involve a custom form or controls, I have not so
far been able to figure out how to use early binding to solve the problem.
Again, your book is great, but everything it says about binding has to do
with controls on forms.

thanks
George.

Sue Mosher said:
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.

Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink.
 
S

Sue Mosher [MVP-Outlook]

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

--
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:
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

thanks
George.

Sue Mosher said:
Which statement raises the error?

"Early binding" is not an Outlook concept but a basic VBA concent. It involves declaring all your object variables explicitly, rather than just using Dim or Dim obj As Object.

showme1946 said:
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 "-". When I
search that message number in the knowledgebase, I get results which say it
is a recognized bug and has to do with early and late binding. It says I can
fix the problem by using early binding.

Since my process does not involve a custom form or controls, I have not so
far been able to figure out how to use early binding to solve the problem.
Again, your book is great, but everything it says about binding has to do
with controls on forms.

thanks
George.

:

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.

Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink.
 
S

showme1946

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.

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

--
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:
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

thanks
George.

Sue Mosher said:
Which statement raises the error?

"Early binding" is not an Outlook concept but a basic VBA concent. It involves declaring all your object variables explicitly, rather than just using Dim or Dim obj As Object.

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 "-". When I
search that message number in the knowledgebase, I get results which say it
is a recognized bug and has to do with early and late binding. It says I can
fix the problem by using early binding.

Since my process does not involve a custom form or controls, I have not so
far been able to figure out how to use early binding to solve the problem.
Again, your book is great, but everything it says about binding has to do
with controls on forms.

thanks
George.

:

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.

Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink.
 
S

Sue Mosher [MVP-Outlook]

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.

--
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:
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.

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

showme1946 said:
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.

Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink.
 
S

showme1946

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.

--
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:
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.

Sue Mosher said:
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.

Am Tue, 4 Sep 2007 11:52:03 -0700 schrieb showme1946:

Hmmm, perhaps I should move to your location. When I execute this code:
Set NewTask = CreateItem(olTaskItem)
With NewTask
.Subject = TaskSubj
.DueDate = TaskDue
.Body = "<outlook://Mailbox - Rickerson, George/Tasks/Testfolder>"
.Save
End With
NewTask.ShowCategoriesDialog
NewTask.Display

The result in the body of NewTask is a text string (including the angle
brackets) that is not a hyperlink.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top