A program is trying to access e-mail addresses...

  • Thread starter Thread starter whitsendstudios
  • Start date Start date
W

whitsendstudios

Hi all,

I was under the impression that running a macro in Outlook from a
trusted source would bypass the "A program is trying to access e-mail
addresses you have stored in Outlook...." dialog. As it's all internal
to Outlook.

Am I wrong with this assumption?

If I am is there anyway around it?

I have created a macro from within Outlook to save bulk selected
emails and extract any attachments and rename everything into a
sensible filename "date time-received Sender Subject No-of-
attachments" and the same format with any attachments (apart from
adding the original filename to the end).

Thanks for any assistance,

Rob
 
Your impression is correct but ...

1) VBA may not be a trusted source. On the Help | About Microsoft Outlook, what is the setting for Security Mode? What Outlook version are you using?

2) VBA code that you want to be trusted needs to derive all its Outlook objects from the intrinsic Application object.
 
What version of Outlook?

How are you getting your trusted Application object?

Show your code.
 
Sorry, I'm using Outlook 2003 and the security mode is "Default".
When I ran my code it prompted me to enable macros and I ticked
"Always trust from this publisher" box.

My code is as follows which is copied straight into a macro into the
"ThisOutlookSession" section...

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Private Const MAX_PATH As Long = 260

' *** Add a REFERENCE to MICROSOFT SHELL CONTROlS AND AUTOMATION for
the directory browser

Function BrowseFolder(Optional Caption As String, _
Optional InitialFolder As String) As String

Dim SH As Shell32.Shell
Dim F As Shell32.Folder
Dim WshShell As Object

Set SH = New Shell32.Shell
Set F = SH.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS,
InitialFolder)
Set WshShell = CreateObject("WScript.Shell")

If Not F Is Nothing Then
'Special folders don't always return their full path that is
why we check the title first
Select Case F.Title
Case "Desktop"
BrowseFolder = WshShell.SpecialFolders("Desktop")
Case "My Documents"
BrowseFolder = WshShell.SpecialFolders("MyDocuments")
Case "My Computer"
MsgBox "Invalid selection", vbCritical + vbOKOnly,
"Error"
Exit Function
Case "My Network Places"
MsgBox "Invalid selection", vbCritical + vbOKOnly,
"Error"
Exit Function
Case Else
BrowseFolder = F.Items.Item.Path
End Select
End If

'Cleanup
Set SH = Nothing
Set F = Nothing
Set WshShell = Nothing

End Function

Sub FileEmails_Click()

' This uses an existing instance if available (default Outlook
behavior).
Dim oApp As New Outlook.Application
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection ' You need a selection
object for getting the selection.
Dim oItem As Object ' You don't know what type
it is yet
Dim oMail As Outlook.MailItem ' Holds the email
Dim oAttachment As Outlook.Attachment ' Holds the email
attachment
Dim strMsgName As String ' Holds name of msessage
Dim strFileName As String ' Holds name of any
attachments
Dim strDir As String ' Holds the directory to
save the items

Set oExp = oApp.ActiveExplorer ' Get the ActiveExplorer.
Set oSel = oExp.Selection ' Get the selection.

' Set initial directory
'strDir = "C:\"
strDir = "\\W2kserver\rtt data\NewCo\PROJECTS"

' Make the user the folder selection dialog
Dim FolderPath As String
FolderPath = BrowseFolder("Select destination folder for emails",
strDir)
If FolderPath = "" Then
Response = MsgBox("Please select a folder. No items were
saved", vbExclamation, MyApplName)
Exit Sub
End If

For i = 1 To oSel.Count ' Loop through all the
currently .selected items
Set oItem = oSel.Item(i) ' Get a selected item.
'DisplayInfo oItem

Set oMailItem = oItem
strMsgName = Mid(oMailItem.ReceivedTime, 7, 2) & "-" &
Mid(oMailItem.ReceivedTime, 4, 2) & "-" & Mid(oMailItem.ReceivedTime,
1, 2) & " [" & Right(oMailItem.ReceivedTime, 8) & "]" & " - FROM; " &
oMailItem.SenderName & " - " & oMailItem.Subject ' Create a unique
name for the message
strMsgName = Replace(strMsgName, ":", "")
strMsgName = Replace(strMsgName, "/", "-")
If oMailItem.Attachments.Count > 0 Then
For Each oAttachment In oMailItem.Attachments
'Set oAttachment = oMailItem.Attachments
strFileName = strMsgName & " [Filename] " &
oAttachment

oAttachment.SaveAsFile "" & FolderPath & "\" &
strFileName & ""
Next oAttachment
strMsgName = strMsgName & " [" &
oMailItem.Attachments.Count & " attachment(s)]"
End If
strMsgName = strMsgName & ".msg"
oMailItem.SaveAs "" & FolderPath & "\" & strMsgName & ""
Next i

MsgBox "Filing Complete!", vbOKOnly, "Done!" ' Inform user
proceedure is finished

End Sub
 
This is the problem:

Dim oApp As New Outlook.Application

As I said, you need to derive all Outlook objects from the intrinsic Application object:

Dim oApp as Outlook.Application
Set oApp = Application

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


Sorry, I'm using Outlook 2003 and the security mode is "Default".
When I ran my code it prompted me to enable macros and I ticked
"Always trust from this publisher" box.

My code is as follows which is copied straight into a macro into the
"ThisOutlookSession" section...

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Private Const MAX_PATH As Long = 260

' *** Add a REFERENCE to MICROSOFT SHELL CONTROlS AND AUTOMATION for
the directory browser

Function BrowseFolder(Optional Caption As String, _
Optional InitialFolder As String) As String

Dim SH As Shell32.Shell
Dim F As Shell32.Folder
Dim WshShell As Object

Set SH = New Shell32.Shell
Set F = SH.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS,
InitialFolder)
Set WshShell = CreateObject("WScript.Shell")

If Not F Is Nothing Then
'Special folders don't always return their full path that is
why we check the title first
Select Case F.Title
Case "Desktop"
BrowseFolder = WshShell.SpecialFolders("Desktop")
Case "My Documents"
BrowseFolder = WshShell.SpecialFolders("MyDocuments")
Case "My Computer"
MsgBox "Invalid selection", vbCritical + vbOKOnly,
"Error"
Exit Function
Case "My Network Places"
MsgBox "Invalid selection", vbCritical + vbOKOnly,
"Error"
Exit Function
Case Else
BrowseFolder = F.Items.Item.Path
End Select
End If

'Cleanup
Set SH = Nothing
Set F = Nothing
Set WshShell = Nothing

End Function

Sub FileEmails_Click()

' This uses an existing instance if available (default Outlook
behavior).
Dim oApp As New Outlook.Application
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection ' You need a selection
object for getting the selection.
Dim oItem As Object ' You don't know what type
it is yet
Dim oMail As Outlook.MailItem ' Holds the email
Dim oAttachment As Outlook.Attachment ' Holds the email
attachment
Dim strMsgName As String ' Holds name of msessage
Dim strFileName As String ' Holds name of any
attachments
Dim strDir As String ' Holds the directory to
save the items

Set oExp = oApp.ActiveExplorer ' Get the ActiveExplorer.
Set oSel = oExp.Selection ' Get the selection.

' Set initial directory
'strDir = "C:\"
strDir = "\\W2kserver\rtt data\NewCo\PROJECTS"

' Make the user the folder selection dialog
Dim FolderPath As String
FolderPath = BrowseFolder("Select destination folder for emails",
strDir)
If FolderPath = "" Then
Response = MsgBox("Please select a folder. No items were
saved", vbExclamation, MyApplName)
Exit Sub
End If

For i = 1 To oSel.Count ' Loop through all the
currently .selected items
Set oItem = oSel.Item(i) ' Get a selected item.
'DisplayInfo oItem

Set oMailItem = oItem
strMsgName = Mid(oMailItem.ReceivedTime, 7, 2) & "-" &
Mid(oMailItem.ReceivedTime, 4, 2) & "-" & Mid(oMailItem.ReceivedTime,
1, 2) & " [" & Right(oMailItem.ReceivedTime, 8) & "]" & " - FROM; " &
oMailItem.SenderName & " - " & oMailItem.Subject ' Create a unique
name for the message
strMsgName = Replace(strMsgName, ":", "")
strMsgName = Replace(strMsgName, "/", "-")
If oMailItem.Attachments.Count > 0 Then
For Each oAttachment In oMailItem.Attachments
'Set oAttachment = oMailItem.Attachments
strFileName = strMsgName & " [Filename] " &
oAttachment

oAttachment.SaveAsFile "" & FolderPath & "\" &
strFileName & ""
Next oAttachment
strMsgName = strMsgName & " [" &
oMailItem.Attachments.Count & " attachment(s)]"
End If
strMsgName = strMsgName & ".msg"
oMailItem.SaveAs "" & FolderPath & "\" & strMsgName & ""
Next i

MsgBox "Filing Complete!", vbOKOnly, "Done!" ' Inform user
proceedure is finished

End Sub
 
That worked perfectly!

Now I've seen your code example I can see what you mean't by
"intrinsic".

Thank you so much.
 
Back
Top