Word vba - 从INI文件中读取所有值并放入数组

时间:2011-03-17 06:03:31

标签: vba ms-word word-vba ini

好的,所以我有一堆Word模板,在Document_New上读取注册表中的某些值,并将它们插入到文档中的书签中。直到现在我这样做的方式是首先从INI文件中读取,该文件指示模板中的书签被调用的内容。这是逐行完成的,它再次被放入一个数组中。然后我通过该数组并逐个阅读注册表。

现在,是否可以读取整个INI部分并将其直接放入数组中? 像这样?:

Dim strIniKey as String
strIniKey = INI key

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

然后通过每个INI值循环这个?

我现在就是这样做的:

    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, regStrForetaksnr)

这基本上创建了一个充满INI文件值的数组。

代码继续获取数组的每个部分并将其循环通过书签,搜索匹配,然后从注册表中插入相应的值。

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

每个书签都被称为“书签”+来自INI的每一行的值。 比如“bookmarkFIRSTNAME”等。

1 个答案:

答案 0 :(得分:1)

您可以使用词典(来自MS Scripting库)。

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

现在您可以将您的值引用为(例如):     oDict( “路径”)

但是,你所做的就是将它们从ini文件移动到Dictionary:在这两种情况下,你只需按名称引用它们。不过,它的代码更少...... 我不确定首先将它们转移到阵列的目的?您的代码是否只是返回数组的函数的一部分?