Saving emails using Visual Basic - Selecting folder with msoFileDialogFolderPicker

Status
Not open for further replies.

mail11

Senior Member
Outlook version
Email Account
IMAP
Hi, I asked this a few years ago but didn't get a response.
Was hoping it's now possible.

This macro saves selected emails into a folder chosen by function "BrowseForFolder". It also adds the date as part of the filename. My issue is that the default start folder selected is always the same and I have to navigate to the folder each time.
I want to use "msoFileDialogFolderPicker" to select the folder because this is able to remember the last folder chosen and default to that path as the start when the macro is called again.


msoFileDialogFolderPicker can be used in Excel.


Code:
Option Explicit

Public Sub Save_Messages_Select_Ask2()

Dim oMail As Outlook.MailItem
Dim objItem As Object
Dim sPath As String
Dim dtDate As Date
Dim sName As String
Dim enviro As String

enviro = CStr(Environ("USERPROFILE"))
For Each objItem In ActiveExplorer.Selection

If objItem.MessageClass = "IPM.Note" Then
Set oMail = objItem

sName = oMail.Subject

dtDate = oMail.ReceivedTime
sName = Format(dtDate, "yyyy mm dd", vbUseSystemDayOfWeek, _
vbUseSystem) & Format(dtDate, " hhnn", _
vbUseSystemDayOfWeek, vbUseSystem) & " - " & sName & ".msg"

sPath = BrowseForFolder(enviro & "\Documents\")
Debug.Print sPath & "\" & sName
oMail.SaveAs sPath & "\" & sName, olMSG

End If
Next

End Sub

Function BrowseForFolder(Optional OpenAt As Variant) As Variant

Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0

Set ShellApp = Nothing
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select

Exit Function

Invalid:
BrowseForFolder = False

End Function
 
I have a sample that uses Word's object model to access msoFileDialogFolderPicker here -

The VBA code is basically the same as you would use in Word or Excel - you just need to reference the app's object model so outlook can access it. Some (or most) but not all macros recorded in either of those apps can be used in Outlook if you reference the object model. In some cases you will need to use early binding though.
 
Hi, thanks but I couldn't see anything in that link relating to msoFileDialogFolderPicker.
With the simpler one above, there is a variable called OpenAt. Is there a way to specify this and store the value for next use?
 
From the macro the link points to -
Code:
    'Set the dialog box type to Open
    Set dlgOpen = wdApp.FileDialog(msoFileDialogFilePicker)


This sample uses a specified folder path to start - but you can only browse subfolders, not go up a level.

The values are:
BrowseForFolder(WINDOW_HANDLE, "Window Title", WINDOW_OPTIONS, StartPath)

In the function it is
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

the path is passed as the value of openat:
StrFolderPath = BrowseForFolder("C:\Users\diane\Documents\Email\")

If I use the variable option so it works on any computer:
StrFolderPath = BrowseForFolder(StrUserPath)

I need to dim StrUserPath as a variant, not a string, so it jumps to the right folder. If it's a string, it goes to the user's home folder. (I will fix that in my code)
Dim StrUserPath As Variant

Or I can do it this way:
OpenAt = StrUserPath
' Get the BrowseForFolder function How to use Windows filepaths in a macro
StrFolderPath = BrowseForFolder(OpenAt )

And my example path is not going to let me browse, because it only browses down -
1646324471700.png


Use the topmost folder so you can browse subfolders-
1646324637360.png
 
OK thanks, this is useful, but it's a shame there's no ability to go up a level.
 
you just need to start at the top. :)
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
R Saving Emails and Attachments as .msg file Using Outlook 3
I Saving attachments from multiple emails and updating file name Outlook VBA and Custom Forms 0
D Saving Selected Emails as PDF and saving Attachments Outlook VBA and Custom Forms 6
M Dialog called up multiple times when saving emails from macro Outlook VBA and Custom Forms 2
Kevin H Remotely saving emails Using Outlook 1
S trouble with Outlook 2010 saving sent emails Using Outlook 2
D Saving outlook emails in html and attachments Using Outlook 4
C Saving Emails as Messages Problems with Access Data Collection et Outlook VBA and Custom Forms 1
S Automate saving of attachments on new incoming emails Outlook VBA and Custom Forms 3
Rupert Dragwater Background colors not saving in Outlook 365 Using Outlook 15
CWM550 Saving Data: Don't check certain folders Using Outlook 2
D Outlook 2016 Outlook not saving Sent Items Using Outlook 4
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
M Adding Subject to this Link-Saving VBA Outlook VBA and Custom Forms 5
L Attachment saving and tracking - PLEASE help! Outlook VBA and Custom Forms 5
B Saving items under a folder Using Outlook 3
R Quick Access view in File Explorer when saving attachments Using Outlook 0
N Saving And Deleting Outlook Attachments with Unknown Error Message Outlook VBA and Custom Forms 1
V Saving attachment from outlook in My Documents Outlook VBA and Custom Forms 14
A saving attachement to folder named the same as rule name Outlook VBA and Custom Forms 0
T Saving all email to file folder in Windows Using Outlook 2
J Saving attachments from specific sender (phone number) to specific folder on hard drive Using Outlook 3
C Saving Outlook attachments and links to attachments with VBA Outlook VBA and Custom Forms 2
R Outlook 2010 Modify Style "Do not check spelling or grammar" not saving Outlook VBA and Custom Forms 0
R Outlook Office 365 not saving addresses Using Outlook 0
A Keep color categories when saving vCards Using Outlook 1
P Saving All Messages to the Hard Drive Using VBA Outlook VBA and Custom Forms 5
e_a_g_l_e_p_i question about saving my .pst so I can import it to my Outlook after I build a new system Using Outlook 10
S Editing an email with notes and saving it for record using Macro Outlook VBA and Custom Forms 3
O Saving Attachments to folder on disk and adding Initials to end of file name Outlook VBA and Custom Forms 9
J Outlook 2013 crashes saving VBA & clicking tools | digital signature Outlook VBA and Custom Forms 1
bifjamod Saving sent email to specific folder based on category with wildcard Outlook VBA and Custom Forms 1
N Saving .msg as sent item on send Outlook VBA and Custom Forms 1
erichamion Changes to meeting body not properly saving Outlook VBA and Custom Forms 4
A ItemAdd on Imap Folder get endless loop after saving item Using Outlook 5
T Saving Outlook 2010 email with attachments but read the email without Outlook Using Outlook 2
T From Field Blank when saving to folder other than Sent items Using Outlook 2
L Outlook DST (Daylight Saving Time) problem Using Outlook 0
F Using Outlook 2007 as an IMAP Mail Station Without Saving Data Locally Using Outlook 2
E Saving Changes To Edited E-Mail Received Message Using Outlook 0
D File Lock issue when saving message from Outlook to new folder Using Outlook 1
D Remove extension while saving attachments Using Outlook 1
K Printing & Saving Outlook Contacts Using Outlook 3
S Not saving attachments in the Sent Folder Using Outlook 2
W Default Saving a message as text Using Outlook 2
R Outlook 2007 QAT buttons not saving Using Outlook 2
C Exchange 2003 - Outlook 2003 - Calendar entries saving over each other Using Outlook 2
J Saving Published Outlook Form as msg Using Outlook 1
J Saving recent colors used for fonts in an email? Using Outlook 1
B How to choose which contacts folder to use when saving contacts? Using Outlook 1

Similar threads

Back
Top