Get full path to a file - vba

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
This code here get me only the name of the selected file with its extension.

Code:
[COLOR=#0C0D0E][FONT='inherit'][FONT=inherit]<code style="box-sizing: inherit; margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace; vertical-align: baseline; max-height: 300px; overflow: auto;">[COLOR=#101094]Sub[/COLOR][COLOR=#303336] GetFilePath[/COLOR][COLOR=#303336]()[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]Set[/COLOR][COLOR=#303336] myFile [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] Application[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]FileDialog[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]msoFileDialogOpen[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336] myFile

[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Title [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]"Choose File"[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]AllowMultiSelect [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]False[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]If[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Show [/COLOR][COLOR=#303336]<>[/COLOR][COLOR=#303336]-[/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#101094]Then[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]Exit[/COLOR][COLOR=#101094]Sub[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]If[/COLOR][COLOR=#303336]

FileSelected [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] Replace[/COLOR][COLOR=#303336](.[/COLOR][COLOR=#303336]SelectedItems[/COLOR][COLOR=#303336]([/COLOR][COLOR=#7D2727]1[/COLOR][COLOR=#303336]),[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]InitialFileName[/COLOR][COLOR=#303336],[/COLOR][COLOR=#7D2727]""[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]With[/COLOR][COLOR=#303336]

[/COLOR][COLOR=#303336]MsgBox FileSelected

[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]Sub
[/COLOR]</code>[/FONT][/FONT][/COLOR]

When I change the line

Code:
[COLOR=#303336][FONT=Consolas]FileSelected [/FONT][/COLOR][COLOR=#303336][FONT=Consolas]=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] Replace[/FONT][/COLOR][COLOR=#303336][FONT=Consolas](.[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]SelectedItems[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]([/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas]1[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]),[/FONT][/COLOR][COLOR=#303336][FONT=Consolas].[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]InitialFileName[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas]""[/FONT][/COLOR][COLOR=#303336][FONT=Consolas])[/FONT][/COLOR]


To this
Code:
[COLOR=#303336][FONT=Consolas]FileSelected [/FONT][/COLOR][COLOR=#303336][FONT=Consolas]=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas].[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]SelectedItems[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]([/FONT][/COLOR][COLOR=#7D2727][FONT=Consolas]1[/FONT][/COLOR][COLOR=#303336][FONT=Consolas])[/FONT][/COLOR]

It adds the name of the file and it's extension to the path name.

Now what I want to do is to get the path name only up to the last path separator . I have been digging around for a while now but can't seem to find out how to yet.

I need a hand with that. Thanks
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Just use
Code:
FileSelected = .InitialFileName
 
Upvote 0
Glad to help & thanks for the feedback
 
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