Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / June 2007

Tip: Looking for answers? Try searching our database.

Prompt User for Text File

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Hudson - 11 Apr 2007 17:16 GMT
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.
Signature

Ken Hudson

Bear - 11 Apr 2007 21:52 GMT
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

> 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.
[quoted text clipped - 5 lines]
>
> TIA.
Ken Hudson - 11 Apr 2007 22:14 GMT
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."
Signature

Ken Hudson

> Ken:
>
[quoted text clipped - 18 lines]
> >
> > TIA.
Bear - 11 Apr 2007 22:40 GMT
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
Perry - 11 Apr 2007 22:49 GMT
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

> Ken:
>
[quoted text clipped - 9 lines]
>
> Bear
Russ - 27 Jun 2007 07:31 GMT
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?

>     Dim d As FileDialog
>     Set d = Application.FileDialog(msoFileDialogOpen)
[quoted text clipped - 28 lines]
>>
>> Bear

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Shauna Kelly - 27 Jun 2007 11:38 GMT
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

> What version of VBA does this code work with? Does a reference to a
> library
[quoted text clipped - 34 lines]
>>>
>>> Bear
Russ - 27 Jun 2007 17:55 GMT
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'.

> Hi Russ
>
[quoted text clipped - 58 lines]
>>>>
>>>> Bear

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Shauna Kelly - 28 Jun 2007 08:33 GMT
Hi Russ

The FileDialog object wasn't invented till about Word 2002. Before that,
there was no equivalent. Look at the VBA section at http://www.word.mvps.org 
for alternatives that will work in Word 97.

Hope this helps.

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

> Shauna,
> Thanks for the reply.
[quoted text clipped - 68 lines]
>>>>>
>>>>> Bear
Russ - 30 Jun 2007 02:34 GMT
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.

> Hi Russ
>
[quoted text clipped - 79 lines]
>>>>>>
>>>>>> Bear

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.