开发者

Word vba - Read all values from INI file and put in array

开发者 https://www.devze.com 2023-02-17 03:46 出处:网络
Ok, so I\'ve got a bunch of Word templates that on Document_New reads the registry for some values and inserts these into bookmarks in the document. The way I\'ve done this until now is to first read

Ok, so I've got a bunch of Word templates that on Document_New reads the registry for some values and inserts these into bookmarks in the document. The way I've done this until now is to first read from an INI-file which dictates what the bookmarks in the templates are called. This is done line by line, which again is put into an array. Then I go through that array and read the registry one by one.

Now, is it possible to read the entire INI section and put it into an array directly? Like this?:

Dim strIniKey as String
strIniKey = INI key

If bookmark exists strIniKey Then
   pick up registry string from strIniKey
   Insert into bookmark
End if

And just loop this through each INI value?

This is how I do it now:

    Dim objShell
Dim strShell
Dim strDataArea
Dim Verdier() As String
Dim regPath
Dim regString
Dim Felter
Dim WScript

' Klargjør stringverdier fra INI-fil

regPath = ReadIni(File, "Registry", "Path")
regString = ReadIni(File, "Registry", "String")
regStrFornavn = ReadIni(File, strRegArea, "Fornavn")
regStrEtternavn = ReadIni(File, strRegArea, "Etternavn")
regStrInitialer = ReadIni(File, strRegArea, "Initialer")
regStrBrevnavn = ReadIni(File, strRegArea, "Brevnavn")
regStrStilling = ReadIni(File, strRegArea, "Stilling")
regStrStilling_EN = ReadIni(File, strRegArea, "Stilling_EN")
regStrAvdeling = ReadIni(File, strRegArea, "Avdeling")
regStrAvdeling_EN = ReadIni(File, strRegArea, "Avdeling_EN")
regStrEnhetoffnavn = ReadIni(File, strRegArea, "Enhetoffnavn")
regStrEnhetoffnavn_EN = ReadIni(File, strRegArea, "Enhetoffnavn_EN")
regStrVisitaddress1 = ReadIni(File, strRegArea, "Visitaddress1")
regStrVisitpostnrsted = ReadIni(File, strRegArea, "Visitpostnrsted")
regStrPostadresse1 = ReadIni(File, strRegArea, "Postadresse1")
regStrPostadresse2 = ReadIni(File, strRegArea, "Postadresse2")
regStrPostnrsted = ReadIni(File, strRegArea, "Postnrsted")
regStrLeveringsadresseNr = ReadIni(File, strRegArea, "LeveringsadresseNr")
regStrLeveringsadresseSted = ReadIni(File, strRegArea, "LeveringsadresseSted")
regStrTelesentralbord = ReadIni(File, strRegArea, "Telesentrbord")
regStrDirekteinnvalg = ReadIni(File, strRegArea, "Direkteinnvalg")
regStrMobil = ReadIni(File, strRegArea, "Mobil")
regStrTelefax = ReadIni(File, strRegArea, "Telefax")
regStrWebadresse = ReadIni(File, strRegArea, "Webadresse")
regStrSentrepostmottak = ReadIni(File, strRegArea, "Sentrepostmottak")
regStrEpost = ReadIni(File, strRegArea, "Epost")
regStrForetaksnr = ReadIni(File, strRegArea, "Foretaksnr")

'Klargjør array av alle verdier fra INI-fil
Felter = Array(regStrFornavn, regStrEtternavn, regStrBrevnavn, regStrInitialer, _
regStrStilling, regStrStilling_EN, regStrAvdeling, regStrAvdeling_EN, regStrEnhetoffnavn, _
regStrEnhetoffnavn_EN, regStrVisitaddress1, regStrVisitpostnrsted, regStrPostadresse1, _
regStrPostadresse2, regStrPostnrsted, regStrLeveringsadresseNr, regStrLeveringsadresseSted, _
regStrTelesentralbord, regStrDirekteinnvalg, regStrMobil, regStrTelefax, regStrWebadresse, _
regStrSentrepostmottak, regStrEpost, regStr开发者_高级运维Foretaksnr)

This basically creates an array full of the values from the INI file.

The code continues to take each part of the array and loop it through the bookmarks, searching to a match and then inserts the corresponding value from the registry.

Set objShell = CreateObject("Wscript.Shell")

    For iTeller = 0 To UBound(Felter)
    Dim sBookMarkName, sVerdi
    Dim myRange As Range

    On Error Resume Next
    sBookMarkNametemp = "Bookmark" & Felter(iTeller)

    sVerdi = ""
    sVerdi = objShell.RegRead(regPath & "\" & Felter(iTeller))
    sBookMarkName = ""
    sBookMarkName = ActiveDocument.Bookmarks(sBookMarkNametemp).Name

    With ActiveDocument
        If .Bookmarks.Exists(sBookMarkNametemp) Then
        Set myRange = .Bookmarks(sBookMarkName).Range
        myRange.Text = sVerdi
        .Bookmarks.Add sBookMarkName, myRange
        End If
        End With

    On Error GoTo 0

    objShell = Nothing
WScript = Nothing

Each bookmark is called "bookmark" + value of each line from the INI. Like "bookmarkFIRSTNAME" etc.


You could just use a Dictionary (from the MS Scripting library).

Dim oDict as new scripting.dictionary
Dim arrVals, x

arrVals = Array("Path","String","Fornavn") 'etc etc
for x=lbound(arrVals) to ubound(arrVals)
   oDict.Add arrVals(x), ReadIni(File, strRegArea, arrVals(x))
next x

Now you can reference your values as (eg.) : oDict("Path")

Still, all you've done there is move them from an ini file to a Dictionary: in both cases you just reference the values by name. Still, it's less code... I'm not sure of the purpose of first transferring them to an array ? Is your code part of a function which just returns the array ?

Tim

0

精彩评论

暂无评论...
验证码 换一张
取 消