Share via

Folder Picker Problems

Anonymous
2011-08-30T20:47:33+00:00

I seem to be having an issue with a some VBA code that I wrote.  It will create a PDF of the selected worksheet, naming it based on some predefined criteria.  It creates the PDF just fine.  The problem is that if I choose a folder to save it to, it saves it to one folder up from that.  So if I choose C:\My Documents\My Stuff it will save it to C:\My Documents.  Any thoughts?

Here's the code for the Folder Picker sections:

Function GetSaveToFolder() As String

' Delcare a variable as a FileDialog Object

Dim fd As fileDialog

Dim newFolder As String

GetSaveToFolder = ""

' Create a FileDialog object as a File Picker Dialog box

Set fd = Application.fileDialog(msoFileDialogFolderPicker)

' Use a With...End With block to reference the FileDialog object.

With fd

' Use the show method to display the file picker dialog box and return the user's action.

' The user pressed the action button.

If .Show = -1 Then

GetSaveToFolder = .SelectedItems(1)

' at this time, the path does not have a "" at the end of it

' rather than just adding one on to it, we test to see if it is there

' and if not, then add it

' This keeps it working if they ever change the way

' the folder picker works

If Right(GetSaveToFolder, 1) <> Application.PathSeparator Then

GetSaveToFolder = GetSaveToFolder & Application.PathSeparator

End If

Else

' the user pressed cancel

End If

End With

' set the object variable to Nothing

Set fd = Nothing

End Function

Harlan

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

Answer accepted by question author

HansV 462.6K Reputation points
2011-08-31T05:53:07+00:00

You assemble newFileName, including folderPath, before assigning a value to folderPath. At that point, folderPath is an empty string "".

You assign a value to folderPath later on, but you don't use it for anything.

You should move

folderPath = GetSaveToFolder()

up to before you use it in

newFileName = folderPath & entityName & " - "

Was this answer helpful?

0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-08-31T16:35:59+00:00

    That did it.  Thanks Hans.  

    You've been very helpful and seem to be very good at what you do.  Would you be willing to take a look at one of the other problems I've been having.  I haven't been able to get any help on this.

    http://answers.microsoft.com/en-us/office/forum/office_2010-customize/manipulate-an-add-in/e827e9d7-a64a-e011-8dfc-68b599b31bf5

    Thanks

    Harlan

    Was this answer helpful?

    0 comments No comments