Using msoFileDialogFolderPicker to select a folder

Anonymous
2014-09-18T12:08:33+00:00

Hello

The following code is written to allow the user to select the folder where they wish to save a PDF of the worksheet and the file name will consist of data located in cells D4 and D5.  It also checks to confirm that the data in cell D5 contains 3 characters.  The code works fine but with one issue.  If the user drills down say 4 levels of subfolders the file does not save in the folder, it saves in the folder one level up. 

I can't figure out why the file is not saved it the folder selected.

Can someone indicate what I am missing?

Thank you

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2014-09-18T13:00:19+00:00

    Hi

    Have a try with this

    Dim strCurrDir As String

    Dim fldpath As String

    Debug.Print CurDir

    strCurrDir = CurDir 'keep track of current directory

    Application.FileDialog(msoFileDialogFolderPicker).Show

    Debug.Print CurDir

    fldpath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

    ChDir fldpath 'chang to the new directory

    Debug.Print CurDir

    'Do your stuff

    ChDir strCurrDir 'reset to previous current directory

    End Sub

    HTH

    Regards

    JY

    2 people found this answer helpful.
    0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-09-18T13:33:52+00:00

    Post your actual code, not just a picture of it....

    Do you know how I can paste a copy of my code instead of typing it in?  Paste does not work for me?

    0 comments No comments
  2. Anonymous
    2014-09-18T13:43:07+00:00

    Hi

    Have a try with this

    Dim strCurrDir As String

    Dim fldpath As String

    Debug.Print CurDir

    strCurrDir = CurDir 'keep track of current directory

    Application.FileDialog(msoFileDialogFolderPicker).Show

    Debug.Print CurDir

    fldpath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

    ChDir fldpath 'chang to the new directory

    Debug.Print CurDir

    'Do your stuff

    ChDir strCurrDir 'reset to previous current directory

    End Sub

    HTH

    Regards

    JY

    Changing to the new directory before running the Export worked.

    Thank you  JY

    0 comments No comments
  3. Anonymous
    2014-09-18T13:52:50+00:00

    I just copy and paste - but a picture will do. I thought you may have a error in your code and it is easier with actual code.

    What happens with

    Sub TestMacro()

        Dim FldPath As String

        Application.FileDialog(msoFileDialogFolderPicker).Show

        FldPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

        ActiveSheet.ExportAsFixedFormat xlTypePDF, FldPath & "\Eng test export.pdf"

    End Sub

    0 comments No comments
  4. Anonymous
    2014-09-18T14:01:41+00:00

    I just copy and paste - but a picture will do. I thought you may have a error in your code and it is easier with actual code.

    What happens with

    Sub TestMacro()

        Dim FldPath As String

        Application.FileDialog(msoFileDialogFolderPicker).Show

        FldPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

        ActiveSheet.ExportAsFixedFormat xlTypePDF, FldPath & "\Eng test export.pdf"

    End Sub

    For some reason I am not able to paste to this window.

    I have added a line

    "ChDir fldpath" right after the "fldpath = Application..........." line and the code is working fine now.

    Thanks for your input.

    0 comments No comments