如何更改Microsoft Word表格中突出显示单元格中文本的样式?

huangapple go评论121阅读模式
英文:

How can one change the style of a text in a highlighted cell in a table in MS Word?

问题

I have a table in MS Word.
我在MS Word中有一个表格。

I want to change the style of a few cells to a defined style in the document -- "NewStyle09."
我想将几个单元格的样式更改为文档中定义的样式 - "NewStyle09"。

If I have to change the style of a selected text, I can do the following:
如果我需要更改所选文本的样式,可以执行以下操作:

Selection.Range.Style = "NewStyle09"
Selection.Range.Style = "NewStyle09"

However, I am unable to extend this concept to changing the style of a highlighted cell in a table.
然而,我无法将这个概念扩展到更改表格中突出显示的单元格的样式。

Can you please help.
请问你能帮忙吗?

Update:
更新:

When I select more than one contiguous cells, the following code changes the style of text in the other cells that are adjacent to the ones that I select. However, I only want the cells that I select to change their style.
当我选择多个相邻的单元格时,以下代码会更改与我选择的单元格相邻的其他单元格中的文本样式。然而,我只想要我选择的单元格更改它们的样式。

'change style of table cell
'更改表格单元格的样式
Sub TargetSelectedCells()
    Dim tbl As Table
    Dim rng As Range
    
    ' Check if a table is selected
    ' 检查是否选择了表格
    If Selection.Information(wdWithInTable) Then
        Set tbl = Selection.Tables(1)
        
        ' Check if cells are selected
        ' 检查是否选择了单元格
        If Selection.Cells.Count > 0 Then
            Set rng = Selection.Range
            
            ' Do something with the selected cells
            ' 对所选单元格执行操作
            ' For example, change the background color
            ' 例如,更改背景颜色
            rng.Style = "HON White"
            'rng.Shading.BackgroundPatternColor = wdColorLightBlue
        End If
    End If
End Sub
'更改表格单元格的样式
Sub TargetSelectedCells()
    Dim tbl As Table
    Dim rng As Range
    
    ' 检查是否选择了表格
    If Selection.Information(wdWithInTable) Then
        Set tbl = Selection.Tables(1)
        
        ' 检查是否选择了单元格
        If Selection.Cells.Count > 0 Then
            Set rng = Selection.Range
            
            ' 对所选单元格执行操作
            ' 例如,更改背景颜色
            rng.Style = "HON White"
            'rng.Shading.BackgroundPatternColor = wdColorLightBlue
        End If
    End If
End Sub
英文:

I have a table in MS Word.
I want to change the style of a few cells to a defined style in the document -- "NewStyle09".

If i have to change the style of a selected text, I can do the following:
Selection.Range.Style = "NewStyle09"

However, I am unable to extend this concept to changing the style of a highlighted cell in a table.

Can you please help.

Update:

When I select more than one contiguous cells, the following code changes the style of text in the other cells that are adjacent to the ones that i select. However, i only want the cells that i select to change its style.

'change style of table cell
Sub TargetSelectedCells()
    Dim tbl As Table
    Dim rng As Range
    
    ' Check if a table is selected
    If Selection.Information(wdWithInTable) Then
        Set tbl = Selection.Tables(1)
        
        ' Check if cells are selected
        If Selection.Cells.Count > 0 Then
            Set rng = Selection.Range
            
            ' Do something with the selected cells
            ' For example, change the background color
            rng.Style = "HON White"
            'rng.Shading.BackgroundPatternColor = wdColorLightBlue
        End If
    End If
End Sub

答案1

得分: 1

你可以参考所选单元格的方法是使用 Selection.Range.Cells(1)

以下是更改每个所选单元格样式的代码:

Sub formatSelectedCell()
Dim rg As Word.Range
Set rg = Selection.Range

Dim c As Cell
If Selection.Information(wdWithInTable) = True Then
    For Each c In rg.Cells
        c.Range.Style = "NewStyle09"
    Next
End If

End Sub
英文:

You can reference the cell of a selection use Selection.Range.Cells(1)

Sub formatSelectedCell()
Dim rg As Word.Range
Set rg = Selection.Range

If Selection.Information(wdWithInTable) = True Then
    rg.Cells(1).Range.Style = "NewStyle09"
End If

End Sub

The code checks if selection is within table.

To change style of each selected cell use this code:

Sub formatSelectedCell()
Dim rg As Word.Range
Set rg = Selection.Range

Dim c As Cell
If Selection.Information(wdWithInTable) = True Then
    For Each c In rg.Cells
        c.Range.Style = "NewStyle09"
    Next
End If

End Sub

答案2

得分: 0

当我选择多个连续的单元格时,以下代码会更改与我选择的单元格相邻的其他单元格中的文本样式。然而,我只想要更改我选择的单元格的样式。

请注意样式有段落级字符级,如果您只想应用于一个单元格,您应该在您的"NewStyle09"中使用字符级。请参考以下链接:

https://stackoverflow.com/questions/76178613/why-cant-i-apply-two-different-styles-within-the-same-paragraph-using-vba

https://stackoverflow.com/questions/76169107/changing-style-of-selection-works-but-isnt-reflected-in-the-document-ribbon-bar/76169284?noredirect=1#comment134334245_76169284

你是否在开始之前检查了我提醒你的内容?

你能给我一个测试样本,特别包括你自己定义的样式 "NewStyle09" 吗?

英文:

> When I select more than one contiguous cells, the following code changes the style of text in the other cells that are adjacent to the ones that i select. However, i only want the cells that i select to change its style.

> Note the style have paragraph level and character level, if you just want to apply to a cell , you should use the character level one for your "NewStyle09" . Refer to

<https://stackoverflow.com/questions/76178613/why-cant-i-apply-two-different-styles-within-the-same-paragraph-using-vba>

<https://stackoverflow.com/questions/76169107/changing-style-of-selection-works-but-isnt-reflected-in-the-document-ribbon-bar/76169284?noredirect=1#comment134334245_76169284>

Did you check what I reminded you before first?

Could you give me a sample to test, especially including the style "NewStyle09" you defined by yourself?

huangapple
  • 本文由 发表于 2023年6月13日 17:55:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定