我正在研究一系列文档的解析器。作为解析的一部分,我将书签放在整个单独的文档中,然后一次循环一次,将我实际想要的数据从文档中拉出来,文档中有几个实例。我在文档中感兴趣的信息有几个部分,我按顺序访问,然后扔进一个包含带有数据映射的Dictionary的数组。出于某种原因,当我更新用于在字典后面提供密钥的变量时,字典会更新 - 在它之前需要几行。当我尝试稍后添加密钥时,这会导致抛出错误。如果有人能解释幻影更新,我将不胜感激。
更新:我刚注意到它也发生在
之后Set Topics(TopicsCount).TopicMap = New Scripting.Dictionary
当CurrentString值为" Title"第一次,在我甚至打电话给Add
之前Sub ParseDoc()
Dim objDoc As Document
Set objDoc = ActiveDocument
Dim Topics() As TopicData
Dim TopicsCount As Integer
TopicsCount = -1
Dim DataRange As Range
For i = 1 To objDoc.Bookmarks.count - 1
indexOfBase = InStr(1, objDoc.Range.Bookmarks(i), "Base")
CurrentString = Left(objDoc.Range.Bookmarks(i), indexOfBase - 1) ' after this line the Topices(TopicCount).TopicMap updates
If CurrentString = "Title" Then
TopicsCount = TopicsCount + 1
ReDim Preserve Topics(TopicsCount)
Set Topics(TopicsCount) = New TopicData
Topics(TopicsCount).TopicID = GrabTitle
Set Topics(TopicsCount).TopicMap = New Scripting.Dictionary 'the Map will update here the first time with CurrentString, note that the add line is further down and the Map shouldn't know about CurrentString yet.'
End If
Set DataRange = ActiveDocument.Range(Start:=objDoc.Range.Bookmarks(i).End, End:=objDoc.Range.Bookmarks(i + 1).Start)
If CurrentString = "TechArea" Or CurrentString = "Keywords" Then
DataArray = Split(DataRange.Text, ",")
For j = 0 To UBound(DataArray)
DataArray(j) = Trim(DataArray(j))
Next j
Topics(TopicsCount).TopicMap.Add CurrentString, DataArray ' throws error because of earlier update
Else
Topics(TopicsCount).TopicMap.Add CurrentString, DataRange.Text ' thorws error because of earlier update
End If
Next i
End Sub
答案 0 :(得分:0)
无论出于何种原因,这已经不再是一个问题。感谢帮助人们,但就像这个问题开始一样神秘,它停了下来。
更新 - 问题实际上似乎与调试有关。它只会在调试器在字典行之前遇到断点时发生。显然VBA是以某种方式自动填充它(至少有一个0索引项)当我打到断点时。