【问题标题】:Adjusting Font for Different Sections of Same Paragraph - Excel VBA为同一段落的不同部分调整字体 - Excel VBA
【发布时间】:2016-04-21 22:59:54
【问题描述】:

我通过 VBA 从 Excel 将数据输入 Word 文档。我想像这样格式化 下列的。

1. Service Ticket# 452345: Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut Labore et dolore magna aliqua。

Service Ticket 部分以粗体和斜体显示,并且描述仅以斜体显示。服务票号和说明来自 Excel 中的不同单元格。

这是我的代码:

'' Bookmark where I will be inserting the data.
wdApp.Selection.GoTo what:=-1, Name:="Service_Ticket_Comments"

Dim i As Integer
Dim serviceTicket As String

For i = 4 To shAuditTrail.Range("A4").End(xlDown).Row


    '' Pulling comments from the comments column.
    If Not IsEmpty(Cells(i, 11)) Then

          serviceTicket = "Service Ticket #" & Cells(i, 1)

          wdApp.Selection.TypeText "Service Ticket #" & Cells(i, 1)
          wdApp.Selection.Font.Bold = True
          wdApp.Selection.Font.Italic = True


          wdApp.Selection.TypeText " - " & Cells(i, 11) & Chr(10) & Chr(10)
          wdApp.Selection.Font.Italic = True
    End If

Next

这会使服务单部分和描述加粗和斜体,因为选择应用于整个部分。我如何只保持第一部分粗体和斜体,而另一部分只是粗体?

【问题讨论】:

    标签: excel vba ms-word


    【解决方案1】:

    不要使用Selection,而是使用Range,就像在 Excel 中一样。

    目前还不清楚目标 Word 文档中的 Selection 来自哪里,所以我别无选择,只能使用它作为起点。但通常你只会在用户选择目标的情况下使用这种技术......

    Dim rng as Word.Range
    Set rng = wdApp.Selection.Range
    rng.Text = serviceTicket
    rng.Font.Bold = True
    rng.Font.Italic = True
    rng.Collapse wdCollapseEnd
    rng.Text = Cells(i,11) & vbCr
    rng.Font.Bold = False
    rng.Font.Italic = True
    

    注意:您实际上应该为这种格式创建/使用字符样式,而不是直接应用格式。这会产生更高效的代码和更易于管理的文档。

    【讨论】:

    • 您的代码将所有服务票号放在首位,然后是描述。
    • 我的代码只是为了演示如何使用 Range 对象来插入一些东西,格式化它,插入一些别的东西,以及以不同的方式格式化它。您需要提供生成服务编号和描述的逻辑 - 我只是从您的代码中提取了一些信息,以便给我的一些上下文以使其更易于理解。
    【解决方案2】:

    您可以使用 Range Characters 属性格式化单元格中的特定字符。 (见https://msdn.microsoft.com/en-us/library/office/ff198232.aspx) 例如,以下例程将斜体并将所有文本加粗,直到并包括范围内每个单元格的搜索字符串。在这种情况下,正在寻找的字符串也作为参数传递。

    Sub TestMacro()
       Call BoldItalicsPartofCell(Selection, ":")
    End Sub
    
    
    Sub BoldItalicsPartofCell(rng As Range, searchstr)
       Dim singlecell As Range     'Loop counter
       Dim beforecolon As Integer  'Starting position of search string
    
       For Each singlecell In rng
          beforecolon = InStr(singlecell, searchstr) - 1 + Len(searchstr)
          If beforecolon > 0 Then
             With singlecell.Characters(Start:=1, Length:=beforecolon).Font
                .FontStyle = "Bold Italic"
                '.Underline = True  'If you want udnerlined as well!
             End With
          End If
       Next
    End Sub
    

    【讨论】:

      【解决方案3】:

      使用InsertAfter

      wdApp.Selection.TypeText "Service Ticket #" & Cells(i, 1)
      wdApp.Selection.Font.Bold = True
      wdApp.Selection.Font.Italic = True
      
      wdApp.Selection.InsertAfter " - " & Cells(i, 11) & Chr(10) & Chr(10)
      wdApp.Selection.Font.Bold = False
      wdApp.Selection.Font.Italic = True
      

      【讨论】:

        【解决方案4】:

        这并不优雅,但我做到了。

        If Not IsEmpty(Cells(i, 11)) Then
        
                        With wdApp.Selection
        
                                .Paragraphs.Indent
                                .Font.Bold = True
                                .Font.Italic = True
                                .TypeText Chr(10) & Chr(10) & numList & ".  "
        
        
                                .Font.Underline = True
                                .TypeText "Service Ticket #" & Cells(i, 1) & ":"
        
        
                                .Font.Bold = False
                                .Font.Underline = False
                                .TypeText "  " & Cells(i, 11)
                                .Paragraphs.Reset
                        End With
        
                  numList = numList + 1
        
            End If
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-16
          • 2014-09-15
          • 1970-01-01
          • 1970-01-01
          • 2021-04-24
          • 2011-05-23
          • 2023-01-04
          • 1970-01-01
          相关资源
          最近更新 更多