PDA

View Full Version : [SOLVED:] Current section for cursor/selection object returns -1



simon.a10
08-23-2018, 04:31 AM
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

simon.a10
08-23-2018, 09:11 AM
Update to the above ...

I added two more debug lines above/below the Set rgChp = line, expecting that call to be the one that lost the section but my client's file shows this ...

Before for | Start = 9123 | End = 9123 | Section = 5
Before rgChp | Start = 9123 | End = 9123 | Section = 5
After rgChp | Start = 9123 | End = 9123 | Section = 5
After for xchp0001 | Start = 9123 | End = 9123 | Section = -1

The Section is lost after the for statement concludes. So the requests to get bookmarks for the range has thrown it? Maybe

Can't understand how "For Each bm In rgChp.Bookmarks" would cause the Selection Object to lose the Section index.


Thanks

macropod
08-23-2018, 03:20 PM
Perhaps you could show us all the relevant code in one block instead of snippets that lack context?

When posting your code, please be sure use the code tags, indicated by the # button on the posting menu. Without them, posted code loses much of whatever structure it had.

simon.a10
08-24-2018, 12:43 AM
Hi

Thanks for the comment, I can't show all the code in one block as it is in several procedures and I can't share entire procedures in my client's project. Plus even if I did, it don't think it would help as my debug tests have pinned down exactly where the Selection Object changes.

The line that does it is below and when it happens, it happens on that line every time ...


For Each bm In rgChp.Bookmarks

Before that line runs ...

Selection.Information(wdActiveEndSectionNumber)
... returns a 5

After that line runs ...

Selection.Information(wdActiveEndSectionNumber)
... returns a -1

The range rgChp is created by a function call but the Selection Object is fine when the range is defined.

Can requesting the Bookmarks collection from a Range affect the Selection Object?

macropod
08-24-2018, 05:05 PM
If your code is selecting anything, that suggests it's quite inefficient. And nothing you've posted indicates what whatever is selected at that point might be. Since you're unwilling to post all of the relevant code, we'd only be guessing what's going on.

simon.a10
08-28-2018, 12:29 AM
Hi.

My code isn't selecting anything, I am always careful to avoid doing that.

My code loads up the collection of "chapters" (as classes) and then, using the Selection object to get the section that the cursor is in, the user may or may not be allowed to trigger the feature they clicked on based on that test.

Somehow the Selection object loses track of the Section it is in whilst maintaining its Start / End values.

That is the bit I don't understand - how that could happen (when any code that has run is not dealing with the Selection object at all)

Sorry, I know this is't a straight-forward query but was hoping someone else had had something similar.

Thanks

Simon

macropod
08-28-2018, 01:20 AM
My code isn't selecting anything, I am always careful to avoid doing that.
On the contrary, that's exactly what your 'Selection' code shows you are doing.

simon.a10
08-28-2018, 01:34 AM
Surely, there a difference between querying information about the Selection object like this ...


iSect = Selection.Information(wdActiveEndSectionNumber)

and actually selecting something, e.g.


rgMyRange.Select

I am only doing the former of those two. Therefore my code is not selecting anything.

simon.a10
08-29-2018, 02:06 AM
Hi

I have marked this as solved as I found a workaround. Didn't fix the problem but it went away. Summary of it is ...

1) Store the Selection Object's range in a Range variable before doing anything

2) Run the collection "builder" code (this used to make the Selection Object return -1 for the Information(wdActiveEndSectionNumber) for some reason)

3) Use a .Select call to make the range stored at (1) the Selection again

This appears to work. The code never gets "-1" from Selection.Information(wdActiveEndSectionNumber) by grabbing the Selection range and then reapplying it.

I would have expected to still get "-1" during the stage (2) code as nothing in there has been changed but it doesn't happen.

Anyway, whatever is going on is just weird but hopefully no longer an issue though I don't like the "solution"

Thanks

Simon