Consulting

Results 1 to 10 of 10

Thread: Solved: Better way to get full filepath and name into string

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Solved: Better way to get full filepath and name into string

    All

    I am trying to find an easy way to get a file path/ name into a string, i ve come up with the following but seems a bit long winded, is there a shorter way

    Gibbo

    [VBA]Sub Test()
    With Dialogs(wdDialogFileOpen)
    .Name = "*.*"
    .Display
    Dim NameVar As String
    NameVar = .Name
    FullNameOfDoc = CurDir
    If Right(FullNameOfDoc, 1) = "\" Then
    FullNameOfDoc = CurDir & NameVar
    Else
    FullNameOfDoc = CurDir & "\" & NameVar
    FullNameOfDoc = Replace(FullNameOfDoc, Chr(34), "")
    End If
    End With
    MsgBox FullNameOfDoc
    End Sub[/VBA]

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Huh?????????
    I am trying to find an easy way to get a file path/ name into a string,
    Uh....what file path/name???

    This is strange code.

    First of all, it seems you are NOT using Option Explicit. I am, so of course your code fails if I try to run it. Not a problem, I just actually declared FullNameOfDoc. However, I do recommend that you use Option Explicit for your code.

    In any case...OK, I run your code. The FileOpen dialog is displayed. Ok, fine. NOW WHAT???[vba] .Name = "*.*"
    .Display
    Dim NameVar As String
    NameVar = .Name[/vba].Name is *.*, and then you make a string variable .Name. So, at this point NameVar = "*.*"...OK.

    Selecting a file from the displayed list and clicking Open...does not Open the file. Sure it does display the name of the file you selected - with your messagebox.

    But what the heck is the point to this????

    Also, I am not sure why people keep coding:[vba]If Right(FullNameOfDoc, 1) = "\" Then [/vba]Have any of you people EVER got the string of CurDir that DID have "\" at the end? Hmmmm? I never have. It is not at the end of ActiveDocument.Path either...but I see that IF statement in code often. Path properties do not have "\" at the end. It is not a valid path syntax. VBA will return, when asked, a valid path statement...which does not have "\" at the end. NO path string will ever come back with "\" at the end.

    Which is why - BTW - there is an object in VBA called....PathSeparator.

    OK, so bottom line...you are NOT opening a file, just displaying the FileOpen dialog...for some reason....and getting the path and name of a file the user selects. Of course if they press enter (selecting nothing) or Cancel, you get the CurDir\*.*. Which is very handy and useful I admit.

    But your question is regarding the longwindedness. And it is. So try - and I added the declaration of FullNameOfDoc as I DO use Option Explicit:[vba]Sub Test()
    Dim FullNameOfDoc As String
    With Dialogs(wdDialogFileOpen)
    .Name = "*.*"
    .Display
    FullNameOfDoc = CurDir & _
    Application.PathSeparator & .Name
    End With
    MsgBox FullNameOfDoc
    End Sub[/vba]This does exactly the same as your code.

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    My apologies. That sounds a bit snarky, which is not what I intended.

    I should also add that you can GIVE a path with a "\" to VBA, but it will never give one back.[vba]ChangeFileOpenDirectory "C:\test\"
    MsgBox CurDir[/vba]will display "C:test" - no "\".

    There is one exception - the root. [vba]ChangeFileOpenDirectory "C:\"
    MsgBox CurDir[/vba]WILL display "C:\"

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Test()
    Dim FullNameOfDoc As String
    With Dialogs(wdDialogFileOpen)
    .Name = "*.*"
    .Display
    FullNameOfDoc = CurDir & _
    Application.PathSeparator & .Name
    End With
    MsgBox FullNameOfDoc
    End Sub[/VBA]
    Hi Gerry,
    I thought that too, until I tried a file on the root directory which returns
    C:\\Test.doc
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Correct, and that is because of the ONE exception to VBA returning a path. The Root always has "\". So properly, it should be:[vba]If CurDir = "C:\" Then
    FullNameOfDoc = CurDir & .Name
    Else
    FullNameOfDoc = CurDir & Application.PathSeparator _
    & .Name
    End If[/vba]

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Which means....OK, I am wrong....there IS a reason to test for "\" at the end. You may have selected the root.

    Bingo. OK. OK. I'm wrong. I just never put data files in my root folder. In fact, there is never really any reason that I would be looking for anything in the root folder. But....there you go, maybe someone else would.

    But I never do, so I never use that - is the last character a "\" test.

  7. #7
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thanks for all the replies, made for an interesting debate, and No worries about sounding snakey Gerry, will never take offence when you regularly make my life so much easier.

    Gibbo

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    To generalise Gerry's solution, for those of us on networks or using mapped drive letters
    [VBA]
    Sub Test()
    With Dialogs(wdDialogFileOpen)
    .Name = "*.*"
    .Display
    If Len(CurDir) = 3 Then
    FullNameOfDoc = CurDir & .Name
    Else
    FullNameOfDoc = CurDir & Application.PathSeparator _
    & .Name
    End If
    End With
    MsgBox FullNameOfDoc
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thanks All

    To include the cancel button I have ended up with

    [vba]With Dialogs(wdDialogFileOpen)
    .Name = "*.doc"
    If .Display = -1 Then
    FullNameofDoc = CurDir
    If Len(CurDir) = 3 Then
    FullNameofDoc = CurDir & .Name
    Else
    FullNameofDoc = CurDir & Application.PathSeparator _
    & .Name
    End If
    Else
    Exit Sub
    End If
    End With[/vba]

    Thanks Again

    Gibbo

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ah, much better.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •