GetOpenFileName to select a .URL file

G

Gary''s Student

I need to pick a file (including the path), I have:

Sub pickOne()
s = Application.GetOpenFilename
MsgBox (s)
End Sub

to select a file. If I select a .xls file, I see the full path and file name:

C:\Temp\x.xls

If I select an internet shortcut, I see:
http://www.cnn.com

What I need is:
C:\Temp\x.url
the full path and filename


Can I get GetOpenFileName to give me the full path and filename, or should I
be using something else?
 
J

Joel

The excel FILE commands don't want to see the "http:" as part of the file
name. HTTP is the protocol for transfering the file, not the name. You can
open a file by using "//root/subdirect/abc.xls". Don't use
"http://root/subdirect/abc.xls"

To be able to see network files, you have to mount the network directory on
you computer. You can do in the windows explorer (not internet) using the
menu Tools - Mount Network Drive. Once the Netwrok dirive is mounted then
you can use GetOpenFilename.
 
Z

Zack Barresse

You're wanting to use the GetOpenFileName() to get the file name/path of a
file with the extension "url"? Just use that as your passed syntax for file
types, which you can use to filter it out. A google search will yield many
results.
 
G

Gary''s Student

Thank you Joel. I am, however, not trying to open the file, just get its
full path-name. If we run the code, it brings up the Open File Dialog. If
we pick a file using the Dialog, the msgbox displays the full path-filename.
For all files except .url files.

I need something to work for all files type in the same way.
 
G

Gary''s Student

Thank you Zack.

Using your suggestion does restrict the files displayed in the Dialog box.
If I click on a file with the extension .url, I do not get the path-filename,
I still get the url value itself.

I don't need and don't want the target of the url:

http://www.......

I need the path and file name of the file I selected in the Dialog box:

C:\Documents and Settings\Owner\Favorites\news.url
--
Gary''s Student - gsnu200781


Zack Barresse said:
You're wanting to use the GetOpenFileName() to get the file name/path of a
file with the extension "url"? Just use that as your passed syntax for file
types, which you can use to filter it out. A google search will yield many
results.
 
R

Rick Rothstein \(MVP - VB\)

Give the code after my signature a try... I think it does what you want.
Note that this link (for C++) has a listing of the various constants that
can be set for the "flags" member of the OPENFILENAME Type (structure),
although the actual values are not shown and will need to be Googled for.
The subroutine you will call is named PickOne (like your own subroutine was
named).

Rick

**************** START OF CODE ****************
Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" _
(pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Sub PickOne()
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
'Set the parent window
OFName.hwndOwner = Application.Hwnd
'Set the application's instance
OFName.hInstance = Application.hInstance
'Select a filter
OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + _
Chr$(0) + "All Files (*.*)" + Chr$(0) + _
"*.*" + Chr$(0)
'create a buffer for the file
OFName.lpstrFile = Space$(254)
'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:\"
'Set the title
OFName.lpstrTitle = "Get Path And Filename"
'No flags
OFName.flags = 0
'Show the 'Open File'-dialog
If GetOpenFileName(OFName) Then
MsgBox Trim$(OFName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If
End Sub
**************** END OF CODE ****************
 
Z

Zack Barresse

Mine was similar to Rick's, but uses the same API...


Option Explicit

Private Declare Function GetOpenFileName _
Lib "comdlg32.dll" _
Alias "GetOpenFileNameA" ( _
pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Sub Open_Comdlg32()
'Originally from Ivan F. Moala: http://www.xcelfiles.com/comdlg.html
Dim OpenFile As OPENFILENAME, lReturn As Long, strFilter As String,
FileToOpen As String
OpenFile.lStructSize = Len(OpenFile)
strFilter = "My Files (*.url)" & Chr(0) & "*.url" & Chr(0)
With OpenFile
.lpstrFilter = strFilter
.nFilterIndex = 1
.lpstrFile = String(257, 0)
.nMaxFile = Len(.lpstrFile) - 1
.lpstrFileTitle = .lpstrFile
.nMaxFileTitle = .nMaxFile
.lpstrInitialDir = "C:\Users\Zack\Desktop\"
.lpstrTitle = "My FileFilter Open"
.flags = 0
End With
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "User cancelled"
Else
FileToOpen = Application.WorksheetFunction.Clean(OpenFile.lpstrFile)
MsgBox FileToOpen, vbInformation, "FILE PATH & NAME"
End If
End Sub



HTH


--
Zack Barresse



Gary''s Student said:
Thank you Zack.

Using your suggestion does restrict the files displayed in the Dialog box.
If I click on a file with the extension .url, I do not get the
path-filename,
I still get the url value itself.

I don't need and don't want the target of the url:

http://www.......

I need the path and file name of the file I selected in the Dialog box:

C:\Documents and Settings\Owner\Favorites\news.url
 
G

Gary''s Student

Hi Rick:

The code works perfectly!

Thanks for coming to my rescue (once again)!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top