dialog box help

L

LEU

I have been working with Perry (See My original post under Dialog Box) and he
gave me the following macro to try. I thought it had worked and stated that
it did on my reply to Perry. I was wrong. It does not write the formfields
data to (and saved in) the textfile. I need some help with this.

Dim tempFile
Dim fd As FileDialog
Dim oFF as formfields
Dim i as integer
set oFF = activedocument.formfields

i = FreeFile()

Set fd = Application.FileDialog(3)
fd.Filters.Add "Text Files", "*.txt"
fd.FilterIndex = 0
fd.InitialFileName = "U:\History"
fd.AllowMultiSelect = False
If fd.Show Then
tempFile = fd.SelectedItems(i)
Open tempfile For Output As #i
Write #i, oFF(5).Result, oFF(6).Result, _
oFF(7).Result, oFF(8).Result, _
oFF(9).Result, oFF(10).Result
Close #i
End If
 
R

Rob

The code you included here works fine for me. What isn't it doing? Are you
sure the form fields have data in them? Are you trying to append to the file
each time and you don't see the NEW data? Is there actually a file
U:\History.txt? Can it be written to?

You might also try stepping through the code from the Debug menu and
watching what it does.
 
L

LEU

Hi Rob,

The problem is it works if the textfile name already exists. But if I enter
a new file name and try to save it nothing happens. How do I get it to save
new file ames?

LEU
 
R

Rob

The problem with this method is you MUST pick a file from the dialog. To
create a new file you'd want to change your code to something like this.

Dim tempFile As String
Dim fd As FileDialog
Dim oFF As FormFields
Dim i As Integer

Set oFF = ActiveDocument.FormFields

i = FreeFile()

Set fd = Application.FileDialog(msoFileDialogSaveAs)
fd.FilterIndex = 8
fd.InitialFileName = "U:\"
fd.AllowMultiSelect = False

If fd.Show Then
tempFile = fd.SelectedItems(i)
Open tempFile For Output As #i
Write #i, oFF(5).Result, oFF(6).Result, _
oFF(7).Result, oFF(8).Result, _
oFF(9).Result, oFF(10).Result
Close #i
End If

Set oFF = Nothing
 
L

LEU

Hi Rob,

That worked. Thank you.

What is the difference between the following?
fd.FilterIndex = 0 and fd.FilterIndex = 8

When I changed Set fd = Application.FileDialog(3) to Set fd =
Application.FileDialog(msoFileDialogSaveAs) it would give me a error at
fd.FilterIndex = 0 When I looked in the VB HELP I could not find anything on
the '0' or what it meant. How did you know changing the 0 to an 8 would make
it work?

LEU
 
R

Rob

The FilterIndex refers to the list in the "Files of Type" dropdown. In this
case 8 is the number for "Text files (*.txt)". The different dialogues vary
in what's in the list so the number may not be the same (or work) for all of
them.
 

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