Adding a 2nd button (functionality not working)

N

Newt

I'm a hack at writing this code, but here goes. I added a toggle button
several months ago that works great for adding Read Receipts. Now, I'd like
to add another button that allows me to select whether I save a copy in the
Sent Items folder. I figured out the correct code for one button, but don't
know how to make it work so that both buttons function together. Any help?

'THIS OUTLOOK SESSION
Private WithEvents m_oInspector As Outlook.Inspector
Private WithEvents m_oMailItem As Outlook.MailItem
Private WithEvents YourButton As Office.CommandBarButton
Private WithEvents YourButton1 As Office.CommandBarButton
Private m_lKey As Long

Friend Function Init(oInspector As Outlook.Inspector, ByVal lKey As Long) As
Boolean
Set m_oInspector = oInspector
Set m_oMailItem = oInspector.CurrentItem
If m_oMailItem.Sent = False Then
m_lKey = lKey
CreateButton oInspector
CreateButton1 oInspector
Init = True
End If
End Function

Private Sub CreateButton(Inspector As Outlook.Inspector)
Dim objCB As Office.CommandBar
Dim colCB As Office.CommandBars
Dim objPicture As stdole.IPictureDisp
Dim objMask As stdole.IPictureDisp

Const PICTURE_PATH As String = "C:\Documents and
Settings\drhyase\Icons\readreceipt.bmp"
Const PICTURE_MASK As String = "C:\Documents and
Settings\drhyase\Icons\readreceiptmask.bmp"

Set colCB = Inspector.CommandBars
Set objCB = colCB.Item("Standard")
Set colCBcontrols = objCB.Controls
Set YourButton = colCBcontrols.Add(msoControlButton, , "Standard", ,
True)
Set objPicture = LoadPicture(PICTURE_PATH)
Set objMask = LoadPicture(PICTURE_MASK)
With YourButton
.Caption = "&Read Receipt"
.Move Before:=3
.TooltipText = "Add/Remove a Read Receipt"
.Picture = objPicture
.Mask = objMask
.Style = msoButtonIconAndCaption

End With

Set objCB = Nothing
Set objCB = Nothing

End Sub
Private Sub CreateButton1(Inspector As Outlook.Inspector)
Dim objCB As Office.CommandBar
Dim colCB As Office.CommandBars

Set colCB = Inspector.CommandBars
Set objCB = colCB.Item("Standard")
Set colCBcontrols = objCB.Controls
Set YourButton1 = colCBcontrols.Add(msoControlButton, , "Standard",
, True)

With YourButton1
.Caption = "Save Copy"
.Move Before:=4
.TooltipText = "Save in Sent Items Folder"
.Style = msoButtonCaption

End With

Set objCB = Nothing
Set objCB = Nothing

End Sub

Friend Sub CloseInspector()
On Error Resume Next
Application.RemoveInspector m_lKey
Set YourButton = Nothing
Set YourButton1 = Nothing
Set m_oMailItem = Nothing
Set m_oInspector = Nothing
End Sub

Private Sub m_oInspector_Close()
CloseInspector
End Sub

Private Sub m_oMailItem_Close(Cancel As Boolean)
If m_oMailItem.Saved Then
CloseInspector
End If
End Sub

Private Sub m_oMailItem_Send(Cancel As Boolean)
CloseInspector
End Sub
Private Sub YourButton_Click1(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Dim objInsp1 As Outlook.Inspector
Dim objItem1 As MailItem

' get the currently open item and make sure it's a mail message
Set objInsp1 = Application.ActiveInspector
If Not objInsp1 Is Nothing Then
Set objItem1 = objInsp1.CurrentItem
If objItem1.Class = olMail Then
' make sure it's unsent
If objItem1.Sent = False Then
' button action on
If YourButton1.State = msoButtonUp Then
YourButton1.State = msoButtonDown
Else
YourButton1.State = msoButtonUp
End If
If objItem1.DeleteAfterSubmit = False Then
objItem1.DeleteAfterSubmit = True
' button action off
Else
objItem1.DeleteAfterSubmit = False
End If
End If
End If
End If

Set objInsp1 = Nothing
Set objItem1 = Nothing

End Sub

Private Sub YourButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Dim objInsp As Outlook.Inspector
Dim objItem As MailItem

' get the currently open item and make sure it's a mail message
Set objInsp = Application.ActiveInspector
If Not objInsp Is Nothing Then
Set objItem = objInsp.CurrentItem
If objItem.Class = olMail Then
' make sure it's unsent
If objItem.Sent = False Then
' button action on
If YourButton.State = msoButtonUp Then
YourButton.State = msoButtonDown
Else
YourButton.State = msoButtonUp
End If
If objItem.ReadReceiptRequested = False Then
objItem.ReadReceiptRequested = True
' button action off
Else
objItem.ReadReceiptRequested = False
End If
End If
End If
End If

Set objInsp = Nothing
Set objItem = Nothing

End Sub

'CLASS MODULE (cInspector)
Private WithEvents m_oInspector As Outlook.Inspector
Private WithEvents m_oMailItem As Outlook.MailItem
Private WithEvents YourButton As Office.CommandBarButton
Private WithEvents YourButton1 As Office.CommandBarButton
Private m_lKey As Long

Friend Function Init(oInspector As Outlook.Inspector, ByVal lKey As Long) As
Boolean
Set m_oInspector = oInspector
Set m_oMailItem = oInspector.CurrentItem
If m_oMailItem.Sent = False Then
m_lKey = lKey
CreateButton oInspector
CreateButton1 oInspector
Init = True
End If
End Function

Private Sub CreateButton(Inspector As Outlook.Inspector)
Dim objCB As Office.CommandBar
Dim colCB As Office.CommandBars
Dim objPicture As stdole.IPictureDisp
Dim objMask As stdole.IPictureDisp

Const PICTURE_PATH As String = "C:\Documents and
Settings\drhyase\Icons\readreceipt.bmp"
Const PICTURE_MASK As String = "C:\Documents and
Settings\drhyase\Icons\readreceiptmask.bmp"

Set colCB = Inspector.CommandBars
Set objCB = colCB.Item("Standard")
Set colCBcontrols = objCB.Controls
Set YourButton = colCBcontrols.Add(msoControlButton, , "Standard", ,
True)
Set objPicture = LoadPicture(PICTURE_PATH)
Set objMask = LoadPicture(PICTURE_MASK)
With YourButton
.Caption = "&Read Receipt"
.Move Before:=3
.TooltipText = "Add/Remove a Read Receipt"
.Picture = objPicture
.Mask = objMask
.Style = msoButtonIconAndCaption

End With

Set objCB = Nothing
Set objCB = Nothing

End Sub
Private Sub CreateButton1(Inspector As Outlook.Inspector)
Dim objCB As Office.CommandBar
Dim colCB As Office.CommandBars

Set colCB = Inspector.CommandBars
Set objCB = colCB.Item("Standard")
Set colCBcontrols = objCB.Controls
Set YourButton1 = colCBcontrols.Add(msoControlButton, , "Standard",
, True)

With YourButton1
.Caption = "Save Copy"
.Move Before:=4
.TooltipText = "Save in Sent Items Folder"
.Style = msoButtonCaption

End With

Set objCB = Nothing
Set objCB = Nothing

End Sub

Friend Sub CloseInspector()
On Error Resume Next
Application.RemoveInspector m_lKey
Set YourButton = Nothing
Set YourButton1 = Nothing
Set m_oMailItem = Nothing
Set m_oInspector = Nothing
End Sub

Private Sub m_oInspector_Close()
CloseInspector
End Sub

Private Sub m_oMailItem_Close(Cancel As Boolean)
If m_oMailItem.Saved Then
CloseInspector
End If
End Sub

Private Sub m_oMailItem_Send(Cancel As Boolean)
CloseInspector
End Sub
Private Sub YourButton_Click1(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Dim objInsp1 As Outlook.Inspector
Dim objItem1 As MailItem

' get the currently open item and make sure it's a mail message
Set objInsp1 = Application.ActiveInspector
If Not objInsp1 Is Nothing Then
Set objItem1 = objInsp1.CurrentItem
If objItem1.Class = olMail Then
' make sure it's unsent
If objItem1.Sent = False Then
' button action on
If YourButton1.State = msoButtonUp Then
YourButton1.State = msoButtonDown
Else
YourButton1.State = msoButtonUp
End If
If objItem1.DeleteAfterSubmit = False Then
objItem1.DeleteAfterSubmit = True
' button action off
Else
objItem1.DeleteAfterSubmit = False
End If
End If
End If
End If

Set objInsp1 = Nothing
Set objItem1 = Nothing

End Sub

Private Sub YourButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Dim objInsp As Outlook.Inspector
Dim objItem As MailItem

' get the currently open item and make sure it's a mail message
Set objInsp = Application.ActiveInspector
If Not objInsp Is Nothing Then
Set objItem = objInsp.CurrentItem
If objItem.Class = olMail Then
' make sure it's unsent
If objItem.Sent = False Then
' button action on
If YourButton.State = msoButtonUp Then
YourButton.State = msoButtonDown
Else
YourButton.State = msoButtonUp
End If
If objItem.ReadReceiptRequested = False Then
objItem.ReadReceiptRequested = True
' button action off
Else
objItem.ReadReceiptRequested = False
End If
End If
End If
End If

Set objInsp = Nothing
Set objItem = Nothing

End Sub
 
E

Eric Legault [MVP - Outlook]

What exactly do you mean? Do you want both functions - one to request a
receipt and the other to toggle saving a copy - to fire by one button? Or
are you just having troubleh having a second button do anything?
 
N

Newt

I can't get the 2nd button to do anything. I would like 2 buttons--one that
requests read receipts and one that saves a copy. When I put the code in for
both buttons, the "save copy" button doesn't work.
 
E

Eric Legault [MVP - Outlook]

I can't get your code to run at all. Upon review though, it appears you have
way more code than is necessary. The best I can recommend at this point it
to review how Inspectors can be trapped by my example here:

Getting a Handle on Your E-mails with VBA:
http://blogs.officezealot.com/legault/articles/2224.aspx

I also recommend downloading the sample project here for guidelines and best
practices when working with custom toolbars/buttons:

Items Command Bar:
http://www.microeye.com/resources/itemsCB.htm

--
Eric Legault (Outlook MVP, MCDBA, old school WOSA MCSD, B.A.)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/
 

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