Consulting

Results 1 to 2 of 2

Thread: VBA to return to user's original selection of object after running actions?

  1. #1

    VBA to return to user's original selection of object after running actions?

    (Question asked on MS Answers without a solution so far at this link: LINK NOT ALLOWED)

    Word 2013

    What is the best way to code this? I want to note the current selection (could be text, shape, other object, etc.), then run some actions (e.g., create a shape and select THAT new shape) and then return to the original selection. (The actions MUST literally select the new object, no way around it for this project. Ordinarily, I would not change selections, but it's a must for this goal.)


    Below is the code I have, which works GREAT for text. As expected, it changes selection and returns selection to the text layer or text within an object. BUT when the original selection is a non-text object (i.e., a shape, diagram, etc.), it produces an abnormal AND dysfunctional selection marker on the object when it returns the selection. The selection marker looks odd, and is dysfunctional – the selection allows for object deletion but is NOT recognized as being selected for further actions such as formatting. For that, I have to manually click again on the object to return to the selection properly, which defeats the purpose of VBA reselection. (See pic below where the second circle was selected. The first pic shows the original selection, and the bottom pic shows VBA having reselected it – which is odd looking and dysfunctional.)

    before after.jpg

    Here is the code I'm using (abbreviated). You can try it (create a shape in your document and then run the code that changes selection and reselects) and see it does the same for you.

    Again, the question is how can this be coded differently to properly reselect an object? Or is this a known bug? Or is Word VBA incapable of reseelcting an object? (I have it working in Excel/PPT.)

    Thanks. (Code below.)

    Sub DoSumpinAndReselect()
    '>>>>>>>>> Runs actions and then returns to original selection user had whether that was text or objects.
    
    
    'Note the selection in the document to later return to.
       Dim selCurrent As Range
       Set selCurrent = Selection.Range
    
    
    '[Run actions]
       Dim sh As Object
       Set sh = ActiveDocument.Shapes.AddTextbox _
          (msoTextOrientationHorizontal, 72, 72, 12, 12)
    
    
    'Reset original selection
       selCurrent.Select
    
    
    End Sub
    Last edited by Eljay Ishere; 07-29-2015 at 02:15 PM.

  2. #2

Posting Permissions

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