Sviluppare funzionalita su Microsoft Office con VBA Inserire dati in listbox da file.ini

Login Registrati
Stai vedendo 5 articoli - dal 1 a 5 (di 5 totali)
  • Autore
    Articoli
  • #7779 Score: 0 | Risposta

    albatros54
    Moderatore
      89 pts

      Da un thread precedene VF , mi ha messo una pulce nell'orecchio per quanto riguarda i file .INI.

      la mia domanda, a scopo didattico è " Non conoscendo il numero delle chiavi di una sezione di un file .ini, come faccia a ciclare(conoscere il numero delle chiavi della sezione)la sezione per inserire  i valori delle chiavi nella listbox?

      In allegato file di excel e file .ini

      PS. il file funziona bene, perche vado a leggere il valore "count "della sezione "password"

       

      Qual è il punto di avere gusti diversi, se non mostrare che i cervelli lavorano diversamente, che pensiamo diversamente? ( Alan Turing)
      Sempre il mare, uomo libero, amerai!
      ( Charles Baudelaire )
      Allegati:
      You must be logged in to view attached files.
      #7782 Score: 0 | Risposta

      vecchio frac
      Senior Moderator
        272 pts

        Esiste anche getPrivateProfileSection:

        Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
        "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
        ByVal nSize As Long, ByVal lpFileName As String) As Long

        Da usare in combinato disposto con le altre.

        Nel tuo modulo aggiungerei questo codice, scopiazzato da qualche parte:

        'reads an Ini section
        Public Function ReadIniSection(sINIFile As String, sSection As String) As String
        Dim RetVal As String * 255, v As Long
            v = GetPrivateProfileSection(sSection, RetVal, 255, sINIFile)
            ReadIniSection = Left(RetVal, v + 0)
        End Function

        e sostituisce il codice dell'userform con questo tanto per vedere se può funzionare come ti serve:

        Option Explicit
        
        Const sINIFile As String = "K:\EXCELVBA\ExcevbaFile\FileINI\pw.ini"
        
        Private Sub UserForm_Activate()
        Dim Hregioni As String
            Dim sUserName As String
            Dim nCount As Integer
            Dim i As Integer
            
           ListBox1.Clear
          
            nCount = CInt(sGetINI(sINIFile, "username", "Count", 0))
            For i = 1 To nCount
                ListBox1.AddItem sGetINI(sINIFile, "username", "dottori" & i, "?")
            Next i
            ListBox1.Text = sGetINI(sINIFile, "username", _
                                   "", ListBox1.List(0))
            Hregioni = sGetINI(sINIFile, "Regions", "sRegioni", "?")
         
            Call ListIniSectionLines
        End Sub
        
        
        Private Sub ListIniSectionLines()
            Dim S As String:
            
            Dim vLines As Variant:
            Dim vLine As Variant
            
            S = ReadIniSection(sINIFile, "username")
            vLines = Split(S, Chr$(0))
            
            For Each vLine In vLines
               Debug.Print vLine
            Next vLine
        End Sub

        Il gioco è ovviamente nella piccola sub ListIniSectionLines che invece di mandare a Debug la lettura delle chiavi di quella sezione potrebbe diventare una Functin che restituisce un array delle chiavi stesse.

        Se hai bisogno di aiuto fai un fischio che ti aggiusto il codice in via definitiva 🙂

         

         

        #7785 Score: 0 | Risposta

        vecchio frac
        Senior Moderator
          272 pts

          Mi porto avanti 🙂

          Fermo restando che devi lasciare la dichiarazione dell'API nel modulo e la function di appoggio "ReadIniSection", puoi semplificare il codice dell'userform.Activate come di seguito:

          Option Explicit
          
          Private Sub UserForm_Activate()
          Const sINIFile As String = "K:\EXCELVBA\ExcevbaFile\FileINI\pw.ini"
          Dim Hregioni As String
          Dim sUserName As String
          Dim i As Integer
          Dim sSections As Variant
          Dim sSection As Variant
              
              ListBox1.Clear
            
              sSections = Split(ReadIniSection(sINIFile, "username"), Chr(0))
              For Each sSection In sSections
                  If sSection Like "*=*" Then ListBox1.AddItem Split(Trim(sSection), "=")(0)
              Next
              
              ListBox1.Text = sGetINI(sINIFile, "username", "", ListBox1.List(0))
              Hregioni = sGetINI(sINIFile, "Regions", "sRegioni", "?")
          End Sub
          #7786 Score: 0 | Risposta

          albatros54
          Moderatore
            89 pts

            Ho fatto diventare la sub ListIniSectionLines  una function, non sara il massimo, pero funziona è risponde  alla domanda che ho postato  

            Option Explicit
             Public n As Integer
            Const sINIFile As String = "K:\alba\EXCELVBA\ExcevbaFile\FileINI\pw.ini"
            
            
            
            Private Sub UserForm_Activate()
            Const sINIFile As String = "K:\EXCELVBA\ExcevbaFile\FileINI\pw.ini"
            
            Dim Hregioni As String
                Dim sUserName As String
                Dim nCount As Integer
                Dim i As Integer
                Dim n As Integer
               ListBox1.Clear
                
                
                nCount = ListIniSectionLines(n)
                For i = 1 To nCount
            
                    ListBox1.AddItem sGetINI(sINIFile, "username", "dottori" & i, "?")
                Next i
                ListBox1.Text = sGetINI(sINIFile, "username", _
                                       "", ListBox1.List(0))
            
             
            End Sub
            
            Function ListIniSectionLines(Optional ByVal l As String) As Integer
                Dim s As String:
                Dim n As Integer
                Dim vLines As Variant:
                Dim vLine As Variant
                
                s = ReadIniSection(sINIFile, "username")
                vLines = Split(s, Chr$(0))
                n = 0
                For Each vLine In vLines
                   Debug.Print vLine
                   n = n + 1
                Next vLine
                ListIniSectionLines = n
            End Function
            Public Function ReadIniSection(sINIFile As String, sSection As String) As String
            Dim RetVal As String * 255, v As Long
                v = GetPrivateProfileSection(sSection, RetVal, 255, sINIFile)
                ReadIniSection = Left(RetVal, v + 0)
            End Function
            

             

            Qual è il punto di avere gusti diversi, se non mostrare che i cervelli lavorano diversamente, che pensiamo diversamente? ( Alan Turing)
            Sempre il mare, uomo libero, amerai!
            ( Charles Baudelaire )
            #7787 Score: 0 | Risposta

            albatros54
            Moderatore
              89 pts

              Ho visto il tuo codice precedente è piu (Cir)conciso  

               

              Qual è il punto di avere gusti diversi, se non mostrare che i cervelli lavorano diversamente, che pensiamo diversamente? ( Alan Turing)
              Sempre il mare, uomo libero, amerai!
              ( Charles Baudelaire )
            Login Registrati
            Stai vedendo 5 articoli - dal 1 a 5 (di 5 totali)
            Rispondi a: Inserire dati in listbox da file.ini
            Gli allegati sono permessi solo ad utenti REGISTRATI
            Le tue informazioni: