02/09/06 12:15
natamas
Non puoi utilizzare il comando "showopen" perchè questo comando seleziona un file che già esiste e non uno che dovrai salvere.
Ti consiglio di usare la funzione "showsave".
Comunque potresti utilizzare questo codice:
sub salva()
With CommonDialog1
.DialogTitle = "Salva"
.CancelError = False
.Filter = "File di tipo testo(*.txt)|*.txt"
.ShowSave
If Len(.FileName) = 0 Then
msgbox "Non hai dato un nome al file", vbExclamation
exit sub
else
call scrivi(.FileName)
end if
End With
end sub
sub scrivi(file as string)
Open file For Input As #1
strTesto = Input(LOF(1), 1)
Close 1
end sub
Vedi che questa funzione l'ho pensata e scritta, senza provarla.
Perciò se ci sono dei bug contattami che li possiamo risolvere insieme.
In bacca al lupo
Ultima modifica effettuata da natamas 02/09/06 12:17
aaa
03/09/06 15:45
psycho85
Allora, diciamo che ci ho ragionato su un'attimo e con il tuo code possiamo sia aprire sia salvare file senza nessun problema. Voglio solamente dirti che sono un principiante e tutto quello che so l'ho imparato qua e la quindi non ti spaventare se faccio qualche GAF di scrittura del code...
Ho messo sul form il commondialog, una textbox e due command button (APRI E SALVA) ed ecco il code:
-------------------->Inizio Code<-----------------
Dim strTesto As String 'Variabile che contiene il testo
Sub Apri() 'Procedura per l'apertura dei file
With CommonDialog1
.DialogTitle = "Apri"
.CancelError = False
.Filter = "File di tipo testo(*.txt)|*.txt"
.ShowOpen
If Len(.FileName) = 0 Then
MsgBox "Non hai dato un nome al file", vbExclamation
Exit Sub
Else
Call ProceduraApri(.FileName)
End If
End With
End Sub
Sub Salva()
With CommonDialog1
.DialogTitle = "Salva"
.CancelError = False
.Filter = "File di tipo testo(*.txt)|*.txt"
.ShowSave
If Len(.FileName) = 0 Then
MsgBox "Non hai dato un nome al file", vbExclamation
Exit Sub
Else
Call ProceduraSalva(.FileName)
End If
End With
End Sub
Sub ProceduraApri(file As String)
Open CommonDialog1.FileName For Input As #1
strTesto = Input(LOF(1), 1)
Close 1
txtTesto.Text = strTesto
End Sub
Sub ProceduraSalva(file As String)
Open CommonDialog1.FileName For Output As #1
Print #1, txtTesto.Text
Close 1
txtTesto.Text = strTesto
End Sub
Private Sub Command1_Click()
Apri
End Sub
Private Sub Command2_Click()
Salva
End Sub
-------------------->Fine Code<-------------------
Questo codice FUNZIONA l'ho testato sia in apertura che in salvataggio, ovviamente necessita di qualche ritocchino per migliorarlo.
Cosa più importante che IL FAMOSO BUG l'abbiamo eliminato, adesso se si preme il pulsante "INDIETRO" nelle finestre APRI O SALVA non viene visualizzato più l'errore....
aaa
04/09/06 11:26
natamas
Sub ProceduraApri(file As String)
Open CommonDialog1.FileName For Input As #1
strTesto = Input(LOF(1), 1)
Close 1
txtTesto.Text = strTesto
End Sub
Sub ProceduraSalva(file As String)
Open CommonDialog1.FileName For Output As #1
Print #1, txtTesto.Text
Close 1
txtTesto.Text = strTesto
End Sub
se tu richiami la funzione "procedurasalva" e "proceduraapri" e ad entrambi mandi l'informazione che vogliono, cioè "file as string", non c'è bisogno di mettere nella funzione open nuovamente il commondialog1.filename, ma basta inserire "file", cioè l'informazione che tu gli hai mandato quando hai chiamato la funzione.
Eempio:
Sub ProceduraApri(file As String)
Open file For Input As #1
strTesto = Input(LOF(1), 1)
Close 1
txtTesto.Text = strTesto
End Sub
Sub ProceduraSalva(file As String)
Open file For Output As #1
Print #1, txtTesto.Text
Close 1
txtTesto.Text = strTesto
End Sub
Altrimanti se vuoi fare come dici tu ti consiglio di modificare così il codice:
Dim strTesto As String 'Variabile che contiene il testo
Sub Apri() 'Procedura per l'apertura dei file
With CommonDialog1
.DialogTitle = "Apri"
.CancelError = False
.Filter = "File di tipo testo(*.txt)|*.txt"
.ShowOpen
If Len(.FileName) = 0 Then
MsgBox "Non hai dato un nome al file", vbExclamation
Exit Sub
Else
Call ProceduraApri
End If
End With
End Sub
Sub Salva()
With CommonDialog1
.DialogTitle = "Salva"
.CancelError = False
.Filter = "File di tipo testo(*.txt)|*.txt"
.ShowSave
If Len(.FileName) = 0 Then
MsgBox "Non hai dato un nome al file", vbExclamation
Exit Sub
Else
Call ProceduraSalva
End If
End With
End Sub
Sub ProceduraApri()
Open CommonDialog1.FileName For Input As #1
strTesto = Input(LOF(1), 1)
Close 1
txtTesto.Text = strTesto
End Sub
Sub ProceduraSalva()
Open CommonDialog1.FileName For Output As #1
Print #1, txtTesto.Text
Close 1
txtTesto.Text = strTesto
End Sub
Private Sub Command1_Click()
Apri
End Sub
Private Sub Command2_Click()
Salva
End Sub
Fammi sapere,ciao
Ultima modifica effettuata da natamas 04/09/06 11:28
aaa
08/09/06 16:03
psycho85
Ho creato un modulo e all'interno ho inserito il seguente code:
--------------Inizio Codice Modulo---------------
Sub ProceduraApri(file As String)
Open file For Input As #1
strTesto = Input(LOF(1), 1)
Close 1
frmAppuntiXP.txtTesto.Text = strTesto
End Sub
Sub ProceduraSalva(file As String)
Open file For Output As #1
Print #1, frmAppuntiXP.txtTesto.Text
Close 1
End Sub
Sub Apri()
With frmAppuntiXP.CommonDialog1
.DialogTitle = "Apri"
.CancelError = False
.Filter = "File di testo|*.txt|Pagina Web|*.htm|Tutti i file|*"
.ShowOpen
If Len(.FileName) = 0 Then
'Eventuale msgbox che avverte che il file non è stato salvato
blnModifiche = True
Else
Call ProceduraApri(.FileName)
End If
End With
End Sub
Sub Salva()
With frmAppuntiXP.CommonDialog1
.DialogTitle = "Salva"
.CancelError = False
.Filter = "File di testo|*.txt|Pagina Web|*.htm|Tutti i file|*"
.ShowSave
If Len(.FileName) = 0 Then
'Eventuale msgbox che avverte che il file non è stato salvato
blnModifiche = True
Exit Sub
Else
Call ProceduraSalva(.FileName)
End If
End With
End Sub
----------------Fine Codice Modulo--------------
Poi nel form principale del programma ho inserito
------------Inizio Code per aprire File-----------
Private Sub mnuApri_Click()
Apri
End Sub
-------------Fine Code per aprire File-----------
------------Inizio Code per salvare File------
Private Sub mnuSalva_Click()
Salva
End Sub
-----------Inizio Code per salvare File---------
In questo modo, ho accorciato enormemente il codice del programma e risolto il bug del comando annulla....GRAZIE MILLE NATAMAS
aaa