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

发布于 2024-10-24 01:43:05 字数 3640 浏览 4 评论 0原文

好的,我有一堆 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”等。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-10-31 01:43:05

您可以只使用字典(来自 MS 脚本库)。

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("Path")

不过,您所做的只是将它们从 ini 文件移动到字典:在这两种情况下,您只需按名称引用值。不过,代码还是少了......
我不确定首先将它们转移到数组的目的?您的代码是仅返回数组的函数的一部分吗?

蒂姆

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文