none
Weird Control in VSTO RRS feed

  • Question

  • I am wondering why this code will select everything, make it bold and italic:

        Private Sub Sheet1_Startup() Handles Me.Startup

     

            Dim Selection As Excel.Range = Me.Application.Selection

            Me.Application.Range("A2:B3").Select()

            Me.Application.Selection.Font.Bold = True

            Me.Application.Selection.Font.Italic = True

     

        End Sub

     

    However, this will select everything, and make only cell A2 bold and italic:

        Private Sub Sheet1_Startup() Handles Me.Startup

     

            Dim Selection As Excel.Range = Me.Application.Selection

            Range("A2:B3").Select()

            Selection.Font.Bold = True

            Selection.Font.Italic = True

     

        End Sub

     

    It would seem like the control for Me.Application.Selection should be transferred to Dim Selection As Excel.Range.

     

    However, it doesn’t seem like VSTO is actually doing that.  What is the purpose of:

    Dim Selection As Excel.Range = Me.Application.Selection

     

    I would expect that code to act kind of like a With...End With in Excel.

    It seems like that would save a lot of work for the developer, but it actually doesn’t do anything at all.

     

    Thursday, December 15, 2011 9:00 PM

Answers

  • Hi ryguy72,

     

    Thanks for posting in the MSDN Forum.

     

    I’m not able to reproduce your issue.

     

    I get the “A2:B3” range’s bold and italic as true via snippet 1 and “a1”,”a2:b3” range’s bold italic as true (the active is “a1” before the snippet run) via snippet 2.

     

     

    I suppose that you have the A2 cell active before you snippet run. In the second snippet you variable "Selection" only keep the address of the A2 cell, and you didn't change the point to the new range at the following code.

     

    I hope it can help you.

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, December 19, 2011 9:17 AM
    Moderator