Discussion:
Customize toolbar buttoms
(too old to reply)
HenrikH
2009-09-20 12:01:45 UTC
Permalink
I have some VBA code which runs when the user presses a buttom on the
toolbar.

Diffent code have to be run when the user presses a buttom on the
toolbar of the new mail window (the window that shows a mail beeing
written) or if he presses the buttom on the toolbar of the received
mail window (the windows showing a opened mail).

I have placed the butooms (2 different buttoms each assigned each own
macro) on the 2 diffent windows. However both buttoms show up on both
windows.

How do I prevent the wrong buttom from showing at the wrong window?

The buttoms are not put on the toolbars using code but using the
standard way of editing the standard toolbar.

Hope you understand my question ;-)
Sue Mosher [MVP]
2009-09-20 12:33:04 UTC
Permalink
Why not use one button that calls different procedures, depending on the
state of the item, i.e. whether the Sent property is True or False?
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by HenrikH
I have some VBA code which runs when the user presses a buttom on the
toolbar.
Diffent code have to be run when the user presses a buttom on the
toolbar of the new mail window (the window that shows a mail beeing
written) or if he presses the buttom on the toolbar of the received
mail window (the windows showing a opened mail).
I have placed the butooms (2 different buttoms each assigned each own
macro) on the 2 diffent windows. However both buttoms show up on both
windows.
How do I prevent the wrong buttom from showing at the wrong window?
The buttoms are not put on the toolbars using code but using the
standard way of editing the standard toolbar.
Hope you understand my question ;-)
HenrikH
2009-09-21 06:31:10 UTC
Permalink
Hi Sue

The buttons has diffent looks and different help texts. One button
"Send and Save", another just "Save" and a 3th reads "Save and
delete".

However if it cannot be solved any other way than your suggestion I
must re-think the buttons.

BTW I'm using your suggestion to run code when a mail i sent and
reaches the sent folder but it is unstable. Sometimes it fires and
after sometimes it don't. If I restart Outlook it runs again but only
one or 2 times then it doesnt fire again until Outlook is restartet -
any ideas to this behavior?

The code (in ThisOutlookSession):
Dim WithEvents colSentItems As Items

Private Sub Application_Startup()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
Set NS = Nothing
End Sub

Private Sub colSentItems_ItemAdd(ByVal Item As Object)
If Item.Class = olMail Then
strname2 = Left(Item.CreationTime, 10)
If Kundenr = "" Then
Item.SaveAs ValgtFolder & "\" & strname2 & " - " & strname
& ".msg", olMSG
Else
Item.SaveAs rootdir & Kundenr & "\Mail\" & strname2 & " -
" & strname & ".msg", olMSG
End If
End If
End Sub
Sue Mosher [MVP]
2009-09-21 10:26:34 UTC
Permalink
What version of Outlook?
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by HenrikH
Hi Sue
The buttons has diffent looks and different help texts. One button
"Send and Save", another just "Save" and a 3th reads "Save and
delete".
However if it cannot be solved any other way than your suggestion I
must re-think the buttons.
BTW I'm using your suggestion to run code when a mail i sent and
reaches the sent folder but it is unstable. Sometimes it fires and
after sometimes it don't. If I restart Outlook it runs again but only
one or 2 times then it doesnt fire again until Outlook is restartet -
any ideas to this behavior?
Dim WithEvents colSentItems As Items
Private Sub Application_Startup()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
Set NS = Nothing
End Sub
Private Sub colSentItems_ItemAdd(ByVal Item As Object)
If Item.Class = olMail Then
strname2 = Left(Item.CreationTime, 10)
If Kundenr = "" Then
Item.SaveAs ValgtFolder & "\" & strname2 & " - " & strname
& ".msg", olMSG
Else
Item.SaveAs rootdir & Kundenr & "\Mail\" & strname2 & " -
" & strname & ".msg", olMSG
End If
End If
End Sub
HenrikH
2009-09-21 11:36:01 UTC
Permalink
Outlook 2003

Regards

Henrik
Sue Mosher [MVP]
2009-09-21 12:00:48 UTC
Permalink
This article explains some circumstances where the Items.ItemAdd event is
unreliable: http://support.microsoft.com/kb/290653. Also, I don't see any
statements to set the value of the Kundenr, ValgtFolder, and rootdir
variables; maybe the items are being saved, but in a location you don't
expect. If ItemAdd stops working for some other, unknown reason, you can
reinstantiate the Items collection by simply running the Application_Startup
procedure. In other words, you don't need to restart Outlook.

As for the toolbar buttons, you can use the Inspectors.NewInspector event to
respond to a new item window being displayed and the MailItem.Open event
(instantiate the MailItem from NewInspector) to check properties of the
message and show or hide toolbar buttons appropriately.

FWIW, I cannot imagine what utility a "Save and Delete" button might offer.
And you may want to read the article at
http://blogs.msdn.com/stephen_griffin/archive/2008/01/08/no-msg-for-you.aspx
in which one of Microsoft's experts explains why saving messages as .msg
files may not be a good idea.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by HenrikH
Outlook 2003
Post by HenrikH
The buttons has diffent looks and different help texts. One button
"Send and Save", another just "Save" and a 3th reads "Save and
delete".
However if it cannot be solved any other way than your suggestion I
must re-think the buttons.
BTW I'm using your suggestion to run code when a mail i sent and
reaches the sent folder but it is unstable. Sometimes it fires and
after sometimes it don't. If I restart Outlook it runs again but only
one or 2 times then it doesnt fire again until Outlook is restartet -
any ideas to this behavior?
Dim WithEvents colSentItems As Items
Private Sub Application_Startup()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
Set NS = Nothing
End Sub
Private Sub colSentItems_ItemAdd(ByVal Item As Object)
If Item.Class = olMail Then
strname2 = Left(Item.CreationTime, 10)
If Kundenr = "" Then
Item.SaveAs ValgtFolder & "\" & strname2 & " - " & strname
& ".msg", olMSG
Else
Item.SaveAs rootdir & Kundenr & "\Mail\" & strname2 & " -
" & strname & ".msg", olMSG
End If
End If
End Sub
HenrikH
2009-09-21 19:27:24 UTC
Permalink
The article about the unstabilty mentions large numbers. But there is
only one mail being sent.

The varibles Kundenr, ValgtFolder, and rootdir are being declared as
global variable in the declation section of a module in which the rest
of the code resides, and are also being set in that module.

I've tried setting af breakpoint in the code in ThisOutlookSession to
see if the code is run - and it isnt. So the variables are not being
saved. Can I run the Application_Startup procedure at the end of my
code in the module to force it to run?

About the toolbar buttons I will look into your suggestion - it
certainly sounds like a good solution - if I can figure it out ;-)

The Save and Delete button is used for saving the message and then
"deleting" it - thereby having outlook move it to the Deleted messages
folder (like pressing Outlooks own delete button). The button that
only saves the message leaves the message open (the user wants it to
stay in the inbox).

Thanks Sue, and have a nice day.
Sue Mosher [MVP]
2009-09-22 03:36:53 UTC
Permalink
You can run the Application_Startup procedure manually at any time to
reinstantiate your variables. There's no point in running it from your other
event handlers, though.
Post by HenrikH
The Save and Delete button is used for saving the message and then
"deleting" it - thereby having outlook move it to the Deleted messages
I guess I don't see the point in saving the message if you're just going to
delete it.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by HenrikH
The article about the unstabilty mentions large numbers. But there is
only one mail being sent.
The varibles Kundenr, ValgtFolder, and rootdir are being declared as
global variable in the declation section of a module in which the rest
of the code resides, and are also being set in that module.
I've tried setting af breakpoint in the code in ThisOutlookSession to
see if the code is run - and it isnt. So the variables are not being
saved. Can I run the Application_Startup procedure at the end of my
code in the module to force it to run?
About the toolbar buttons I will look into your suggestion - it
certainly sounds like a good solution - if I can figure it out ;-)
The Save and Delete button is used for saving the message and then
"deleting" it - thereby having outlook move it to the Deleted messages
folder (like pressing Outlooks own delete button). The button that
only saves the message leaves the message open (the user wants it to
stay in the inbox).
Thanks Sue, and have a nice day.
HenrikH
2009-09-23 07:40:05 UTC
Permalink
Post by Sue Mosher [MVP]
You can run the Application_Startup procedure manually at any time to
reinstantiate your variables. There's no point in running it from your other
event handlers, though.
I don't have any code in a Application_Startup procedure and i can't
expect the users to start it manually.
Maybe I don't understand what you mean by running Application_Startup.
I thought I could force it to run by triggering it in the macro-code
executed rom the toolbar button.

The unstability is a big problem in the project.
Post by Sue Mosher [MVP]
I guess I don't see the point in saving the message if you're just going to
delete it.
The message is saved outside Outlook on an external drive. So when the
user deletes it in Outlook at is "saved" in Deleted Items as the
normal Outlook action when deleting mails. Thereby the user has the
mail stored in 2 places - internal in Outlook and external (along with
many other customer-related data).
HenrikH
2009-09-23 08:02:28 UTC
Permalink
Post by HenrikH
I don't have any code in a Application_Startup procedure
Forget the above - of course I have the code ;-) Must have been sleepy.
HenrikH
2009-09-29 10:20:07 UTC
Permalink
Post by Sue Mosher [MVP]
You can run the Application_Startup procedure manually at any time to
reinstantiate your variables. There's no point in running it from your other
event handlers, though.
Trying to call the Application_Startup procedure from my code but keep
getting errors.

How do I call it from code?

I want to be sure that the Application_Startup procedure is running
when the mail hits the Sent Items folder. And due to the unstability
it oftens doesnt run when it has to. So if I could initiate from code
it would be fine.
Sue Mosher [MVP]
2009-09-29 13:52:59 UTC
Permalink
Did you read what I wrote earlier? There's no point in calling
Application_Startup from your code. Just run it manually if you suspect that
your event handlers are no longer working.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
Post by HenrikH
Post by Sue Mosher [MVP]
You can run the Application_Startup procedure manually at any time to
reinstantiate your variables. There's no point in running it from your other
event handlers, though.
Trying to call the Application_Startup procedure from my code but keep
getting errors.
How do I call it from code?
I want to be sure that the Application_Startup procedure is running
when the mail hits the Sent Items folder. And due to the unstability
it oftens doesnt run when it has to. So if I could initiate from code
it would be fine.
HenrikH
2009-10-01 10:56:14 UTC
Permalink
Post by Sue Mosher [MVP]
Did you read what I wrote earlier? There's no point in calling
Application_Startup from your code. Just run it manually if you suspect that
your event handlers are no longer working.
--
Yes thanks I read it.

However I cannot expect the users to run Application_Startup manually
if it isnt running. And they might not even know that it isnt running
and thereby not saving their mail as they expect it to do.

Thats the reason why I need to be sure that the code is active and
running.
Ken Slovak - [MVP - Outlook]
2009-10-01 13:06:23 UTC
Permalink
Application_Startup will run each time Outlook is started. However, if you
are planning on deploying your code to other people you really shouldn't be
using VBA code, you should use a COM addin. VBA macros aren't designed to be
deployed and a COM addin is a more robust and deployable supported solution.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by HenrikH
Post by Sue Mosher [MVP]
Did you read what I wrote earlier? There's no point in calling
Application_Startup from your code. Just run it manually if you suspect that
your event handlers are no longer working.
--
Yes thanks I read it.
However I cannot expect the users to run Application_Startup manually
if it isnt running. And they might not even know that it isnt running
and thereby not saving their mail as they expect it to do.
Thats the reason why I need to be sure that the code is active and
running.
HenrikH
2009-10-02 08:06:29 UTC
Permalink
Thanks Ken but I have no experience with COM addins and only us VBA
from time to time so having to learn using COM addins would be
"overkill" compared to the use I would have of it in the future.

Is there no way to be sure that the Application_Startup runs when it's
expected to, maybe invoking it from code?
Ken Slovak - [MVP - Outlook]
2009-10-02 13:37:33 UTC
Permalink
If the startup procedure doesn't run on Outlook startup either there's an
error in it or macros are limited and running it manually won't work either.

Normally the only use for running a startup event manually is to test your
code to make sure it works. After all that's the purpose of a startup event
handler, to run automatically.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by HenrikH
Thanks Ken but I have no experience with COM addins and only us VBA
from time to time so having to learn using COM addins would be
"overkill" compared to the use I would have of it in the future.
Is there no way to be sure that the Application_Startup runs when it's
expected to, maybe invoking it from code?
HenrikH
2009-10-09 10:09:42 UTC
Permalink
It runs sometimes and sometimes it dosnt. I cannot find any reason til
why/why not.

So there is no error in it and the macros are allowed to run.

If I start manually it works for some time and then it stops.

However if I could invoke it (start it) from the code I execute when
the user wants to save the mail the sent mail it would be a solution
to my problem. Maybe not the best (better to find out why Startup
dosnt run), but it would work for now.

Heres the code (inspired from Sue Mosher):
Dim WithEvents colSentItems As Items


Private Sub Application_Startup()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
Set NS = Nothing
End Sub

Private Sub colSentItems_ItemAdd(ByVal Item As Object)

If SendPost = 1 Then
If Item.Class = olMail Then
strname2 = Left(Item.CreationTime, 10)
strname3 = RenFile(Item.Subject, strname)

If Kundenr = "" Then
Item.SaveAs ValgtFolder & "\" & strname2 & " - " &
strname & ".msg", olMSG
Else
Item.SaveAs rootdir & Kundenr & "\Mail\" & strname2 &
" - " & strname & ".msg", olMSG
'MsgBox Item.Subject
End If
SendPost = 0
End If
End If
End Sub
Ken Slovak - [MVP - Outlook]
2009-10-09 12:53:45 UTC
Permalink
If Application_Startup() doesn't run would any macros run for you? You can
just replicate the code in that event handler and stick it in any macro
procedure which you can run like any other macro. But if macros aren't
running then you're back to the same problem.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by HenrikH
It runs sometimes and sometimes it dosnt. I cannot find any reason til
why/why not.
So there is no error in it and the macros are allowed to run.
If I start manually it works for some time and then it stops.
However if I could invoke it (start it) from the code I execute when
the user wants to save the mail the sent mail it would be a solution
to my problem. Maybe not the best (better to find out why Startup
dosnt run), but it would work for now.
Dim WithEvents colSentItems As Items
Private Sub Application_Startup()
Dim NS As Outlook.NameSpace
Set NS = Application.GetNamespace("MAPI")
Set colSentItems = NS.GetDefaultFolder(olFolderSentMail).Items
Set NS = Nothing
End Sub
Private Sub colSentItems_ItemAdd(ByVal Item As Object)
If SendPost = 1 Then
If Item.Class = olMail Then
strname2 = Left(Item.CreationTime, 10)
strname3 = RenFile(Item.Subject, strname)
If Kundenr = "" Then
Item.SaveAs ValgtFolder & "\" & strname2 & " - " &
strname & ".msg", olMSG
Else
Item.SaveAs rootdir & Kundenr & "\Mail\" & strname2 &
" - " & strname & ".msg", olMSG
'MsgBox Item.Subject
End If
SendPost = 0
End If
End If
End Sub
HenrikH
2009-10-19 14:00:23 UTC
Permalink
Hi Ken

Other macros in the same project runs.

The code shown earlier is called from code in a normal module.

Here's the project:

I have a button (in fact several buttons) in the mail-toolbar to give
the user the oppertunity to save a received mail or save a sent mail
to an external location. The part that saves a received mail works
fine. If the user wants to save a sent mail I will have to wait until
the mail reaches the sent folder otherwise the mail is saved as a mail
being edited (no sent date and showing up editable/sendable if it is
opened from the external location). Hope you understand!.

For the same reason I cannot put the code any other place since it
will run and save the mail before the mail is actually sent.

Sometimes the code above fires and somtime it doesnt - however the
code that calls the above code always fires - you see the user has to
enter a customer number in an messagebox do tell where the mail is to
be saved. And the code requesting the customer number always runs. So
I now that macros run allright.
Ken Slovak - [MVP - Outlook]
2009-10-19 17:34:04 UTC
Permalink
If as you say the other macros in the project run then the problem is likely
an error that's not handled in the macro that isn't running. Especially if
the code that calls that macro did run. Calling Application_Startup isn't
likely to help with that sort of problem.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by HenrikH
Hi Ken
Other macros in the same project runs.
The code shown earlier is called from code in a normal module.
I have a button (in fact several buttons) in the mail-toolbar to give
the user the oppertunity to save a received mail or save a sent mail
to an external location. The part that saves a received mail works
fine. If the user wants to save a sent mail I will have to wait until
the mail reaches the sent folder otherwise the mail is saved as a mail
being edited (no sent date and showing up editable/sendable if it is
opened from the external location). Hope you understand!.
For the same reason I cannot put the code any other place since it
will run and save the mail before the mail is actually sent.
Sometimes the code above fires and somtime it doesnt - however the
code that calls the above code always fires - you see the user has to
enter a customer number in an messagebox do tell where the mail is to
be saved. And the code requesting the customer number always runs. So
I now that macros run allright.
HenrikH
2009-10-20 16:57:31 UTC
Permalink
Thanks Ken

But is it possible to call Application_Startup from my code in the
module?

When i put a liune with :

Call Application_Startup

or just

Application_Startup

In my code I get an error."Sub or function not defined"

I have tried making it public with "Public Sub Application_Startup()"
instead of "Private Sub Application_Startup()" with no luck.
Post by Ken Slovak - [MVP - Outlook]
If as you say the other macros in the project run then the problem is likely
an error that's not handled in the macro that isn't running. Especially if
the code that calls that macro did run. Calling Application_Startup isn't
likely to help with that sort of problem.
--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.http://www.slovaktech.com/products.htm
Ken Slovak - [MVP - Outlook]
2009-10-20 17:24:06 UTC
Permalink
It has to be Public and then it can be called as
Application.Application_Startup from other VBA code modules or outside code.
Understand that while this works it's not supported by MS and may or may not
work in future versions of Outlook.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by HenrikH
Thanks Ken
But is it possible to call Application_Startup from my code in the
module?
Call Application_Startup
or just
Application_Startup
In my code I get an error."Sub or function not defined"
I have tried making it public with "Public Sub Application_Startup()"
instead of "Private Sub Application_Startup()" with no luck.
HenrikH
2009-10-29 11:46:48 UTC
Permalink
Thanks Ken

The part I was missing was the "Application." before
Application_Startup

Will give it a go and see if it solves the problem.

HenrikH

Loading...