PDA

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



gibbo1715
10-14-2006, 11:38 AM
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

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

fumei
10-14-2006, 09:16 PM
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??? .Name = "*.*"
.Display
Dim NameVar As String
NameVar = .Name.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:If Right(FullNameOfDoc, 1) = "\" Then 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:Sub Test()
Dim FullNameOfDoc As String
With Dialogs(wdDialogFileOpen)
.Name = "*.*"
.Display
FullNameOfDoc = CurDir & _
Application.PathSeparator & .Name
End With
MsgBox FullNameOfDoc
End SubThis does exactly the same as your code.

fumei
10-14-2006, 09:32 PM
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.ChangeFileOpenDirectory "C:\test\"
MsgBox CurDirwill display "C:test" - no "\".

There is one exception - the root. ChangeFileOpenDirectory "C:\"
MsgBox CurDirWILL display "C:\"

mdmackillop
10-15-2006, 12:43 AM
Sub Test()
Dim FullNameOfDoc As String
With Dialogs(wdDialogFileOpen)
.Name = "*.*"
.Display
FullNameOfDoc = CurDir & _
Application.PathSeparator & .Name
End With
MsgBox FullNameOfDoc
End Sub

Hi Gerry,
I thought that too, until I tried a file on the root directory which returns
C:\\Test.doc

fumei
10-15-2006, 06:13 AM
Correct, and that is because of the ONE exception to VBA returning a path. The Root always has "\". So properly, it should be:If CurDir = "C:\" Then
FullNameOfDoc = CurDir & .Name
Else
FullNameOfDoc = CurDir & Application.PathSeparator _
& .Name
End If

fumei
10-15-2006, 06:17 AM
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.

gibbo1715
10-15-2006, 08:17 AM
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

mdmackillop
10-15-2006, 10:27 AM
To generalise Gerry's solution, for those of us on networks or using mapped drive letters

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

gibbo1715
10-16-2006, 11:01 AM
Thanks All

To include the cancel button I have ended up with

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

Thanks Again

Gibbo

fumei
10-17-2006, 01:19 AM
Ah, much better.