ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open File (https://www.excelbanter.com/excel-programming/329132-open-file.html)

PraxisPete

Open File
 
Hi, I am new to VB and cant find how to get my macro to open a file. I know
the location of the file but not the filename, I need the user to pick which
file to open. Basically I need the VB to display Excels Open Dialog Box

Many thanks in advance


Harald Staff

Open File
 
Sub test()
Dim F As Variant
Dim Myfolder As String
Myfolder = "C:\Temp"
ChDrive Myfolder
ChDir Myfolder

F = Application.GetOpenFilename("XL Files (*.xl*), *.XL*")
If F = False Then Exit Sub
Workbooks.Open F
End Sub

HTH. Best wishes Harald

"PraxisPete" skrev i melding
...
Hi, I am new to VB and can't find how to get my macro to open a file. I

know
the location of the file but not the filename, I need the user to pick

which
file to open. Basically I need the VB to display Excel's Open Dialog Box

Many thanks in advance




PraxisPete

Open File
 
Thanks for that Harald, a further development of that is, if all the files
that could be opened required the same password and were to be open as read
only, can this be coded in?

Thank you again

"Harald Staff" wrote:

Sub test()
Dim F As Variant
Dim Myfolder As String
Myfolder = "C:\Temp"
ChDrive Myfolder
ChDir Myfolder

F = Application.GetOpenFilename("XL Files (*.xl*), *.XL*")
If F = False Then Exit Sub
Workbooks.Open F
End Sub

HTH. Best wishes Harald

"PraxisPete" skrev i melding
...
Hi, I am new to VB and can't find how to get my macro to open a file. I

know
the location of the file but not the filename, I need the user to pick

which
file to open. Basically I need the VB to display Excel's Open Dialog Box

Many thanks in advance





NickHK

Open File
 
Pete,
Read up on the optional parameters to .Open in the help.

NickHK

"PraxisPete" wrote in message
...
Thanks for that Harald, a further development of that is, if all the files
that could be opened required the same password and were to be open as

read
only, can this be coded in?

Thank you again

"Harald Staff" wrote:

Sub test()
Dim F As Variant
Dim Myfolder As String
Myfolder = "C:\Temp"
ChDrive Myfolder
ChDir Myfolder

F = Application.GetOpenFilename("XL Files (*.xl*), *.XL*")
If F = False Then Exit Sub
Workbooks.Open F
End Sub

HTH. Best wishes Harald

"PraxisPete" skrev i melding
...
Hi, I am new to VB and can't find how to get my macro to open a file.

I
know
the location of the file but not the filename, I need the user to pick

which
file to open. Basically I need the VB to display Excel's Open Dialog

Box

Many thanks in advance







PraxisPete

Open File
 
Thanks NickHK,
I've read that and can see that there are parameters for ReadOnly and
Password, but, as I understand it, the Open Method requires the FileName
which I do not have, the GetOpenFilename does not need the Filename, but does
not have parameters for ReadOnly and Password. Its like I need a combination
of both.
Many thanks.

"NickHK" wrote:

Pete,
Read up on the optional parameters to .Open in the help.

NickHK

"PraxisPete" wrote in message
...
Thanks for that Harald, a further development of that is, if all the files
that could be opened required the same password and were to be open as

read
only, can this be coded in?

Thank you again

"Harald Staff" wrote:

Sub test()
Dim F As Variant
Dim Myfolder As String
Myfolder = "C:\Temp"
ChDrive Myfolder
ChDir Myfolder

F = Application.GetOpenFilename("XL Files (*.xl*), *.XL*")
If F = False Then Exit Sub
Workbooks.Open F
End Sub

HTH. Best wishes Harald

"PraxisPete" skrev i melding
...
Hi, I am new to VB and can't find how to get my macro to open a file.

I
know
the location of the file but not the filename, I need the user to pick
which
file to open. Basically I need the VB to display Excel's Open Dialog

Box

Many thanks in advance








Harald Staff

Open File
 
No, very wrong.
GetOpenFilename does just what it says, gets the filename for a file to be
opened. It doesn NOT open the file. The filename is F in my code.

This F is passed to the Open method in the last line, which is where you add
the parameters:

Workbooks.Open F, , True, , "Password"

HTH. best wishes Harald

"PraxisPete" skrev i melding
...
Thanks NickHK,
I've read that and can see that there are parameters for ReadOnly and
Password, but, as I understand it, the Open Method requires the FileName
which I do not have, the GetOpenFilename does not need the Filename, but
does
not have parameters for ReadOnly and Password. Its like I need a
combination
of both.
Many thanks.




PraxisPete

Open File
 
Thank you all for your help. You will have to forgive me for being a bit
slow on the uptake, I am starter at this, but I do understand now.

"Harald Staff" wrote:

No, very wrong.
GetOpenFilename does just what it says, gets the filename for a file to be
opened. It doesn NOT open the file. The filename is F in my code.

This F is passed to the Open method in the last line, which is where you add
the parameters:

Workbooks.Open F, , True, , "Password"

HTH. best wishes Harald

"PraxisPete" skrev i melding
...
Thanks NickHK,
I've read that and can see that there are parameters for ReadOnly and
Password, but, as I understand it, the Open Method requires the FileName
which I do not have, the GetOpenFilename does not need the Filename, but
does
not have parameters for ReadOnly and Password. Its like I need a
combination
of both.
Many thanks.





Harald Staff

Open File
 
No problem. Glad to assist.

Best wishes Harald.

"PraxisPete" skrev i melding
...
Thank you all for your help. You will have to forgive me for being a bit
slow on the uptake, I am starter at this, but I do understand now.





All times are GMT +1. The time now is 12:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com