Selection

The most common selection that you'll see is text selected in a Word document or in an e-mail message. The text appears inverted as you select its characters. You can tell that a shape or an object is selected by the sizing handles that appear at the corners and along the edges of the shape's or object's rectangle. In Excel, a thick black border around the range of cells tells you that a range of cells is selected. You work with selections every day. For example, you select a cell in a worksheet and add a formula or a text value. You select a word in a document and make it bold. You select a shape in PowerPoint and set its background fill to yellow.

The user selects content and applies some formatting to it by clicking on a menu item or toolbar button. Solutions often use the current user selection in order to format the selection or send it to some other document or application. At other times, solutions set the selection to draw the user's attention to that content. The following section tells you how to determine and manipulate what the selection is and how to set the selection for cases like the spelling checker or the Find and Replace functionality.

Retrieving the Selection

You can retrieve the selection from the active window. In fact, you can retrieve it from any window, whether it's in view or not. Depending on the application, the window remembers the selection; so when you navigate from one window to another, the selection becomes visible when the window is activated. Selection is window-based, not document-based.

Word

The most common selection in Word is text. For example, you select a paragraph to indent it an inch. Or you select a word and click the Underline button on the Formatting toolbar. A selection can also exist when you see the cursor only in between two characters or at the end of a line. Word understands this selection as an insertion point (IP). When you select a row in a table, Word also understands this as a row selection and not just a selection of text.

You may also select a shape or picture, and Word again sees this as a different selection. In this case, Word indicates the selection by placing sizing handles around the shape or picture (instead of highlighting the text). Because a document may have different content types, Word has different types of selections. For example, when you select a word in a document, you'll see the Bold and Italic buttons enabled. But when you select a picture, the Bold and Italic buttons are disabled. Word can't apply the bold and italic attributes to a picture, only to text. So just as Word determines what the selection type is in order to update its menus and toolbars, you also need to determine what the selection is.

When you determine what the selection is, you can use the Type property of the Selection object. The Type property returns one of the WdSelectionType constant values. The following procedure shows a Select Case statement that contains all of the possible values that the Type property can return. Based on the type of selection, your code will do different things.

Sub GetSelectionInWord()
    Dim oSel As Word.Selection
    Set oSel = ActiveWindow.Selection
    Select Case oSel.Type
        Case wdNoSelection '0
        Case wdSelectionIP '1
        Case wdSelectionNormal '2
        Case wdSelectionFrame '3
        Case wdSelectionColumn '4
        Case wdSelectionRow '5
        Case wdSelectionBlock '6
        Case wdSelectionInlineShape '7
        Case wdSelectionShape '8
    End Select
End Sub

The table below indicates when the selection types listed in the above procedure or in the WdSelectionType constant in the Object Browser in the Microsoft Visual Basic Editor are returned.

Word selection type It's returned when…
wdNoSelection No selection exists.
wdSelectionIP A blinking cursor resides anywhere in text between two characters, or at the beginning or end of a line, including text in a table cell or shape.
wdSelectionNormal A contiguous range of text is selected. This is the most common selection type. It's also returned when a combination of table and text is selected.
wdSelectionFrame A frame is selected. (Frames were commonly used in versions of Word prior to Word 97.)
wdSelectionColumn A column in a table is selected. It's also returned when one or more than one cell is selected in a table. If more than one cell is selected, this value is returned whether the cells are in the same row or in the same column.
wdSelectionRow A row in a table is selected (it has to be a full row, as when the user clicks to the left of the row). It's also returned when the full table is selected.
wdSelectionBlock When the user holds down the ALT key and uses the mouse to select text. The user can block only text, not a table.
wdSelectionInlineShape The shape within the text is an inline shape. If a shape like a picture is inline, the "In line with text" wrapping style is highlighted on the Layout tab in the Format Picture dialog box.
wdSelectionShape The shape is over or under the text. In the Format AutoShape dialog box, on the Layout tab, the "In front of text" wrapping style is highlighted.

Format the Text Selection to Bold

When you want to format text in a document, several selections may indicate that text is actually selected or the cursor exists within text characters. Just determining whether the selection is of type wdSelectionNormal won't handle all the cases where the user may have text selected. Text exists within a block selection or within a table cell, column, or row. The following procedure uses a Select Case statement to handle selection types that indicate that text is contained within the selection. The commas used within the Case expression indicate that the selection type can be any of the types listed in the Case expression.

Sub ApplyFormatToTextSelection()
    Select Case ActiveWindow.Selection.Type
        Case wdSelectionIP, wdSelectionNormal, _
            wdSelectionBlock, wdSelectionColumn, wdSelectionRow
            ActiveWindow.Selection.Font.Bold = True
        Case Else
            MsgBox "Please select range of text."
    End Select
End Sub

NOTE
With the exception of the line of code that sets the font to Bold, this is a generic procedure that's applicable to any scenario in which you need to determine if the selection is text and therefore, if you can apply text methods and properties to the selection. This procedure is used as the basis for the sample in Chapter 10 under the heading "Set Up the WindowSelectionChange Event in Word."

Insert a New Paragraph Before the Text Selection

The following procedure adds a new paragraph by first inserting a new paragraph before the current selection and then setting the text range of the newly inserted paragraph. The selection must be either an insertion point or a normal text selection in the active document. This also means that if the insertion point exists in a table cell or if a set of characters is selected in a table cell, the procedure inserts a new paragraph in the table cell.

Sub InsertNewParagraph()
    Select Case ActiveWindow.Selection.Type
        Case wdSelectionIP, wdSelectionNormal
            With ActiveWindow.Selection.Paragraphs(1)
                .Range.InsertParagraphBefore
                .Previous.Range = "New paragraph." & Chr(13)
            End With
        Case Else
            MsgBox "Please select range of text."
    End Select
End Sub

As discussed in Chapter 6 under the heading "Determining Where Text Belongs," you can use the Paragraphs property of the Range object to return the first paragraph of the selected text. For example, if one word is selected, ActiveWindow.Selection.Paragraphs(1) returns the paragraph that contains the selected word. In the preceding procedure, a new paragraph is inserted before the first paragraph of the selected range of text. Then, using the Previous property on the Paragraph object, the newly inserted paragraph is returned and its text is set to "New paragraph," followed by a carriage return. The carriage return character is represented by using the built-in language function Chr and passing into the function the value of 13.

Excel

The most common selection in Excel is a range of cells. For example, you select a cell and enter a formula or value into it. Or you select a range of cells and use their data to build a new chart. If you select one cell, a set of contiguous cells, or a discontiguous range of cells, Excel returns the type of selection as a Range object. Like Word and PowerPoint, Excel never mixes the type of content in a selection. For example, you can't select a cell and a shape at the same time. You select either a drawing shape, a chart element on a chart sheet, or a range.

Unlike Word and PowerPoint, however, Excel doesn't have a Selection object. Excel does consistently have a Selection property on the Window object, but in Excel the Selection property returns a specific object based on the selection and not a Selection object as in Word and PowerPoint. What does this mean? In Word and PowerPoint, you'd use the Type property on the Selection object to determine the selection type. In Excel, neither a Selection object nor an equivalent for the selection Type property exists. However, you can use the built-in Visual Basic for Applications function called TypeName to determine what type of object Excel returns from the ActiveWindow.Selection property.

In the following procedure, you use the TypeName function in the Visual Basic for Applications language to determine what type of object is returned by ActiveWindow.Selection. Essentially, the result is similar to using the Type property on the Selection object in Word and PowerPoint, except that the TypeName function returns a string of the name of the object type, whereas the Type property returns a constant value defined by Word and PowerPoint.

Sub GetSelectionInExcel()
    Dim oSel As Object
    Set oSel = ActiveWindow.Selection
    Select Case TypeName(oSel)
        Case "Range"
        Case "Picture"
        Case "OLEObject"
        Case "ChartArea"
        Case "ChartTitle"
        Case "PlotArea"
        Case "Series"
        Case "Point"
        Case "Gridlines"
        Case "Axis"
        Case "AxisTitle"
        Case "Legend"
        Case "LegendEntry"
        Case "DataTable"
        Case "DataLabel"
        Case "DataLabels"
        Case "Nothing"
        Case Else
    End Select
End Sub

In this procedure, the Case expression "Nothing" indicates that the active sheet in Excel is a chart but no object on the chart is selected. The Case Else expression indicates that every other selection is a drawing shape or a picture. Each drawing shape returned by ActiveWindow.Selection returns a specific drawing object. For example, a rectangle returns an object of type Rectangle.

NOTE
When the cursor is in a cell or in the text of a shape, no commands from the object model can be executed. Excel is in a modal state. In this state Excel prevents any property or method executed in Visual Basic from accessing or modifying any part of the Excel application or any workbook or content. In this case, if the Visual Basic Editor is displayed, you cannot click within the editor's window. When the cursor is not in a cell or in the text of a shape, the Excel application and any workbook or content can be accessed or modified through Visual Basic code.

The following table indicates when the object types are returned from the TypeName function.

Excel selection type It's returned when…
Range Any single cell or adjacent or nonadjacent range of cells on any worksheet is selected.
Picture A picture inserted on a worksheet or chart is selected.
OLEObject An embedded object inserted with the Insert Object dialog box is selected.
ChartArea; ChartTitle; PlotArea; Series; Point; Gridlines; Axis; AxisTitle; Legend; LegendEntry; DataTable; DataLabel; DataLabels Anything on a chart sheet or on a chart on a worksheet is selected.
Line, Rectangle, Oval, TextBox A drawing shape is selected.

Set the Font Color of Cells to Red

In Excel you usually manipulate a selection over a range of cells. To manipulate a range of cells, you use the Selection property on the Window object to return an object of type Range. The following procedure uses a Select Case statement to handle the type name of the object returned by the Selection property. If the type name is Range, the font color of the cell is set to red. In this procedure, ActiveWindow.Selection returns a Range object. The Font property is on the Range object and it returns the Font object, whose Color property is used to set the color.

Sub SetCellFormatting()
    Select Case TypeName(ActiveWindow.Selection)
        Case "Range"
            ActiveWindow.Selection.Font _
                .Color = RGB(255, 0, 0)
        Case Else
            MsgBox "Please select a range."
    End Select
End Sub

Set the Text of Selected Cells

If you'd like to display the Auto List Members drop-down list as you work with the selection, you should declare a variable of the type of object you'll use in your code. For example, in the following procedure, after you type the word rngSel and press the period key, the procedure will display the Auto List Members drop-down list, showing the methods and properties of the Range object.

Sub SetCellText()
    Dim rngSel As Excel.Range
    Select Case TypeName(ActiveWindow.Selection)
        Case "Range"
            Set rngSel = ActiveWindow.Selection
            rngSel.Value = "New text."
        Case Else
            MsgBox "Please select a range."
    End Select
End Sub

Retrieve the Text and Value From a Range of Selected Cells

In the previous example, you first select more than one cell and run the procedure. After execution is complete, the string New Text appears in every cell in the range. With the same cells selected, you can retrieve the value from the range of selected cells. First, you need to determine if the selection does contain more than one cell. If so, the IsArray function provided by the Visual Basic for Applications language returns a value of True after evaluating the rngSel object variable.

Sub GetTextFromRangeSelection()
    Dim rngCell As Range, rngSel As Range
    
    If TypeName(ActiveWindow.Selection) <> "Range" Then Exit Sub
    Set rngSel = ActiveWindow.Selection
    
    If IsArray(rngSel) Then
        For Each rngCell In rngSel
            Debug.Print rngCell.Text
            Debug.Print rngCell.Value
        Next rngCell
    End If
End Sub

A For Each...Next loop then iterates through each cell in the selection. The Value property returns the cell value. The property can also be used to assign a new value to the cell, as the previous example reveals. The Text property returns the cell text. Unlike the use of the Text property in Word and PowerPoint, you can use the Text property in Excel only to retrieve cell text, not to set the cell's text. You use the Value property instead to set text.

PowerPoint

The most common selection in PowerPoint is a shape. For example, you select a shape and set the background fill color to green or add a border. Whether you have one shape selected or more than one, PowerPoint interprets the selection as a shape range selection. If text is selected in a shape, PowerPoint interprets the selection as a text range selection. You can never have both a text range and a shape range selected, just one or the other. You also can have no selection when you're working with a slide. If you click a slide in Slide view or Normal view (new in PowerPoint 2000), no shape or text is selected. PowerPoint also provides other views of the content in the presentation, such as Outline view and Slide Sorter view (both of which also have different selection types).

As in Word, when you determine what the selection is in PowerPoint, you can use the Type property of the Selection object. The Type property returns one of the values from the constant PpSelectionType. However, PowerPoint has fewer selection types than Word. The following procedure shows a Select Case statement that contains all of the possible values that the Type property can return. Based on the type of selection, your code will do different things.

Sub GetSelectionInPowerPoint()
    Dim oSel As PowerPoint.Selection
    Set oSel = ActiveWindow.Selection
    Select Case oSel.Type
        Case ppSelectionNone '0
        Case ppSelectionSlides   '1
        Case ppSelectionShapes '2
        Case ppSelectionText '3
    End Select
End Sub

The following table indicates when the selection types listed in the above procedure or in the PpSelectionType constant in the Object Browser in the Visual Basic Editor are returned.

PowerPoint selection type It's returned when the selection is…
ppSelectionNone
  • In Slide view or Normal view (PowerPoint 2000 only) and no shape or text is selected.
  • In Slide Sorter view and no slide preview is selected; just a blinking cursor between slide previews is displayed.
  • In Notes Page view and nothing is selected. In Normal view, if the active slide in the view changes, the selection Type property will return ppSelectionNone.
  • ppSelectionSlides
  • In Slide Sorter view and slide preview is selected.
  • ppSelectionShapes
  • In a slide, Notes Page or Handout view and handles around a shape appear.
  • ppSelectionText
  • In a shape where the adjust handles and a shaded selection rectangle appear around the shape.
  • In Outline view or in the notes pane in Normal view (PowerPoint 2000 only). In either of these cases, this selection type constant is returned only when one or more text characters are selected. If an insertion point exists (that is, a blinking cursor exists in between two text characters), the selection type constant returned in this case is ppSelectionNone, or the value of 0.
  • Set the Font Name of Selected Text or All Text in a Shape

    When you want to format text in a shape, the selection type should be ppSelectionText. When you can manipulate a range of text, the Selection property on the DocumentWindow object in PowerPoint will return ppSelectionText. The following procedure uses a Select Case statement to handle the selection type, and if the selection type is text, the font name is set to Arial.

    Sub SetFontFormatting()
        Select Case ActiveWindow.Selection.Type
            Case ppSelectionText
                ActiveWindow.Selection.TextRange _
                    .Font.Name = "Arial"
            Case Else
                MsgBox "Please select a shape or text."
        End Select
    End Sub
    

    Set the Font Name of Selected Shapes That Can Contain Text

    In the above procedure, the Case ppSelectionText expression indicates that the selection must be text. In PowerPoint, even though you may have selected a shape, you can still access the text of the shape using the TextRange property of the Selection object. If you do have a shape selected that can contain text (shapes like a picture can't contain text), you can use the same code to change the font name, just as if the selection had been text. But what if your selection contained one shape that can contain text and another that couldn't (like a line, a picture or a WordArt shape)? In the first procedure on the facing page, you add a new Case expression explicitly to handle just the shape selection type. Within the Case expression that handles the value of ppSelectionShapes, the procedure SetFontNameOfShapes is called.

    In the second procedure, you use a For…Each loop to iterate through the shapes in the selection. The statement Shp.HasTextFrame in the If…Then statement determines whether each shape in the selection contains a text frame. For any shape that does, the font name of the shape is set to Arial; for any shape that doesn't, the loop continues to the next shape in the selection.

    Sub SetFontFormattingForAllShapesSelected()
        Select Case ActiveWindow.Selection.Type
            Case ppSelectionText
                ActiveWindow.Selection.TextRange _
                    .Font.Name = "Arial"
            Case ppSelectionShapes
                SetFontNameOfShapes
            Case Else
                MsgBox "Please select a shape or text."
        End Select
    End Sub
    
    Sub SetFontNameOfShapes()
        Dim shp As PowerPoint.Shape
        For Each shp In ActiveWindow _
            .Selection.ShapeRange
    
            If shp.HasTextFrame Then
                shp.TextFrame.TextRange _
                    .Font.Name = "Arial"
            End If
        Next shp
    End Sub
    

    A number of shapes don't contain a text frame. For example, a line, a picture, a WordArt shape, and any OLE object inserted on the slide—such as a Chart, a Word document, or a sound—do not have a text frame.

    Set the Font Name of Text in a Table

    Add the following If…Then statement just after the End If line in the above SetFontNameOfShapes procedure. Use the If…Then statement to determine if the shape in the selection range is of the type msoTable. Each shape in Word, Excel, and PowerPoint has a Type property. The Type property returns a value from the MsoShapeType constant defined in the Microsoft Office 9.0 Object Library. If the shape type is msoTable, the two For…Next loops iterate through each cell in the table. Each cell in a table is ultimately just a shape in PowerPoint. Once your code accesses a specific cell in a table, you can use the Shape object to manipulate the formatting and content of the cell.

    If shp.Type = msoTable Then
        Dim i As Integer, j As Integer
        For i = 1 To shp.Table.Columns.Count
            For j = 1 To shp.Table.Rows.Count
                shp.Table.Columns(i).Cells(j) _
                    .Shape.TextFrame.TextRange _
                    .Font.Name = "Arial"
            Next j
        Next i
    End If
    

    Outlook

    In Outlook, the selection is based on the different types of items that Outlook stores. If you look through your Mailbox, you'll see the breakdown by the types of folders that are displayed after you install Outlook. You have an Inbox that contains mail items, a calendar with meeting requests and appointments, a Notes folder containing notes, a Contacts folder, and a couple of other folders. If you want to programmatically determine what the selected item is in Outlook, you can use the following procedure. Like Excel, Outlook doesn't have a Selection object. You have to use the built-in Visual Basic for Applications function called TypeName to determine what type of item object Outlook returns from the ActiveExplorer.Selection property.

    The following procedure shows a Select Case statement that contains all of the possible string values that the built-in Visual Basic for Applications function TypeName can return when evaluating the expression ActiveExplorer.Selection. Depending on the type of selection, your code will do different things. The TypeName string returns similar values that are listed in the OlItemType. In most cases it's obvious when the string values are returned. A MailItem object is returned from ActiveExplorer.Selection.Item(1) when the first selected item is a mail message.

    Sub GetItemSelectionInOutlook()
        If ActiveExplorer.Selection.Count < 1 Then Exit Sub
        Select Case TypeName(ActiveExplorer _
            .Selection.Item(1))
            Case "MailItem"
            Case "AppointmentItem"
            Case "ContactItem"
            Case "JournalItem"
            Case "NoteItem"
            Case "TaskItem"
            Case "PostItem"
            Case "DistributionListItem"
        End Select
    End Sub
    

    Open All E-Mail Messages Selected

    In Outlook, the selection can contain many selected items. You need to use the Item method to iterate through the selected items. If a folder in your Mailbox contains multiple item types such as mail, contact, and task items, the following procedure iterates through each item in the selection and determines if the object returned is of type MailItem. If it is, the procedure opens the mail item and displays it on screen. Declaring the variable oMailItem as Outlook.MailItem and setting it to the generically declared oItem allows you to use the Auto List Members drop-down list when you're coding with the MailItem object. This step may be redundant, but if you were using the MailItem object extensively, having the Auto List Members drop-down list available as you type your code would improve your efficiency without greatly increasing your program's size.

    Sub OpenMailItemsInSelection()
        Dim oItem As Object
        Dim oMailItem As Outlook.MailItem
        
        For Each oItem In ActiveExplorer.Selection
            Select Case TypeName(oItem)
                Case "MailItem"
                    Set oMailItem = oItem
                    oMailItem.Display
            End Select
        Next oItem
    End Sub
    

    Setting the Selection

    When you click Find in the Edit menu in Word, Excel, or PowerPoint, the Find dialog box is displayed. You can then enter the string you want to find in the Find What text box. When you click the Find or Find Next button in the dialog box and the application finds a string in the document that matches the find criteria in the Find dialog box, it will scroll through the document to the string and select it. With the Find and even the spell checking functionalities, the application conducts three actions. First, it navigates to the content; second, it scrolls until the content is in view; third, it actually selects the content.

    Navigating to content and scrolling until it's in view is discussed later in this chapter in the section titled "Finding Content on the Screen." You use the Select method to achieve the last action, selecting content. You'll find the Select method throughout the suite of Office object libraries. Depending on the application, however, in order to select content you have to ensure that the content is visible in the active window. If it's not, the Select method will fail. For example, if you select cell D21 on the second worksheet in an Excel workbook and the first worksheet is currently displayed, the Select method generates an error. That's why navigating to the content and ensuring that it's in the active window in some cases is the first step to selecting content.

    Word

    In Word, the Select method exists for a number of objects, including the Range, Table, Cell, Row, Bookmark, Field, Shape, and InlineShape objects. You'll see a number of features that select the text range. The Find dialog box selects a text string that matches the search criteria, and the spelling and grammar checker selects the text string that's misspelled or contains incorrect grammar. You can use the Select method on any text range in the document, including text in headers and footers.

    You also can use the Select method on any content element, such as a shape in the document. Unlike Excel and PowerPoint, Word lets you set the selection in any window regardless of whether that window is active. As you'll see in Excel and PowerPoint, before selecting the content you need to make the workbook or presentation active, along with the worksheet or slide.

    Select the First Paragraph in the Document

    The following procedure selects the range that represents the first paragraph in the active window. You use the ScrollIntoView method after the Select method so the user can see the selection. In "Using the ScrollIntoView Method" later in this chapter, you'll read more about the ScrollIntoView method so you can scroll the selection—or any object that's not selected—into view.

    Sub SetSelection()
        Dim rngText As Word.Range
        Set rngText = ActiveWindow.Document _
            .Paragraphs.First.Range
        rngText.Select
        ActiveWindow.ScrollIntoView rngText
    End Sub
    

    Select the Whole Paragraph of the Current Insertion Point or Selection

    Word allows you to determine which paragraph in any range of text contains the text range. For example, if you had a character selected (or just an insertion point in a paragraph), you could use properties on the Range object to return the word in which the character or insertion point were contained. You can also determine which sentence or paragraph contains the character or insertion point. A useful scenario for this type of capability is an electronic document reader that starts reading a document from the current selection. If the document reader reads a paragraph at a time and starts reading with the first paragraph containing the selection, it can easily identify the paragraph, select it, and send it to a program that can read the text back to you.

    The following procedure determines whether the selection is an insertion point or a normal text selection. If it's either one, the procedure selects the first paragraph containing the selection and scrolls to display it. You can also use rngSel.Words(1).Select or rngSel.Sentences(1).Select, respectively, to select the first word or sentence in the selection (where rngSel represents a text range). To see this, create a new Word document, insert two paragraphs of text, and place the cursor in the middle of the first paragraph. Run the following procedure to select the paragraph containing the selection.

    Sub SelectWholeParagraph()
        Dim rngSel As Word.Range
        Select Case ActiveWindow.Selection.Type
            Case wdSelectionIP, wdSelectionNormal
                Set rngSel = ActiveWindow.Selection.Range
                rngSel.Paragraphs(1).Range.Select
                ActiveWindow.ScrollIntoView rngSel
        End Select
    End Sub
    

    Determine What Page the Current Selection Is On

    You can use the Information property on the Range object to determine information about a range of text. This information includes whether the range of text is in a table, in an endnote or footnote area in Print Layout view, or in an endnote or footnote pane in Normal view. The most common information you can retrieve about a range of text, especially selected text, is the information you see in the status bar at the bottom left of the document window.

    You can get information like the page number, section number, number of pages, line number, and column number wherever the current selection exists. In the following procedure, the constant value wdActiveEndPageNumber retrieves the page number where the end of the selection exists. If the selection or range of text spanned two pages, the wdActiveEndPageNumber value would return the page number at the end of the selection.

    Sub GetPageNumberOfSelection()
        Select Case ActiveWindow.Selection.Type
            Case wdSelectionIP, wdSelectionNormal
                MsgBox "End of selection is on page " & _
                    ActiveWindow.Selection.Range _
                    .Information(wdActiveEndPageNumber)
        End Select
    End Sub
    

    Excel

    In Excel, the Select method exists on all objects that the ActiveWindow.Selection property returns. See the "Excel" part of "Retrieving the Selection" earlier in this chapter for the list of all the objects returned by ActiveWindow.Selection. The main thing to remember when using the Select method in Excel is, unlike in Word, you can't set the selection unless the content the Select method acts on is in view in the active window. For example, when two workbooks are open and the second is the active workbook, if you want to set the selection in the first document you first have to make its window active and then set the selection. (Note that you not only have to make the workbook active, but have to make the worksheet active as well.)

    Select a Specific Cell on the Active Worksheet

    The following procedure makes an arbitrary cell active. The code first ensures that the active sheet is a worksheet (not a chart), and then selects cell H10 in the active window. The procedure scrolls the selection into view so the user can see it. In the case where the user changes the active sheet, you might want to programmatically move the active cell to A1 (or any specific cell), for example:

    Sub SetSelection()
        Dim rngCell As Excel.Range
        If TypeName(ActiveSheet) = "Worksheet" Then
            Set rngCell = ActiveSheet.Range("H10")
            rngCell.Select
            ActiveWindow.ScrollIntoView rngCell.Left, _
                rngCell.Top, rngCell.Width, rngCell.Height
        End If
    End Sub
    

    Select a Specific Cell on a Specific Worksheet and Workbook

    In scenarios where you know the workbook and worksheet you want to work with and select content on, you first need to activate the workbook and worksheet and then set the selection on the range of cells. Once you know the worksheet and workbook, you can use the following procedure. The same holds true if you need to select content on a chart sheet: you first need to activate the chart sheet and then set the selection. To see the following procedure work, start Excel, create two workbooks, make the second workbook active, and then run the procedure.

    Sub ActivateSheetAndSelect()
        Workbooks(1).Worksheets(1).Activate
        Workbooks(1).Worksheets(1).Range("H10").Select
    
        With ActiveWindow.Selection
            ActiveWindow.ScrollIntoView.Left, _
                .Top, .Width, .Height
        End With
    End Sub
    

    If you comment out the first line, make the second workbook active again, and then run the procedure, you'll get the error "Select method of Range class failed" and Visual Basic for Applications will break on the line of code containing the Select method.

    PowerPoint

    In PowerPoint, the Select method exists on either the TextRange, Shape, ShapeRange, Slide, or SlideRange objects and the table objects Cell, Column, and Row (in PowerPoint 2000 only). When you want to select either text in a shape or the shape itself, you need to ensure that either the shape containing the text or just the shape itself exists on the slide currently displayed in the active window.

    Select the Title Placeholder on the Slide in the Active Window

    The following procedure selects the title placeholder on the slide in the active window. The If…Then statement within the With…End block uses the HasTitle property on the Shapes collection of a slide to determine if a title placeholder exists. If it does, the title placeholder is selected.

    Sub ActivateWindowSetSelection()
        With ActiveWindow.View.Slide.Shapes
            If .HasTitle Then .Placeholders(1).Select
        End With
    End Sub
    

    Select a Specific Shape on a Specific Slide and Presentation

    In scenarios where you know the presentation and slide you want to work with and select content on, you first need to activate the presentation, go to the slide, and then set the selection. To see the following procedure work, start PowerPoint, create a presentation with a few slides, and then save the presentation as "MyPres.ppt". Create a new presentation, and then run the following procedure. This procedure does several things. It activates the first document window belonging to the presentation "MyPres"; it changes the view to Normal; it activates the slide pane; it determines the number of slides in the presentation using Slides.Count; it uses the GotoSlide method on the View object to navigate to the last slide; and it selects the title placeholder (if the slide has one).

    Sub SetTitleSelection()
        Dim oWindow As DocumentWindow
        Dim oSlide As Slide, nSlides As Integer
        Dim oPres As Presentation
    
        Set oPres = Presentations("MyPres")
        Set oWindow = oPres.Windows(1)
        With oWindow
            .Activate
            .ViewType = ppViewNormal
            .Panes(2).Activate
            nSlides = oPres.Slides.Count
            .View.GotoSlide nSlides
            Set oSlide = .View.Slide
            If oSlide.Shapes.HasTitle Then
                oSlide.Shapes.Placeholders(1).Select 
            End If
        End With
    End Sub