Pathname and Filename

K

Karen

I really need help, I've been trying all day to find the appropriate VBA
code on the Microsoft site, at the mvps.org side, in the VBA help, in Google
groups, etc.....and it just can't be this difficult.

All I want to do is open a dialog box (preferably, I really don't want to
build a userform) and have the user select a graphic to insert into a Word
doc.

Things I'd like to do:

I'd like the dialog box to open to the user's My_Pictures folder
I want the dialog box to be looking for graphics files so I'd like to
specify .jpg, .gif etc.

When all is said and done I need to path to whatever directory the user
moved to and I need the filename that the user selected.

Any help will be very appreciated.

Karen
 
J

Jezebel

The obvious way to do this is to use the CommonDialog control -- but that
requires a UserForm to hold the control. (You don't have to display the
form, by the way: you can use the control without the form being visible).

It's also possible to use the built-in dialogs (InsertObject or
InsertPicture). In both cases - as with all built-in dialogs - it's possible
to set the defaults, display the dialog, and retrieve the user's selections,
without Word itself acting on the information. But for your current purpose,
unless you have seriously strong objections to UserForms, the CommonDialog
would be less work. Your UserForm doesn't even need any code in it:

1. Create a UserForm and add a common dialog control to it.

2. Create a macro with this code:

Sub GetPictureFile()

With New UserForm1
With .CommonDialog1
.ShowOpen
End With
End With

End Sub

3. Set the dialog's initial values (starting folder, file selection filter,
flags, etc) immediately before the ShowOpen statement.

4. Read the dialog result (remember that the user might cancel) immediately
after the ShowOpen.
 
P

Peter Hewett

Hi Karen

It pretty easy, try the following which uses Word built in dialog rather than the nasty
common dialog control:

Public Sub TestDialog()
Dim strPhotoPath As String

With Application.Dialogs(wdDialogInsertPicture)
.Display
strPhotoPath = .Name
End With
MsgBox strPhotoPath
End Sub

HTH + Cheers - Peter
 
K

Karen

Peter,

Thanks so much, it is so frustrating, who would have know that .name for
wdDialogInsertPicture does include the path where .name for wdDialogFindFile
does not?

Any advice on setting the initial directory to 'My Pictures' folder? I'm
trying this code and it doesn't error out but it also does not change the
directory:


Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(My_Pictures)
Set objfolderitem = objFolder.Self

ChangeFileOpenDirectory objfolderitem.Path

With Dialogs(wdDialogInsertPicture)
.Display
MyFile= .Name
End With

Karen
 
K

Karen

Jezebel,

Thanks for this. I've been so gun-shy about using the CommonDialog control
because I have to distribute this and there are so many dire warnings about
compatibility across systems.

On the other hand, it sure is tempting because it lets me 'tailor' it much
better.

Karen
 
J

Jezebel

I've distributed a lot of applications. Compatability is always a concern,
but the CommonDialog has never caused me a problem.
 
P

Peter Hewett

Hi Karen

I's on my "to investigate" list. One of the unfortunate aspects of the Built In Dialogs
is very poor documentation. There may be a property that you can set that controls the
startup folder but I don't know it. As you've seen changing the current drive/folder does
not affect the dialog. I suspect this is controlled by a registry key.

Sorry - Peter


Peter,

Thanks so much, it is so frustrating, who would have know that .name for
wdDialogInsertPicture does include the path where .name for wdDialogFindFile
does not?

Any advice on setting the initial directory to 'My Pictures' folder? I'm
trying this code and it doesn't error out but it also does not change the
directory:


Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(My_Pictures)
Set objfolderitem = objFolder.Self

ChangeFileOpenDirectory objfolderitem.Path

With Dialogs(wdDialogInsertPicture)
.Display
MyFile= .Name
End With

Karen

HTH + Cheers - Peter
 
J

Jezebel

The wdDialogInsertPicture dialog uses whatever is set as the Options picture
path: set this to whatever you want before calling the dialog.

Dim pOldPath as string

'Get the current folder
pOldPath = Options.DefaultFilePath(Path:=wdPicturesPath)

'Set the new path
Options.DefaultFilePath(Path:=wdPicturesPath) = ...whatever folder you want
to use

'Call the dialog
With Dialogs(wdDialogInsertPicture)
.Display
MyFile= .Name
End With

'Restore the folder
Options.DefaultFilePath(Path:=wdPicturesPath) = pOldPath
 
P

Peter Hewett

Hi Karen

You need to be aware that if you are deploying this template in a corporate environment
system policies may prevent you from changing any of the setting you can see under
Tools>Options>File Locations. If you change the path and it still returns the old value
rather than the new value then you've been blocked by policy enforcement.

HTH + Cheers - Peter
 
K

Karen

Peter and Jezebel,

Thanks so much. I'm happily pursuing both methods to find the one that
really meets my needs.

I'm not distributing to a corporate environment but rather to a lot of small
business users so we'll see.

Karen
 

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