Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-16-2024, 06:33 AM
gmaxey gmaxey is offline Selection Anomoly Windows 10 Selection Anomoly Office 2019
Expert
Selection Anomoly
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default Selection Anomoly

I was working on a larger project and have stumbled on an anomaly that I can't work out/understand the underlying cause:

The following procedures illustrate:



Code:
Option Explicit
Sub Demo_LostSelection_AnomolyI()
Dim oRng As Range
Dim oDocTmp As Document
  ActiveDocument.Range.Text = "One two three"
  ActiveDocument.Words(1).Select
  Set oDocTmp = Documents.Add
  oDocTmp.Close wdDoNotSaveChanges
  'Notice after the procedure runs that the selection is reset to the start of the page.
lbl_Exit:
  Exit Sub
End Sub
Sub Demo_LostSelection_AnomolyII()
Dim oRng As Range
Dim oDocTmp As Document
  ActiveDocument.Range.Text = "One two three"
  ActiveDocument.Words(1).Select
  Set oDocTmp = Documents.Add
  'The problem seems to manifest from closing the temporary document.
  'Set that out, and the selection in the original document is preserved.
  'oDocTmp.Close wdDoNotSaveChanges
lbl_Exit:
  Exit Sub
End Sub
Sub Demo_LostSelection_AnomolyIII()
Dim oRng As Range
Dim oDocTmp As Document
Dim i
  ActiveDocument.Range.Text = "One two three"
  ActiveDocument.Words(1).Select
  Set oDocTmp = Documents.Add
  'The problem seems to manifest from closing the temporary document.
  oDocTmp.Close wdDoNotSaveChanges
  'The problem does not persist if there is a breakpoint in the code.  Let the code
  'run to the stop then run to end.
  Stop
lbl_Exit:
  Exit Sub
End Sub
Sub Demo_LostSelection_AnomolyIV()
Dim oRng As Range
Dim oDocTmp As Document
  ActiveDocument.Range.Text = "One two three"
  ActiveDocument.Words(1).Select
  Set oDocTmp = Documents.Add
  'The problem seems to manifest from closing the temporary document.
  oDocTmp.Close wdDoNotSaveChanges
  'The problem persists even if you attempt to reselect the original selection after closing the temporay file.
  ActiveDocument.Words(1).Select
lbl_Exit:
  Exit Sub
End Sub
Sub Demo_LostSelection_AnomolyV()
Dim oRng As Range
Dim oDocTmp As Document
  ActiveDocument.Range.Text = "One two three"
  ActiveDocument.Words(1).Select
  Set oDocTmp = Documents.Add
  'The problem seems to manifest from closing the temporary document.
  oDocTmp.Close wdDoNotSaveChanges
  '... even though that text was still selected
  MsgBox Selection.Text
  '... or if you attempt to make another selection
  ActiveDocument.Words(3).Select
  MsgBox Selection.Text
  'For whatever reason, closing the temporary document and letting the procedure run to completion
  'without a breakpoint destroys the selection (resets it to the top of the page).
lbl_Exit:
  Exit Sub
End Sub
Why does closing the temporary document and letting the code run to completion cause the selection to reset to top of page?
Why does adding the breakpoint (Stop) prevent this behavior?
Anyone know of a solution to prevent this behavior without resorting to breakpoint?



Crossposted at: Selection Anomoly and Selection Anomoly - Eileen's Lounge
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 06-16-2024, 09:56 AM
gmaxey gmaxey is offline Selection Anomoly Windows 10 Selection Anomoly Office 2019
Expert
Selection Anomoly
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Well, I wallowed around in this one long enough. It seems that simply adding a DoEvents after the Documents.Add method eliminates the anomaly.

Sub Demo_LostSelection_AnomolyI()
Dim oRng As Range
Dim oDocTmp As Document
ActiveDocument.Range.Text = "One two three"
ActiveDocument.Words(1).Select
Set oDocTmp = Documents.Add
DoEvents
oDocTmp.Close wdDoNotSaveChanges
'Notice after the procedure runs that the selection is reset to the start of the page.
lbl_Exit:
Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 06-16-2024, 06:18 PM
Guessed's Avatar
Guessed Guessed is offline Selection Anomoly Windows 10 Selection Anomoly Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Greg
Without the DoEvents, on my machine the selection doesn't change so I think we can say there is variable behaviour with the selection.

Another alternative you might also add to the experiment list is to add the temp doc invisibly. Is it closing the temp doc that is the issue on your machine or changing the activedocument?
Code:
Set oDocTmp = Documents.Add(Visible:=False)
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 06-17-2024, 03:33 AM
gmaxey gmaxey is offline Selection Anomoly Windows 10 Selection Anomoly Office 2019
Expert
Selection Anomoly
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Andrews,



I had already tried that. But I do have an update. Coming back to this issue a little later yesterday, I discovered that DoEvents was not the solution. The solution was realizing the whole issue was my own fault.


I tried the process with my installation of Word 2010 and 2003 and like others have reported, there was no issue. Something about my installation of Word 2019 was the culprit. Then I remembered that years ago, I had created a small procedure to ensure the correct (or my preferred) view and zoom when I open Word (Word 2019) or create a new document.



It was an element in that procedure that was resetting the selection. It was perplexing simply because I was focusing on Document.Close as the culprit (which it seems to be) but actually it was the Document.Add which triggered the external procedure. I'm still not clear why the issue doesn't appear if the new document is left open or if the Stop is inserted but can't spend more time trying to fix something no longer broken. Thanks for posting.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Selection Anomoly Selection.Information not working as intended for selection position m2ramos Word VBA 3 11-07-2022 05:54 AM
Help with Selection VBA JamesWood Word VBA 6 11-17-2020 06:46 AM
Selection Anomoly Is there a way to go to the next selection? wardw Word 2 09-25-2019 09:19 AM
Selection Anomoly Selection.Bookmarks("\headinglevel") WITHOUT Selection NobodysPerfect Word VBA 3 01-14-2015 12:58 PM
Selection Anomoly Selection of all Text for a specific page in word is spanning selection across pages ramsgarla Word VBA 9 12-05-2012 03:23 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:28 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft