Word vba-从INI文件读取所有值并放入数组
好的,我有一堆Word模板,它们在Document_New上读取注册表中的一些值,并将这些值插入文档中的书签中。到目前为止,我一直采用的方法是首先读取INI文件,该文件指定了模板中的书签的名称。这是一行一行地完成的,再次放入一个数组中。然后我遍历该数组并逐个读取注册表 现在,是否可以读取整个INI部分并将其直接放入数组中? 像这样Word vba-从INI文件读取所有值并放入数组,vba,ms-word,ini,Vba,Ms Word,Ini,好的,我有一堆Word模板,它们在Document_New上读取注册表中的一些值,并将这些值插入文档中的书签中。到目前为止,我一直采用的方法是首先读取INI文件,该文件指定了模板中的书签的名称。这是一行一行地完成的,再次放入一个数组中。然后我遍历该数组并逐个读取注册表 现在,是否可以读取整个INI部分并将其直接放入数组中? 像这样 Dim strIniKey as String strIniKey = INI key If bookmark exists strIniKey Then p
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”等。您可以使用字典(来自MS脚本库) 现在您可以将您的值引用为(例如): oDict(“路径”) 不过,您所做的只是将它们从ini文件移动到字典:在这两种情况下,您只需按名称引用值。不过,它的代码更少。。。 我不确定首先将它们传输到阵列的目的是什么?您的代码是只返回数组的函数的一部分吗
蒂姆不,一开始可能写得不好。基本上,我所做的是将数据从注册表加载到模板中预定义的书签中。ini文件告诉代码注册表中每个键的名称。然后,它将从注册表中收集的数据加载到一个数组中,并将每个数据输入到与相同名称匹配的相应书签中。如果您有任何建议,请随时提供:)如果您不需要存储ini/注册表查找结果,只需要将其插入书签中,只需采用循环方法,和readfrom ini>read from registry>insert to bookmark在循环的每个迭代中。我真的不需要存储它们否。我只需要从ini文件中查找值,浏览书签,看看书签是否称为“bookmark+ini value”,然后使用与INI项相同的值,从注册表项中提取值,然后将其插入书签。是否可以读取INI文件的部分并创建数组而不必指定项?我希望代码转到ini文件中的指定部分,然后读取所有字符串并将它们放入数组中。然后只需遍历该数组,查找书签和注册表。但要做到这一点,我需要它来读取整个部分并将其放入数组中。无需我在ini部分中指定每个键。我相信你明白我的意思,除非我不善于解释:p
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