How to pass the result of FolderPicker between macros

Bering

Board Regular
Joined
Aug 22, 2018
Messages
186
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

I have a macro that prompts the user to select a path:

Macro no.1

VBA Code:
   With Excel.Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "U:\MyPath\"
        If .Show = -1 Then ' if OK is pressed
         strFolderpath = .SelectedItems(1)

        End If
    End With

Macro no.1 then calls another Macro no.2 that creates a number of emails and SHOULD store any attachments in the strFolderpath:

Code:
   If strFolderpath <> "" Then ' if a file was chosen
'
'        ' Combine with the path to the Temp folder.
        strFile = strFolderpath
        ' Save the attachment as a file.
        oAttachments.Item(i).SaveAsFile strFile
        
    End If



Since the path is the same for all the emails, I don't want the folder picker prompt to appear each time. However, I cannot think of a way to pass the path to macro no. 2
Thank you in advance for any suggestion.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You would pass it as an argument - eg

Code:
Sub Macro2(strFolderPath as string)

and macro 1 simply passes it along:

Code:
macro2 strFolderpath
 
Upvote 1
Solution
You would pass it as an argument - eg

Code:
Sub Macro2(strFolderPath as string)

and macro 1 simply passes it along:

Code:
macro2 strFolderpath


Thank you for your reply! Unfortunately, I am still doing something wrong because the attachments do not get saved in strFolderpath ... Any idea? Thank you so much

Macro1

VBA Code:
Sub Macro1()
 
With Excel.Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "U:\MyPath\"
        If .Show = -1 Then ' if OK is pressed
         strFolderpath = .SelectedItems(1)

        End If
End With

Call Macro2 (strFolderPath)


VBA Code:

Macro2:

VBA Code:
Sub Email_to_Investors_2(strFolderPath As String)

If strFolderPath <> "" Then

         strFile = strFolderPath
        ' Save the attachment as a file.
        oAttachments.Item(i).SaveAsFile strFile
        
End If
 
Upvote 0
Thank you for your reply! Unfortunately, I am still doing something wrong because the attachments do not get saved in strFolderpath ... Any idea? Thank you so much

Macro1

VBA Code:
Sub Macro1()
 
With Excel.Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "U:\MyPath\"
        If .Show = -1 Then ' if OK is pressed
         strFolderpath = .SelectedItems(1)

        End If
End With

Call Macro2 (strFolderPath)


VBA Code:

Macro2:

VBA Code:
Sub Email_to_Investors_2(strFolderPath As String)

If strFolderPath <> "" Then

         strFile = strFolderPath
        ' Save the attachment as a file.
        oAttachments.Item(i).SaveAsFile strFile
       
End If

Solved by declaring

Public strFolderpath as String
beween Option Explicit and sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top