Word VBA - wdDialogFileOpen can I specify path?


M

mr tom

I'm trying to build something intuitive - esp for users not confident with
file navigation.

Part of the code will involve opening another file (file name varies - to be
selected by the user), but the path is fixed, e.g. j:\salestargets\xxx.doc,
where xxx could be anything.

I think the best approcah is:
Application.Dialogs (wdDialogFileOpen).Show
but I've looked at the list of arguements, and can't see one which allows a
particular path to be specified - e.g:
Application.Dialogs (wdDialogFileOpen).Show Path:=j:\salestargets\*.doc

Anybody got any ideas? Is there a more suitable way to code this?

Many thanks,

Tom.
 
Ad

Advertisements

D

Dr. Stephan Kassanke

mr tom said:
I'm trying to build something intuitive - esp for users not confident with
file navigation.

Part of the code will involve opening another file (file name varies - to
be
selected by the user), but the path is fixed, e.g.
j:\salestargets\xxx.doc,
where xxx could be anything.

I think the best approcah is:
Application.Dialogs (wdDialogFileOpen).Show
but I've looked at the list of arguements, and can't see one which allows
a
particular path to be specified - e.g:
Application.Dialogs (wdDialogFileOpen).Show Path:=j:\salestargets\*.doc

Anybody got any ideas? Is there a more suitable way to code this?

Many thanks,

Tom.
Tom,

http://groups-beta.google.com/group/microsoft.public.excel.programming/browse_thread/thread/5c672302bedf1e9b/d8f4c722421f8777?q=vba+api+select+file+set+path&rnum=5&hl=en#d8f4c722421f8777

includes some sample class code and should be exactly what you are looking
for.

cheers, Stephan
 
M

mr tom

Thank you.

That would certainly filter the files to .doc (or .xls in the example), but
doesn't allow me to specify a path.
Any ideas how I can do that using Application.Dialogs
(wdDialogFileOpen).Show (or similar)?

Many thanks,

Tom.
 
D

Dr. Stephan Kassanke

mr tom said:
Thank you.

That would certainly filter the files to .doc (or .xls in the example),
but
doesn't allow me to specify a path.
Any ideas how I can do that using Application.Dialogs
(wdDialogFileOpen).Show (or similar)?

Many thanks,

Tom.
Hi Tom,

oh yes, it does allow you to specifiy the path.

.sFileTitle = vbNullChar & Space$(512) & _
vbNullChar & vbNullChar
.nMaxTitle = Len(OFN.sFileTitle)
'Starting folder, double-null terminated
.sInitialDir = ActivePresentation.Path & vbNullChar


.sDialogTitle = sTitle 'the dialog title string

set .sInitialDir to "c:\" or whatever and the dialog starts in that
directory.

Another example is
http://support.microsoft.com/default.aspx?scid=kb;en-us;161286

OpenFile.lpstrInitialDir = "C:\"


hth,
Stephan
 
M

mr tom

Dear Stephan,

You're right - it does look like it should do the job!

However, when I've copied it into the Word VBA Editor, the thing falls over.

Perhaps this is due to my degree of inexperience in coding - it's a little
more advanced than anything I've done previously - is this in fact VBA code?
If not, can the VBA editor read it?

Sorry for not understanding more quickly - I really am grateful for the help
you've given so far!

Many thanks,

Tom.
 
J

Jonathan West

You can use the built-in file open dialog with a pre-selected folder, like
this

Sub OpenDialog()
With Dialogs(wdDialogFileOpen)
.Name = "J:\salestargets"
.Show
End With
End Sub

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
Ad

Advertisements

D

Dr. Stephan Kassanke

mr tom said:
Dear Stephan,

You're right - it does look like it should do the job!

However, when I've copied it into the Word VBA Editor, the thing falls
over.

Perhaps this is due to my degree of inexperience in coding - it's a little
more advanced than anything I've done previously - is this in fact VBA
code?
If not, can the VBA editor read it?

Sorry for not understanding more quickly - I really am grateful for the
help
you've given so far!

Many thanks,

Tom.
Tom,

no problem at all, we all started some day. The sample
http://support.microsoft.com/default.aspx?scid=kb;en-us;161286 is intended
for VB5 but compiles ok with a few modifications.

VBA does not offer .hWnd and .hInstance for Fors but for the application
object. So you have to replace the following lines

OpenFile.hwndOwner = Form1.hWnd
OpenFile.hInstance = App.hInstance


with

OpenFile.hwndOwner = Application.Hwnd
OpenFile.hInstance = Application.hInstance

I am not sure wether thes properties are available in pre Office 2003
versions. Just in case, you can pass 0 to hwndowner and hInstance as a
workaround.

The rest of the code uses the API (application programming interface), a
modular approach where MS Windows offers functionality by Dynamc link
libraries and the API. In VBA you declare the functions, e.g.

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

and call them in your program. The DLL does the job and passes the paramters
back you your VBA program. In this sense, yes, advanced VBA programming but
no magic ;-)

Good luck ;-)

Stephan
 
D

Dr. Stephan Kassanke

Dear Jonathan,

i did not know that the built-in dialogs offer a similar degree of control
as the API approach. Thanks!
Stephan
 
Ad

Advertisements

Joined
Nov 26, 2013
Messages
2
Reaction score
0
Nope, doesn't work. Returns to last folder accessed.


You can use the built-in file open dialog with a pre-selected folder, like
this

Sub OpenDialog()
With Dialogs(wdDialogFileOpen)
.Name = "J:\salestargets"
.Show
End With
End Sub

--
 

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