Common Dialog Question

ahgao2
03-12-2003, 10:08 AM
I am having problem with this codings and I am not sure if I am doing it right.

Dim fileName As String
dlgOpen.ShowOpen
While dlgOpen.CancelError = True
fileName = dlgOpen.fileName
frmLetter.Show
Call frmLetter.setFileName(fileName)
Wend

What my intention was to allow the user to open a file using the common dialog box (dlgOpen) but if the user presses the cancel button, the program will not continue on to open anything. The good thing is the Cancel button works but then if I wish to open a file, the Open button does not open it! I am really stuck at this.

Machaira
03-12-2003, 10:15 AM
You actually have to write the code to retrieve the data in the file yourself. The CommonDialog control just retrieves the filename of the file for you. Something like:

Private Sub Form_Load()

Dim sText As String
Dim iFileNum As Integer

iFileNum = FreeFile

Open App.Path & "\test.txt" For Input As iFileNum

sText = Input(LOF(iFileNum), iFileNum)

Debug.Print sText

Close iFileNum

End Sub

ahgao2
03-12-2003, 10:28 AM
Ok I know that the CommonDialog control only allows you to obtain the filename so I have created a method to set the filename and open the specific file. But my main concern is that no matter which button I press in the CommonDialog.ShowOpen window, nothing happens. What I wanted was if users press open, the file opens. And when the user press cancel, don't do anything.

Sorry Machairae.

Diurnal
03-12-2003, 11:32 AM
Maybe this can help. I was not sure if you wanted to do some file operations or just view the file. Assuming that the file in question is a text file...


Private Sub OpenFile()
'Create variables for the filename, filenumber to open/close and the Shell function.
Dim FileName As String, retval As Long, FileNum As Integer

With CommonDialog1
.CancelError = False
.DialogTitle = "Open"
.Filter = "Text (*.txt)|*.txt"
.ShowOpen
End With

'Return the selected file to the FileName variable.
FileName = CommonDialog1.FileName

'To open and close the file for reading and writing...
'Set the filenumber to the first open freefile.
FileNum = FreeFile
Open FileName For Input As FileNum
'Do File operations here.
Close FileNum

'To open the file and read it into Notepad use the Shell Function. Extend the FileName _
'to include the path for notepad. The FileName is passed as a parameter to Notepad.
FileName = "d:\windows\notepad.exe " & FileName
retval = Shell(FileName, vbMaximizedFocus)
End Sub


There are various file handling operations you can use. Search MSDN for more information. Also, the shell function can be used to open a program on the user's computer to view the file. I hope this helps.

ahgao2
03-12-2003, 08:19 PM
What does the .CancelError do?

BostjanK
03-13-2003, 01:10 AM
CancelError returns or sets a value indicating whether an error is generated when the user chooses the Cancel button. It must be set to true.

This is how I use Common Dialog:

Dim fileName As String

On Error Resume Next
dlgOpen.ShowOpen
If Err <> cdlCancel Then 'user didn't press Cancel
'open file
fileName = dlgOpen.fileName
...
Else
'do whatever you want
End If

Bostjan

ahgao2
03-13-2003, 09:15 AM
Ok there are some parts of the codings that I don't really understand, and tried to modify it but....

Dim fileName As String

On Error Resume Next
dlgOpen.ShowOpen
If Err <> cdlCancel Then 'user didn't press Cancel
'open file
fileName = dlgOpen.fileName
...
Else
'do whatever you want
End If


What is Err and cdlCancel? Sorry that I got to ask these kinds of newbie questions.

Gamer X
03-13-2003, 10:14 AM
What is Err and cdlCancel? Sorry that I got to ask these kinds of newbie questions.
Err is an Long holding the numerical ID of the error that just occured (actually, it's Err.Number, but .Number is the default anyway). cdlCancel appears to be a constant, holding the value 32755 -- the error number of the error that is returned when a user presses cancel on a CommonDialog.

Arigato,
Gamer X

ahgao2
03-13-2003, 10:22 AM
Ok understand. Thanks!

ahgao2
03-13-2003, 10:34 AM
Ok I have tried the codings

Dim fileName As String

On Error Resume Next
dlgOpen.ShowOpen
If Err <> cdlCancel Then 'user didn't press Cancel
'open file
fileName = dlgOpen.fileName
...
Else
'do whatever you want
End If


But the system still trys to open a file even when I clicked the 'Cancel' button. Did I miss out anything?

Gamer X
03-13-2003, 10:39 AM
Yes. Put dlgOpen.CancelError = True before dlgOpen.ShowOpen, or set dlgOpen's CancelError property to True in the properties window.

Arigato,
Gamer X

ahgao2
03-13-2003, 10:56 AM
It works! It works! Oh thanks alot Gamer X! :D

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum