Prompt User for Text File

K

Ken Hudson

In Excel I can use the following code to prompt a user to open a text file.
With this code only text files are displayed in the file open window.

FileOpenName = Application.GetOpenFilename("Text Files (*.txt),*.txt", 1,
"Find Text File", , False)

Is there a way to get the same result in a Word macro?

TIA.
 
B

Bear

Ken:

You'll need to use Word's built-in dialog. I can't seem to make the .Format
property work as I'd expect, but the following does what you want.

With Application.Dialogs(wdDialogFileOpen)
.Name = "*.TXT"
' .Format = wdOpenFormatText
.Show
End With

Bear
 
K

Ken Hudson

Thanks Bear.
And is there a way to include a message prompt when that dialog box opens?
E.g. "Find text file showing your sales."
 
B

Bear

Ken:

I don't know of any simple way to alter the title of a built-in dialog box.

You can easily display a message before the dialog box opens.

MsgBox "Browse to the text file that shows your sales.", vbInformation,
"Ken's Tools"

Displays a box with just an Ok button.

Bear
 
P

Perry

Dim d As FileDialog
Set d = Application.FileDialog(msoFileDialogOpen)
d.Title = "search textfiles"

d.Filters.Add "My Textfiles (*.txt)", "*.txt"

d.FilterIndex = d.Filters.Count
If d.Show Then
sFileName = d.SelectedItems(0)
End If


--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
R

Russ

What version of VBA does this code work with? Does a reference to a library
or .dll have to be made to get it to work in Word VBA? Or will it only work
in Visual Basic or Visto?
 
S

Shauna Kelly

Hi Russ

For what it's worth, Perry's code runs for me in VBA in Word 2003 if I make
two tiny changes as follows: add the Dim statement to create the variable,
and choose .SelectedItems(1), not (0).

Dim sFileName As String

Dim d As FileDialog
Set d = Application.FileDialog(msoFileDialogOpen)
d.Title = "search textfiles"

d.Filters.Add "My Textfiles (*.txt)", "*.txt"

d.FilterIndex = d.Filters.Count
If d.Show Then
sFileName = d.SelectedItems(1)
End If

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
R

Russ

Shauna,
Thanks for the reply.
It must be the version of VBA.
The macro doesn't seem to work on Word 97 at work or MacWord 2004 at home.
An error message highlights 'd as FileDialog' and says 'User-defined type
not defined'.
 
R

Russ

Thanks Shauna,
I have been using the alternatives in Word97 and the workaround to be able
to pick multiple items through code. I was hoping that a simple reference in
Word97 would allow me to use the newer FileDialog object that allows titles,
selecteditems, filters, etc.
 

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