FileDialog

Anonimo
2017-12-11T13:46:09+00:00

ciao mi serve un consiglio su come correggere un errore su Filedialog, in pratica io vorrei selezionare un file tramite finestra di ialogo e scrivere la sua path su un campo maschera.

il codice è il seguente:

Dim fd As FileDialog

Set fd = Application.FileDialog(msoFileDialogFolderPicker)

    With fd

            .InitialFileName = "C:"    

            .Title = "Sfoglia cartelle"

            .ButtonName = "Ok"

            .AllowMultiSelect = False

            .InitialView = msoFileDialogViewDetails

            .Show

            For Each objfl In .SelectedItems

                filename = objfl

            Next objfl

    End With

NomeDirectory = filename

[Forms]![formMail2]![Testo12] = filename

end sub

mi da il messaggio necessario oggetto...

aspetto un consiglio su come fare.

ho provato anche in questo modo, ma praticamente non parte proprio la funzione, clicco sul tasto ma non fa nulla.

ho fatto un modulo standard così:

Option Compare Database

Option Explicit

Public Const OFN_FILEMUSTEXIST = &H1000

Public Const OFN_HIDEREADONLY = &H4

Public Const OFN_PATHMUSTEXIST = &H800

Public Declare PtrSafe Function GetOpenFileName Lib "comdlg32.dll" _

Alias "GetOpenFileNameA" (lpofn As OPENFILENAME6) As Long

Public Type OPENFILENAME6

lStructSize As Long

hwndOwner As Long

hInstance As Long

lpstrFilter As String

lpstrCustomFilter As String

nMaxCustomFilter As Long

nFilterIndex As Long

lpstrFile As String

nMaxFile As Long

lpstrFileTitle As String

nMaxFileTitle As Long

lpstrInitialDir As String

lpstrTitle As String

flags As Long

nFileOffset As Integer

nFileExtension As Integer

lpstrDefExt As String

lCustData As Long

lpfnHook As Long

lpTemplateName As String

End Type

Public Sub ShowFile3(frm As Form, fb As OPENFILENAME6)

Dim fName As String

Dim result As Long

Dim Path As String

Path = "C:"

With fb

.lStructSize = Len(fb) 

.hwndOwner = frm.Hwnd 

.hInstance = 0 

.lpstrFilter = "Tutti i file" & vbNullChar & "*.*" & vbNullChar

.nMaxCustomFilter = 0

.nFilterIndex = 1 'Default filter is the first one

.lpstrFile = Space(256) & vbNullChar d 

.nMaxFile = Len(.lpstrFile)

.lpstrFileTitle = Space(256) & vbNullChar 

.nMaxFileTitle = Len(.lpstrFileTitle)

.lpstrInitialDir = Path & vbNullChar 

.lpstrTitle = "Apri file" & vbNullChar 

.flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY

.nFileOffset = 0 

.nFileExtension = 0

.lCustData = 0

.lpfnHook = 0

End With

result = GetOpenFileName(fb)

If result <> 0 Then

fName = Left(fb.lpstrFile, InStr(fb.lpstrFile, vbNullChar) - 1)

End If

End Sub

 poi ho preparato un pulsante con questo codice:

Dim fName As String

Dim fb As OPENFILENAME6

Call ShowFile3(Me, fb)

fName = Left(fb.lpstrFile, InStr(fb.lpstrFile, vbNullChar) - 1) 'percorso completo

[Forms]![formMail2]![Testo12] = fName

Microsoft 365 e Office | Access | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

Risposta accettata dall'autore della domanda

Anonimo
2017-12-13T11:38:29+00:00

ciao Tanoz,

chiedendo il path del file ho interpretato alla lettera il path del file*,* se vuoi il path+nome file, modifica in questo modo :

Public Function GetPathDialog() As String

Dim fDialog              As Object

Dim strFileFullPath      As String

Set fDialog = Application.FileDialog(3)

With fDialog

.Title = "Seleziona uno i più files "

.AllowMultiSelect = True

.Filters.Clear

.Filters.Add "mostra tutti i files", "*.*"

.InitialFileName = "c:"

strFileFullPath = vbNullString

If .Show Then

strFileFullPath = .SelectedItems(1)

  End If

End With

GetPathDialog = strFileFullPath

Set fDialog = Nothing

End Function

lasciando invariato quanto detto per il codice sul command button.

Ciao, Sandro.

La risposta è stata utile?

1 persona ha trovato utile questa risposta.
0 commenti Nessun commento

3 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2017-12-13T12:40:36+00:00

    Ciao Sandro grazie ancora, funziona da dio :-) :-)

    Tanoz

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2017-12-13T11:04:18+00:00

    Ciao Sandro grazie per il codice, :-)...il problema è che nella casella txtpath mi mostra il percorso ma non il file, mi compila la cesella così: "C:\Users\Tano\Desktop" ma manca il file selezionato.

    Tanoz

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2017-12-12T20:21:15+00:00

    ciao Tanoz,

    il tuo post non è stato mostrato nell'elenco dei post non capisco perché, ma indagheremo!

    Lascia perdere la seconda soluzione che si basa su APi, copia incolla il codice che segue in un modulo standard e per mostrare il path del file scelto in una textBox chiamata txtPath con un command button chiamato cmdChooseFile invoca :

    Private Sub cmdChooseFile_Click()

    Me.txtPath = GetPathDialog()

    End Sub

    in un modulo standard invece :

    Option Compare Database

    Option Explicit

    Public Function GetPathDialog() As String

         Dim fDialog              As Object

         Dim strFileFullPath      As String

         Set fDialog = Application.FileDialog(3)

         With fDialog

           .Title = "Seleziona uno i più files "

           .AllowMultiSelect = True

            .Filters.Clear

            .Filters.Add "mostra tutti i files", "*.*"

            .InitialFileName = "c:"

             If .Show Then

                strFileFullPath = .SelectedItems(1)

             End If

         End With

         If Len(strFileFullPath) > 0 Then

            GetPathDialog = GetFilePath(strPath:=strFileFullPath)

         End If

         Set fDialog = Nothing

      End Function

    Private Function GetFilePath(strPath As String) As String

        Dim strIn   As String

        strIn = Mid$(strPath, 1, InStrRev(strPath, ""))

        GetFilePath = strIn

    End Function

    ciao, Sandro.

    La risposta è stata utile?

    0 commenti Nessun commento