View Single Post
 
Old 02-12-2024, 02:05 AM
Italophile Italophile is offline Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 542
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

In Word, whilst there is an open document there is always a selection, as the cursor must always be somewhere in the document.

If you select something in code and don't want that to be selected when your code has finished, you must move the selection somewhere else. A common way to do this is to record what was selected at the start and re-select it at the end. For example:
Code:
    Dim initSel As Range: Set initSel = Selection.Range
    'working code
    initSel.Select
However, a better approach is to avoid using Selection in your code and work with the objects directly. There are very few occasions where Selection has to be used. Mostly you would work with the Range object instead.

For the specific case in your question it is not necessary to select the entire row. To prevent errors, your code should first check that the selection is inside a table. Then you can access the row and its cells from the selection, as below.
Code:
Sub TblCellShadeOf_TEN_Percent()
    If Selection.Information(wdWithInTable) Then
        With Selection.Rows(1).Cells
            With .Shading
                .Texture = wdTextureNone
                .ForegroundPatternColor = wdColorAutomatic
                .BackgroundPatternColor = wdColorGray10
            End With
        End With
    End If
End Sub
Reply With Quote