Consulting

Results 1 to 4 of 4

Thread: Selection Anomoly

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,388
    Location

    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:

    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?

    Cross posted at: https://www.msofficeforums.com/word-...tml#post182059 and https://eileenslounge.com/viewtopic.php?f=26&t=41116
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,388
    Location
    Well I wallowed around in this one long enough. It seems that just adding 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

    Visit my website: http://gregmaxey.com

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,217
    Location
    Glad you got that sorted out Greg. Its hard for us mere mortals to tread where the gurus wander.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,388
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •