Hi
I have a strange problem and am wondering if anyone else has come across something similar and has a fix or workaround.
I have a template that is quite complicated and belongs to my client so I can't share it here but I can detail where it goes wrong as I have had them run a version that created a debug file.
The crux of the problem is that the Selection Object suddenly returns -1 as the result of Selection.Information(wdActiveEndSectionNumber)
There is nothing in my code that moves/changes the Selection object.
It doesn't seem to happen every time and it doesn't happen at all on my machine (2013), or my laptop (2016) or a test virtual machine (2016) but it does happen regularly on client machines (all are 2016, they have recently upgraded which may be causing the problem)
Here is the code that relates to the issue ...
gAddToDebug "Before for"
For Each bm In rgChp.Bookmarks
gAddToDebug "After for " & bm.Name
The "for" is the line that causes the problem, seemingly. The two Debug lines create this ...
Before for | Start = 9123 | End = 9123 | Section = 5
After for xchp0001 | Start = 9123 | End = 9123 | Section = -1
The code that grabs the info is this ...
lStart = Selection.Start
lEnd = Selection.End
lSect = Selection.Information(wdActiveEndSectionNumber)
So before the for statement the Selection Object (cursor) start position is 9123, it ends in same place (so it is an insertion point) and it is in section 5 - that is right, Chapter 1 of the report is section 5
After the for statement the Start position is the same but the section has gone to -1. -1 is "undefined" according to MS
rgChp is a function ...
Private Function rgChp() As Range
Set rgChp = mudtData.doc.Range(mudtData.lStart, mudtData.lStart)
End Function
This is all inside a class. mudtData.doc is the ActiveDocument and mudtData.lStart is the character position of the start of the current chapter.
It is a bit complex but it all works fine and has done for ages.
There are no Events defined that may be firing up asynchronously.
Can anyone shed some light as to why getting a range could cause the cursor to lose track of what section it is in BUT still have same Start position?
Or some trick to force Word to re-assess where the cursor is.
Am totally stumped at the moment!
Thanks
Simon