Outlook 2007 AttachmentContextMenuDisplay(CommandBar, Attachments)

  • Thread starter Thread starter kul
  • Start date Start date
K

kul

http://msdn2.microsoft.com/en-us/library/bb175107.aspx

How can I convert this VBA code to C# or VB.NET by using Visual Studio 2005
Professional edition, shared add-in?

How can I define AttachmentContextMenuDisplay(CommandBar, Attachments) event
in OnStartupComplete(...) part or where ?

VBA Code:

Private Declare Function SHGetSpecialFolderLocation _
Lib "shell32" (ByVal hWnd As Long, _
ByVal nFolder As Long, ppidl As Long) As Long

Private Declare Function SHGetPathFromIDList _
Lib "shell32" Alias "SHGetPathFromIDListA" _
(ByVal Pidl As Long, ByVal pszPath As String) As Long

Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pvoid As Long)

Dim objAttachments As AttachmentSelection

Private Sub Application_AttachmentContextMenuDisplay( _
ByVal CommandBar As Office.CommandBar, _
ByVal Attachments As AttachmentSelection)

Dim objButton As CommandBarButton

On Error GoTo ErrRoutine

If Attachments.Count > 0 Then

' Get a reference to the selected attachments
' so we can work with them in the
' SaveToDesktop routine.
Set objAttachments = Attachments

' Create a new menu item and place it
' just after the Reply To All button
Set objButton = CommandBar.Controls.Add( _
msoControlButton, , , , True)

' Configure the menu item.
With objButton
.Style = msoButtonIconAndCaption
.Caption = "Save to &Desktop"
.FaceId = 355
' If you place this sample in a class module
' other than ThisOutlookSession, update this
' line of code to ensure that the OnAction
' property contains the correct project,
' class, and routine name.
.OnAction = "Project1.ThisOutlookSession.SaveToDesktop"
End With
End If

EndRoutine:
On Error GoTo 0
Set objButton = Nothing
Exit Sub

ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, _
vbOKOnly Or vbCritical, _
"Application_AttachmentContextMenuDisplay"
GoTo EndRoutine
End Sub

Private Sub SaveToDesktop()
Dim lngPidlFound As Long
Dim lngFolderFound As Long
Dim lngPidl As Long
Dim strPath As String
Dim objAttachment As Attachment

Const CSIDL_DESKTOPDIRECTORY = &H10
Const MAX_PATH = 260
Const NOERROR = 0

On Error GoTo ErrRoutine

' Obtain the physical path to the desktop folder
' for the current user.
strPath = Space(MAX_PATH)
lngPidlFound = SHGetSpecialFolderLocation( _
0, CSIDL_DESKTOPDIRECTORY, lngPidl)

If lngPidlFound = NOERROR Then
lngFolderFound = SHGetPathFromIDList(lngPidl, strPath)
If lngFolderFound Then
strPath = Left$(strPath, _
InStr(1, strPath, vbNullChar) - 1)
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
End If
End If

CoTaskMemFree lngPidl

' Save each selected attachment to the
' desktop folder.
If strPath <> "" Then
For Each objAttachment In objAttachments
objAttachment.SaveAsFile strPath & objAttachment.FileName
Next
End If

EndRoutine:
On Error GoTo 0
Set objAttachment = Nothing
Exit Sub

ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, _
vbOKOnly Or vbCritical, _
"SaveToDesktop"
GoTo EndRoutine
End Sub

Private Sub Application_ContextMenuClose(ByVal ContextMenu As OlContextMenu)
On Error Resume Next

If ContextMenu = olAttachmentContextMenu Then
' Once the context menu closes, remove the
' object reference to the attachments.
If Not (objAttachments Is Nothing) Then
Set objAttachments = Nothing
End If
End If

On Error GoTo 0
End Sub
 
You have to convert the VBA code line by line, there are no conversion tools
that are worth much of anything.

If you declare the Outlook.Application object WithEvents (VB.NET) you can
handle any Application event including the context menu events. For C# you
add an event handler.

You might want to download the Outlook sample addins from the Office Web
site, that would give you a starting point at least.
 
Thanks for your answer

I get from below msdn web site

'http://msdn2.microsoft.com/en-us/li...ts_11_event.attachmentcontextmenudisplay.aspx

and I use below code for events but give error :

not know this part
ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler

How can I write these event definition?

'Visual Basic (Declaration)
Event AttachmentContextMenuDisplay As
ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler

''Visual Basic (Declaration)
Public Event AttachmentContextMenuDisplay As
ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler Implements
ApplicationEvents_11_Event.AttachmentContextMenuDisplay

''Visual Basic (Usage)
Dim instance As Microsoft.Office.Interop.Outlook.ApplicationClass
Dim handler As
ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler

AddHandler instance.AttachmentContextMenuDisplay, handler

'*******************************
' Visual Basic (Declaration)
Public Event ContextMenuClose As
ApplicationEvents_11_ContextMenuCloseEventHandler Implements
ApplicationEvents_11_Event.ContextMenuClose
'Visual Basic (Usage)
Dim instance As Outlook.applicationClass
Dim handler As ApplicationEvents_11_ContextMenuCloseEventHandler

AddHandler instance.ContextMenuClose, handler

thanks
 
I'd do it differently.

In the module level declarations:

Dim WithEvents Application As Outlook.Application

In OnConnection:

Me.Application = CType(Application, Outlook.Application)

Then the events handlers would be like this:

Private Sub Application_ContextMenuClose(ByVal ContextMenu as
Outlook.OlContextMenu) Handles Application.ContextMenuClose

End Sub

Private Application_ItemContextMenuDisplay(ByVal CommandBar As
Office.CommandBar, ByVal Selection As Outlook.Selection) Handles
Application.ItemContextMenuDisplay

End Sub

I think Implements is wrong in this context and you shouldn't be declaring
Outlook.ApplicationClass but Outlook.Application.

I think you really should download the sample addins that the Outlook team
developed, they show things like this and provide a starting point for you
to experiment.
 
Thanks alot, it works wonderfull


kul said:
http://msdn2.microsoft.com/en-us/library/bb175107.aspx

How can I convert this VBA code to C# or VB.NET by using Visual Studio 2005
Professional edition, shared add-in?

How can I define AttachmentContextMenuDisplay(CommandBar, Attachments) event
in OnStartupComplete(...) part or where ?

VBA Code:

Private Declare Function SHGetSpecialFolderLocation _
Lib "shell32" (ByVal hWnd As Long, _
ByVal nFolder As Long, ppidl As Long) As Long

Private Declare Function SHGetPathFromIDList _
Lib "shell32" Alias "SHGetPathFromIDListA" _
(ByVal Pidl As Long, ByVal pszPath As String) As Long

Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pvoid As Long)

Dim objAttachments As AttachmentSelection

Private Sub Application_AttachmentContextMenuDisplay( _
ByVal CommandBar As Office.CommandBar, _
ByVal Attachments As AttachmentSelection)

Dim objButton As CommandBarButton

On Error GoTo ErrRoutine

If Attachments.Count > 0 Then

' Get a reference to the selected attachments
' so we can work with them in the
' SaveToDesktop routine.
Set objAttachments = Attachments

' Create a new menu item and place it
' just after the Reply To All button
Set objButton = CommandBar.Controls.Add( _
msoControlButton, , , , True)

' Configure the menu item.
With objButton
.Style = msoButtonIconAndCaption
.Caption = "Save to &Desktop"
.FaceId = 355
' If you place this sample in a class module
' other than ThisOutlookSession, update this
' line of code to ensure that the OnAction
' property contains the correct project,
' class, and routine name.
.OnAction = "Project1.ThisOutlookSession.SaveToDesktop"
End With
End If

EndRoutine:
On Error GoTo 0
Set objButton = Nothing
Exit Sub

ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, _
vbOKOnly Or vbCritical, _
"Application_AttachmentContextMenuDisplay"
GoTo EndRoutine
End Sub

Private Sub SaveToDesktop()
Dim lngPidlFound As Long
Dim lngFolderFound As Long
Dim lngPidl As Long
Dim strPath As String
Dim objAttachment As Attachment

Const CSIDL_DESKTOPDIRECTORY = &H10
Const MAX_PATH = 260
Const NOERROR = 0

On Error GoTo ErrRoutine

' Obtain the physical path to the desktop folder
' for the current user.
strPath = Space(MAX_PATH)
lngPidlFound = SHGetSpecialFolderLocation( _
0, CSIDL_DESKTOPDIRECTORY, lngPidl)

If lngPidlFound = NOERROR Then
lngFolderFound = SHGetPathFromIDList(lngPidl, strPath)
If lngFolderFound Then
strPath = Left$(strPath, _
InStr(1, strPath, vbNullChar) - 1)
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
End If
End If

CoTaskMemFree lngPidl

' Save each selected attachment to the
' desktop folder.
If strPath <> "" Then
For Each objAttachment In objAttachments
objAttachment.SaveAsFile strPath & objAttachment.FileName
Next
End If

EndRoutine:
On Error GoTo 0
Set objAttachment = Nothing
Exit Sub

ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, _
vbOKOnly Or vbCritical, _
"SaveToDesktop"
GoTo EndRoutine
End Sub

Private Sub Application_ContextMenuClose(ByVal ContextMenu As OlContextMenu)
On Error Resume Next

If ContextMenu = olAttachmentContextMenu Then
' Once the context menu closes, remove the
' object reference to the attachments.
If Not (objAttachments Is Nothing) Then
Set objAttachments = Nothing
End If
End If

On Error GoTo 0
End Sub
 
Back
Top