Insert a Signature via Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I insert a signature into an item via code? I have a VBScript which
automatically inserts a custom message into the item and need to be able to
add the signature of the current user. Unfortunately, the message cannot
contain any signature.
 
Actually, this all pertains to the question a week or so ago about message
templates. The solution that I'm implementing invovles building a custom
Outlook form with VBScript to insert message specific text into the body of
the message. The message is marked up with tags (i.e. <%Show_Name%>) that
correspond to user fields on the form. When the user clicks on a command
button, VBScript code cycles through the custom fields and uses Replace() to
insert the values into the message text. The specific problem with the
Signatures is that when the code runs, any hyperlink is converted to read
'HYPERLINK <URL> <Link Text>'. I've added another button that will take the
current message body and save it to a *.txt file for use as the form's
template message when the form is initially opened. Thought you might be
interested.
 
Any code to work with message bodies needs to be sensitive to the value of MailItem.BodyFormat and work with the Body or HTMLBody property as appropriate.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
Its very strange because I only thought to use the .Body property, however as
I flip between TEXT, RTF, and HTML formats the text is updated properly
within Outlook and when I view sent mail via a web browser.
 
"flip between ... formats" how? If you change format manually with Word as the editor, Outlook handles the formatting change. Never assume that something you can do manually works exactly the same way in code.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
Wasn't aware of that.

I did just run a test to confirm wether or not the line of code below
updates the .Body property as well as .HTMLBody.

Item.Body = Replace(Item.Body, objControls(i).name, objControls(i).value)

The test was conducted by putting a STOP statement in the in the ITEM_SEND
event and then inspecting the .Body and .HTMLBody properties via the debug
window. ?ITEM.BODY and ?ITEM.HTML
Both properties were updated correctly regardless of the value in the
MESSAGE FORMAT set in the MAIL FORM tab of the OPTIONS screen.
 
But what format was the actual message? The default format set in Tools | Options isn't relevant once you're dealing with a message that already exists.

You also haven't mentioned your Outlook version.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
So I should also be snooping around the .BodyFormat property?

Absolutely, if you care about formatting of the signature! Only if you know the message format can you know whether you can insert a formatted signature or a plain-text signature and what approach would be appropriate. If you have an HTML-format message, you would have to insert fully tagged HTML -- including the <a> link for a hyperlink -- into the HTMLBody property at the appropriate location.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
It might help if I remembered which specific thread this is. I gave up trying
to code inserting a signature since there was no way to know how the
signature was named ('Untitled', 'Full Signature', etc.) given that multiple
users could have have saved it any of a number ways.

What I did discover is that when the Replace() executes on the .Body
property if the message body contained a hyperlink, the hyper link was
converted from

[www.microsoft.com]

to

[HYPERLINK "www.microsoft.com" www.microsoft.com]

which of course is undesireable. The simple fix was to not have hyperlinks
in the signature that is specifically marked up as us. If its an email
address, the solution was to use the mailto: prefix.
 
I wouldn't do a replace on Body unless I knew from BodyFormat that it's a plain text message or an RTF message whose formatting I don't care about.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


dch3 said:
It might help if I remembered which specific thread this is. I gave up trying
to code inserting a signature since there was no way to know how the
signature was named ('Untitled', 'Full Signature', etc.) given that multiple
users could have have saved it any of a number ways.

What I did discover is that when the Replace() executes on the .Body
property if the message body contained a hyperlink, the hyper link was
converted from

[www.microsoft.com]

to

[HYPERLINK "www.microsoft.com" www.microsoft.com]

which of course is undesireable. The simple fix was to not have hyperlinks
in the signature that is specifically marked up as us. If its an email
address, the solution was to use the mailto: prefix.

Sue Mosher said:
Absolutely, if you care about formatting of the signature! Only if you know the message format can you know whether you can insert a formatted signature or a plain-text signature and what approach would be appropriate. If you have an HTML-format message, you would have to insert fully tagged HTML -- including the <a> link for a hyperlink -- into the HTMLBody property at the appropriate location.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
I'm assuming that you would avoid Replace() if the BodyFormat is HTML due to
the possiblity of inadvertently wacking out/screwing up the tags?

Sue Mosher said:
I wouldn't do a replace on Body unless I knew from BodyFormat that it's a plain text message or an RTF message whose formatting I don't care about.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


dch3 said:
It might help if I remembered which specific thread this is. I gave up trying
to code inserting a signature since there was no way to know how the
signature was named ('Untitled', 'Full Signature', etc.) given that multiple
users could have have saved it any of a number ways.

What I did discover is that when the Replace() executes on the .Body
property if the message body contained a hyperlink, the hyper link was
converted from

[www.microsoft.com]

to

[HYPERLINK "www.microsoft.com" www.microsoft.com]

which of course is undesireable. The simple fix was to not have hyperlinks
in the signature that is specifically marked up as us. If its an email
address, the solution was to use the mailto: prefix.

Sue Mosher said:
So I should also be snooping around the .BodyFormat property?

Absolutely, if you care about formatting of the signature! Only if you know the message format can you know whether you can insert a formatted signature or a plain-text signature and what approach would be appropriate. If you have an HTML-format message, you would have to insert fully tagged HTML -- including the <a> link for a hyperlink -- into the HTMLBody property at the appropriate location.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Outlook 2003 SP2

So I should also be snooping around the .BodyFormat property?

:

But what format was the actual message? The default format set in Tools | Options isn't relevant once you're dealing with a message that already exists. >>
Wasn't aware of that.

I did just run a test to confirm wether or not the line of code below
updates the .Body property as well as .HTMLBody.

Item.Body = Replace(Item.Body, objControls(i).name, objControls(i).value)

The test was conducted by putting a STOP statement in the in the ITEM_SEND
event and then inspecting the .Body and .HTMLBody properties via the debug
window. ?ITEM.BODY and ?ITEM.HTML
Both properties were updated correctly regardless of the value in the
MESSAGE FORMAT set in the MAIL FORM tab of the OPTIONS screen.

:

"flip between ... formats" how? If you change format manually with Word as the editor, Outlook handles the formatting change. Never assume that something you can do manually works exactly the same way in code.


Its very strange because I only thought to use the .Body property, however as
I flip between TEXT, RTF, and HTML formats the text is updated properly
within Outlook and when I view sent mail via a web browser.

:

Any code to work with message bodies needs to be sensitive to the value of MailItem.BodyFormat and work with the Body or HTMLBody property as appropriate.



Actually, this all pertains to the question a week or so ago about message
templates. The solution that I'm implementing invovles building a custom
Outlook form with VBScript to insert message specific text into the body of
the message. The message is marked up with tags (i.e. <%Show_Name%>) that
correspond to user fields on the form. When the user clicks on a command
button, VBScript code cycles through the custom fields and uses Replace() to
insert the values into the message text. The specific problem with the
Signatures is that when the code runs, any hyperlink is converted to read
'HYPERLINK <URL> <Link Text>'. I've added another button that will take the
current message body and save it to a *.txt file for use as the form's
template message when the form is initially opened. Thought you might be
interested.

:

See http://www.outlookcode.com/codedetail.aspx?id=615 for a VBA sample that you should be able to adapt.

Note, though, that your requiremens "need to be able to add the signature of the current user" and "the message cannot contain any signature" are mutually exclusive.

How can I insert a signature into an item via code? I have a VBScript which
automatically inserts a custom message into the item and need to be able to
add the signature of the current user. Unfortunately, the message cannot
contain any signature.
 
Exactly.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


dch3 said:
I'm assuming that you would avoid Replace() if the BodyFormat is HTML due to
the possiblity of inadvertently wacking out/screwing up the tags?

Sue Mosher said:
I wouldn't do a replace on Body unless I knew from BodyFormat that it's a plain text message or an RTF message whose formatting I don't care about.

dch3 said:
It might help if I remembered which specific thread this is. I gave up trying
to code inserting a signature since there was no way to know how the
signature was named ('Untitled', 'Full Signature', etc.) given that multiple
users could have have saved it any of a number ways.

What I did discover is that when the Replace() executes on the ..Body
property if the message body contained a hyperlink, the hyper link was
converted from

[www.microsoft.com]

to

[HYPERLINK "www.microsoft.com" www.microsoft.com]

which of course is undesireable. The simple fix was to not have hyperlinks
in the signature that is specifically marked up as us. If its an email
address, the solution was to use the mailto: prefix.

:

So I should also be snooping around the .BodyFormat property?

Absolutely, if you care about formatting of the signature! Only if you know the message format can you know whether you can insert a formatted signature or a plain-text signature and what approach would be appropriate. If you have an HTML-format message, you would have to insert fully tagged HTML -- including the <a> link for a hyperlink -- into the HTMLBody property at the appropriate location.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Outlook 2003 SP2

So I should also be snooping around the .BodyFormat property?

:

But what format was the actual message? The default format set in Tools | Options isn't relevant once you're dealing with a message that already exists. >>
Wasn't aware of that.

I did just run a test to confirm wether or not the line of code below
updates the .Body property as well as .HTMLBody.

Item.Body = Replace(Item.Body, objControls(i).name, objControls(i).value)

The test was conducted by putting a STOP statement in the in the ITEM_SEND
event and then inspecting the .Body and .HTMLBody properties via the debug
window. ?ITEM.BODY and ?ITEM.HTML
Both properties were updated correctly regardless of the value in the
MESSAGE FORMAT set in the MAIL FORM tab of the OPTIONS screen.

:

"flip between ... formats" how? If you change format manually with Word as the editor, Outlook handles the formatting change. Never assume that something you can do manually works exactly the same way in code.


Its very strange because I only thought to use the .Body property, however as
I flip between TEXT, RTF, and HTML formats the text is updated properly
within Outlook and when I view sent mail via a web browser.

:

Any code to work with message bodies needs to be sensitive to the value of MailItem.BodyFormat and work with the Body or HTMLBody property as appropriate.



Actually, this all pertains to the question a week or so ago about message
templates. The solution that I'm implementing invovles building a custom
Outlook form with VBScript to insert message specific text into the body of
the message. The message is marked up with tags (i.e. <%Show_Name%>) that
correspond to user fields on the form. When the user clicks on a command
button, VBScript code cycles through the custom fields and uses Replace() to
insert the values into the message text. The specific problem with the
Signatures is that when the code runs, any hyperlink is converted to read
'HYPERLINK <URL> <Link Text>'. I've added another button that will take the
current message body and save it to a *.txt file for use as the form's
template message when the form is initially opened. Thought you might be
interested.

:

See http://www.outlookcode.com/codedetail.aspx?id=615 for a VBA sample that you should be able to adapt.

Note, though, that your requiremens "need to be able to add the signature of the current user" and "the message cannot contain any signature" are mutually exclusive.

How can I insert a signature into an item via code? I have a VBScript which
automatically inserts a custom message into the item and need to be able to
add the signature of the current user. Unfortunately, the message cannot
contain any signature.
 
The replace() looks for a word that begins and ends with specific characters
specifically <% and %> as in <%Booth%> or <%Company_Name%>, I wasn't thinking
neccessarily of ASP when I choose to use the percent and bracket combination
- I just needed a combination that was very highly unlikely to ever appear in
a message. To keep the code vanilla, I named the form fields using the <% and
%> prefix and suffix. The code which inserts the values into the message body
loops through the controls using

Set objControls = Item.GetInspector.ModifiedFormPages("Invoice
Request").Controls

and then uses the control name to replace the text as in...

Item.Body = Replace(Item.Body, objControls(i).name, objControls(i).value)

David H

Sue Mosher said:
Exactly.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


dch3 said:
I'm assuming that you would avoid Replace() if the BodyFormat is HTML due to
the possiblity of inadvertently wacking out/screwing up the tags?

Sue Mosher said:
I wouldn't do a replace on Body unless I knew from BodyFormat that it's a plain text message or an RTF message whose formatting I don't care about.

It might help if I remembered which specific thread this is. I gave up trying
to code inserting a signature since there was no way to know how the
signature was named ('Untitled', 'Full Signature', etc.) given that multiple
users could have have saved it any of a number ways.

What I did discover is that when the Replace() executes on the ..Body
property if the message body contained a hyperlink, the hyper link was
converted from

[www.microsoft.com]

to

[HYPERLINK "www.microsoft.com" www.microsoft.com]

which of course is undesireable. The simple fix was to not have hyperlinks
in the signature that is specifically marked up as us. If its an email
address, the solution was to use the mailto: prefix.

:

So I should also be snooping around the .BodyFormat property?

Absolutely, if you care about formatting of the signature! Only if you know the message format can you know whether you can insert a formatted signature or a plain-text signature and what approach would be appropriate. If you have an HTML-format message, you would have to insert fully tagged HTML -- including the <a> link for a hyperlink -- into the HTMLBody property at the appropriate location.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Outlook 2003 SP2

So I should also be snooping around the .BodyFormat property?

:

But what format was the actual message? The default format set in Tools | Options isn't relevant once you're dealing with a message that already exists. >>
Wasn't aware of that.

I did just run a test to confirm wether or not the line of code below
updates the .Body property as well as .HTMLBody.

Item.Body = Replace(Item.Body, objControls(i).name, objControls(i).value)

The test was conducted by putting a STOP statement in the in the ITEM_SEND
event and then inspecting the .Body and .HTMLBody properties via the debug
window. ?ITEM.BODY and ?ITEM.HTML
Both properties were updated correctly regardless of the value in the
MESSAGE FORMAT set in the MAIL FORM tab of the OPTIONS screen.

:

"flip between ... formats" how? If you change format manually with Word as the editor, Outlook handles the formatting change. Never assume that something you can do manually works exactly the same way in code.


Its very strange because I only thought to use the .Body property, however as
I flip between TEXT, RTF, and HTML formats the text is updated properly
within Outlook and when I view sent mail via a web browser.

:

Any code to work with message bodies needs to be sensitive to the value of MailItem.BodyFormat and work with the Body or HTMLBody property as appropriate.



Actually, this all pertains to the question a week or so ago about message
templates. The solution that I'm implementing invovles building a custom
Outlook form with VBScript to insert message specific text into the body of
the message. The message is marked up with tags (i.e. <%Show_Name%>) that
correspond to user fields on the form. When the user clicks on a command
button, VBScript code cycles through the custom fields and uses Replace() to
insert the values into the message text. The specific problem with the
Signatures is that when the code runs, any hyperlink is converted to read
'HYPERLINK <URL> <Link Text>'. I've added another button that will take the
current message body and save it to a *.txt file for use as the form's
template message when the form is initially opened. Thought you might be
interested.

:

See http://www.outlookcode.com/codedetail.aspx?id=615 for a VBA sample that you should be able to adapt.

Note, though, that your requiremens "need to be able to add the signature of the current user" and "the message cannot contain any signature" are mutually exclusive.

How can I insert a signature into an item via code? I have a VBScript which
automatically inserts a custom message into the item and need to be able to
add the signature of the current user. Unfortunately, the message cannot
contain any signature.
 
If you had an HTML format message with the same kind of tokens, you could do the same thing with HTMLBody:

Item.HTMLBody = Replace(Item.HTMLBody, objControls(i).name, objControls(i).value)

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


dch3 said:
The replace() looks for a word that begins and ends with specific characters
specifically <% and %> as in <%Booth%> or <%Company_Name%>, I wasn't thinking
neccessarily of ASP when I choose to use the percent and bracket combination
- I just needed a combination that was very highly unlikely to ever appear in
a message. To keep the code vanilla, I named the form fields using the <% and
%> prefix and suffix. The code which inserts the values into the message body
loops through the controls using

Set objControls = Item.GetInspector.ModifiedFormPages("Invoice
Request").Controls

and then uses the control name to replace the text as in...

Item.Body = Replace(Item.Body, objControls(i).name, objControls(i).value)

David H

Sue Mosher said:
Exactly.

dch3 said:
I'm assuming that you would avoid Replace() if the BodyFormat is HTML due to
the possiblity of inadvertently wacking out/screwing up the tags?

:

I wouldn't do a replace on Body unless I knew from BodyFormat that it's a plain text message or an RTF message whose formatting I don't care about.

It might help if I remembered which specific thread this is. I gave up trying
to code inserting a signature since there was no way to know how the
signature was named ('Untitled', 'Full Signature', etc.) given that multiple
users could have have saved it any of a number ways.

What I did discover is that when the Replace() executes on the ...Body
property if the message body contained a hyperlink, the hyper link was
converted from

[www.microsoft.com]

to

[HYPERLINK "www.microsoft.com" www.microsoft.com]

which of course is undesireable. The simple fix was to not have hyperlinks
in the signature that is specifically marked up as us. If its an email
address, the solution was to use the mailto: prefix.

:

So I should also be snooping around the .BodyFormat property?

Absolutely, if you care about formatting of the signature! Only if you know the message format can you know whether you can insert a formatted signature or a plain-text signature and what approach would be appropriate. If you have an HTML-format message, you would have to insert fully tagged HTML -- including the <a> link for a hyperlink -- into the HTMLBody property at the appropriate location.

Outlook 2003 SP2

So I should also be snooping around the .BodyFormat property?

:

But what format was the actual message? The default format set in Tools | Options isn't relevant once you're dealing with a message that already exists. >>
Wasn't aware of that.

I did just run a test to confirm wether or not the line of code below
updates the .Body property as well as .HTMLBody.

Item.Body = Replace(Item.Body, objControls(i).name, objControls(i).value)

The test was conducted by putting a STOP statement in the in the ITEM_SEND
event and then inspecting the .Body and .HTMLBody properties via the debug
window. ?ITEM.BODY and ?ITEM.HTML
Both properties were updated correctly regardless of the value in the
MESSAGE FORMAT set in the MAIL FORM tab of the OPTIONS screen.

:

"flip between ... formats" how? If you change format manually with Word as the editor, Outlook handles the formatting change. Never assume that something you can do manually works exactly the same way in code.


Its very strange because I only thought to use the ..Body property, however as
I flip between TEXT, RTF, and HTML formats the text is updated properly
within Outlook and when I view sent mail via a web browser.

:

Any code to work with message bodies needs to be sensitive to the value of MailItem.BodyFormat and work with the Body or HTMLBody property as appropriate.



Actually, this all pertains to the question a week or so ago about message
templates. The solution that I'm implementing invovles building a custom
Outlook form with VBScript to insert message specific text into the body of
the message. The message is marked up with tags (i.e. <%Show_Name%>) that
correspond to user fields on the form. When the user clicks on a command
button, VBScript code cycles through the custom fields and uses Replace() to
insert the values into the message text. The specific problem with the
Signatures is that when the code runs, any hyperlink is converted to read
'HYPERLINK <URL> <Link Text>'. I've added another button that will take the
current message body and save it to a *.txt file for use as the form's
template message when the form is initially opened. Thought you might be
interested.

:

See http://www.outlookcode.com/codedetail.aspx?id=615 for a VBA sample that you should be able to adapt.

Note, though, that your requiremens "need to be able to add the signature of the current user" and "the message cannot contain any signature" are mutually exclusive.

How can I insert a signature into an item via code? I have a VBScript which
automatically inserts a custom message into the item and need to be able to
add the signature of the current user. Unfortunately, the message cannot
contain any signature.
 
Back
Top