› Sviluppare funzionalita su Microsoft Office con VBA › Inserire dati in listbox da file.ini
-
AutoreArticoli
-
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.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 LongDa 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 Functione 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 SubIl 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 🙂
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 SubHo 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 FunctionQual è 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 )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 ) -
AutoreArticoli
