【问题标题】:MS WORD 2010 Using VBA to type text into table cell then reformat and type more text w/o deleting previous textMS WORD 2010 使用 VBA 在表格单元格中键入文本,然后重新格式化并键入更多文本而不删除以前的文本
【发布时间】:2023-03-07 02:17:01
【问题描述】:

我正在使用此代码,它专门将文本放置在我想要使用此代码的精确单元格中:

    Dim myText1 As String
    Dim myText2 As String
    myText1 = "Header"
    myText2 = "Body"

    With ActiveDocument.Tables(1).Cell(2, 2).Range

   .Font.Name = "Times New Roman"
    .Font.Size = 12
   .Font.Bold = True
   .Font.Underline = True   
    .Text = myText1 & vbCr & vbCr & myText2
End With

我遇到的问题是“myText2”不应该加下划线或粗体。

我试过这个:

    Dim myText1 As String
    Dim myText2 As String
    myText1 = "Header"
    myText2 = "Body"

    With ActiveDocument.Tables(1).Cell(2, 2).Range

   .Font.Name = "Times New Roman"
    .Font.Size = 12
   .Font.Bold = True
   .Font.Underline = True   
    .Text = myText1 & vbCr & vbCr
   .Font.Bold = False
   .Font.Underline = False  
    .Text = myText2

End With

但发生的情况是第一个 myText1 被删除,剩下的只有 myText2。

还有这个

    With ActiveDocument.Tables(1).Cell(2, 2).Range
       .Font.Name = "Times New Roman"
.Font.Size = 12
.Font.Bold = True
.Font.Underline = True
.InsertAfter myText1 & vbCr & vbCr
.Font.Bold = False
.Font.Underline = False    
.InsertAfter myText2

虽然这会附加文本,但整个帖子的格式没有下划线或粗体,而最终结果应该看起来像

标题

身体

我怎样才能重新格式化 myText2,让它发布,而不会丢失上面格式化的 myText1?

【问题讨论】:

    标签: vba ms-word word-2010


    【解决方案1】:

    在您的代码中,您已将With 语句设置为使用单元格的整个范围。这会导致将格式应用于整个单元格。

    您不必使用Selection 对象来应用格式,您只需要确保您使用的是正确的范围。使用Selection 对象会使代码在移动光标时运行得更慢。

    我在下面重写了你的代码。

    Sub AddTextToCell()
        Dim myText1 As String
        Dim myText2 As String
        myText1 = "Header"
        myText2 = "Body"
    
        With ActiveDocument.Tables(1).Cell(2, 2).Range
            .Text = myText1 & vbCr & vbCr & myText2
            With .Font
                .Name = "Times New Roman"
                .Size = 12
                .Bold = False
                .Underline = False
            End With
            With .Paragraphs.First.Range.Font
                .Bold = True
                .Underline = True
            End With
        End With
    End Sub
    

    【讨论】:

    • 不错,似乎是一个更好的解决方案!
    【解决方案2】:

    通常最好以您的方式输入文本,而不使用Select,但是当将不同格式应用于单元格的不同部分时,我认为您必须使用它。我不得不更改格式的顺序,并在文档中稍作改动以使其正常工作:

        Dim myText1 As String
        Dim myText2 As String
        myText1 = "Header"
        myText2 = "Body"
    
        With ActiveDocument.Tables(1).Cell(2, 2).Range
            .Font.Name = "Times New Roman"
            .Font.Size = 12
            .Font.Bold = True
            .Font.Underline = True
            .Text = myText1 & vbCr & vbCr
        End With
    
        'Select the whole cell
        ActiveDocument.Tables(1).Cell(2, 2).Select
    
        'Move to the right
        Selection.Collapse Direction:=wdCollapseEnd
        'Move back to the left
        Selection.MoveLeft wdCharacter, 1
    
        'Add the text (using the myText1 format)
        Selection.Range.Text = myText2
    
        'Select the on word the right (myText2)
        Selection.MoveRight wdWord, 1, True
    
        'Format myText2
        Selection.Range.Font.Underline = False
        Selection.Range.Font.Bold = False
    

    【讨论】:

    • 这是朝着正确的方向前进,但“正文”是一个有几行文字的段落。您的代码可以确保标题下方的一个单词不是粗体或下划线,但对于整个段落,只有第一个单词是纯文本,段落的其余部分是粗体和下划线。
    • OK - 需要修改Selection.MoveRight wdWord, 1, True 行以说明添加的所有文本。文本的数量是否已知 - 是否总是一个段落?
    • 总是 1 段。我正在使用代码来计算字数,然后像这样插入该变量:Selection.MoveRight wdWord,word_count,True。那行得通,但是如果有一种方法可以引用一个段落,那可能会涉及更少的代码。我已经尝试过 Selection.MoveRight wdParagraph, 1, True 但这会将整个单元格重新格式化为纯文本,包括标题。这就是为什么我去计算字数的原因。谢谢你的回复。
    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    相关资源
    最近更新 更多