I've got the code for Excel - it refers to Outlook folder and loops the items, all OK. Code picks each item of folder items and chooses the needed ones by parametres. But I want this code to work with Outlook Selection - user chooses mails in Outlook and press button on Excel. When I try to determine variable as Outlook.Selection directly like
Public myOlSel As Outlook.Selection
it returns an error, because I need to switch settings to Outlook object model - but I'm writing a tool for common users, and common user can't switch on any models, he can only press the button to start macros :) I determine variable myOlSel as Object and try to set it as Selection of Outlook application object
Set myOlSel = olApp.Selection
And it also doesn't work. I guess, that it happen, because VBA Excel and Outlook both have selection in object model refering with the same synthax.
Sub CheckEmailsSelected()
Set olApp = GetObject(, "Outlook.Application")
Set olNameSpace = olApp.Session '.GetNameSpace("MAPI")
Dim att As Object
If Param3 = 1 Then
Set myOlSel = olApp.Selection
For Each mItem In myOlSel.Items
If mItem.Unread = True Then
If DateValue(mItem.LastModificationTime) >= DateValue(Now) Then
If mItem.Attachments.Count > 0 Then
count4 = count4 1
Set att = mItem.Attachments
For i = 1 To att.Count
If Right(att.Item(i).Filename, 4) = "xlsx" Or Right(att.Item(i).Filename, 3) = "xls" Then
count5 = count5 1
ReDim Preserve Stat(10, count5)
Stat(1, count5) = mItem.LastModificationTime
Stat(2, count5) = mItem.Companies
Stat(3, count5) = mItem.Subject
Stat(4, count5) = mItem.Sender
Stat(5, count5) = mItem.SenderEmailAddress
Stat(6, count5) = att.Item(i).Filename
If Right(att.Item(i).Filename, 4) = "xlsx" Then Stat(7, count5) = Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xlsx" Else Stat(7, count5) = Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xls"
Stat(8, count5) = mItem.Unread
Stat(10, count5) = mItem.EntryID
If Right(att.Item(i).Filename, 4) = "xlsx" Then att.Item(i).SaveAsFile Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xlsx" Else: att.Item(i).SaveAsFile Path2 & "\" & "Temp" & "\" & Right(mItem.EntryID, 24) & "-" & i & ".xls"
End If
Next i
End If
End If
End If
Next mItem
End If
End Sub
How can I get Outlook Selection from Excel?
CodePudding user response:
Try this:
Sub CheckEmailsSelected()
Dim olApp As Object, m As Object
Set olApp = GetObject(, "Outlook.Application")
Set myOlSel = olApp.ActiveExplorer.Selection
For Each m In myOlSel
If m.Class = 43 Then ' olMail = 43
Debug.Print m.Subject
End If
Next
End Sub