Vba 创建标题的脚本

Vba 创建标题的脚本,vba,ms-word,Vba,Ms Word,我有一个DOORS创建的word文档,其中表格中的标题写为“1”、“1.1”、“2.2.3”等(见图) 有没有一种方法可以编写宏或vba脚本来搜索给定列中以数字开头的单元格,并删除数字并为行应用其中一种样式? 例如: 从样式选择中将“1”和“2”替换为“标题1” 从样式选择中将“1.1”和“2.3”替换为“标题2” 将“1.1.3”和“2.3.4”替换为样式选择中的“标题3” 等等 提前谢谢 亲切问候,, 克劳斯(Klaus)我用了一下午的时间来解决这个问题,现在它起作用了:-) 代码可能

我有一个DOORS创建的word文档,其中表格中的标题写为“1”、“1.1”、“2.2.3”等(见图)

有没有一种方法可以编写宏或vba脚本来搜索给定列中以数字开头的单元格,并删除数字并为行应用其中一种样式?
例如:

  • 从样式选择中将“1”和“2”替换为“标题1”
  • 从样式选择中将“1.1”和“2.3”替换为“标题2”
  • 将“1.1.3”和“2.3.4”替换为样式选择中的“标题3”
  • 等等
提前谢谢

亲切问候,,
克劳斯(Klaus)

我用了一下午的时间来解决这个问题,现在它起作用了:-)

代码可能会更漂亮,但它是有效的。 以防其他人需要此功能

子应用程序标题样式() 如表所示的尺寸tbl 暗淡的T细胞作为细胞 作为整数的Dim r

For Each tbl In ActiveDocument.Tables
    For r = 1 To tbl.Rows.Count
        Set tCell = tbl.Cell(r, 3) ' check only row 3
        If tCell.Range.Text Like "#.#.#.#.#.#*" Then ' search for heading number consisting of x.x.x.x.x.x
            tCell.Range.Text = Right(tCell.Range.Text, Len(tCell.Range.Text) - 12) ' remove old heading numbers
            tCell.Range.Text = Replace(tCell.Range.Text, Chr(13), "") ' remove Carrige Return at end of string
            tCell.Range.Style = ActiveDocument.Styles("Heading 6")
        End If
    Next r
Next tbl

For Each tbl In ActiveDocument.Tables
    For r = 1 To tbl.Rows.Count
        Set tCell = tbl.Cell(r, 3) ' check only row 3
        If tCell.Range.Text Like "#.#.#.#.#*" Then ' search for heading number consisting of x.x.x.x.x
            tCell.Range.Text = Right(tCell.Range.Text, Len(tCell.Range.Text) - 10) ' remove old heading numbers
            tCell.Range.Text = Replace(tCell.Range.Text, Chr(13), "") ' remove Carrige Return at end of string
            tCell.Range.Style = ActiveDocument.Styles("Heading 5")
        End If
    Next r
Next tbl

For Each tbl In ActiveDocument.Tables
    For r = 1 To tbl.Rows.Count
        Set tCell = tbl.Cell(r, 3) ' check only row 3
        If tCell.Range.Text Like "#.#.#.#*" Then ' search for heading number consisting of x.x.x.x
            tCell.Range.Text = Right(tCell.Range.Text, Len(tCell.Range.Text) - 8) ' remove old heading numbers
            tCell.Range.Text = Replace(tCell.Range.Text, Chr(13), "") ' remove Carrige Return at end of string
            tCell.Range.Style = ActiveDocument.Styles("Heading 4")
        End If
    Next r
Next tbl

For Each tbl In ActiveDocument.Tables
    For r = 1 To tbl.Rows.Count
        Set tCell = tbl.Cell(r, 3) ' check only row 3
        If tCell.Range.Text Like "#.#.#*" Then ' search for heading number consisting of x.x.x
            tCell.Range.Text = Right(tCell.Range.Text, Len(tCell.Range.Text) - 6) ' remove old heading numbers
            tCell.Range.Text = Replace(tCell.Range.Text, Chr(13), "") ' remove Carrige Return at end of string
            tCell.Range.Style = ActiveDocument.Styles("Heading 3")
        End If
    Next r
Next tbl

For Each tbl In ActiveDocument.Tables
    For r = 1 To tbl.Rows.Count
        Set tCell = tbl.Cell(r, 3) ' check only row 3
        If tCell.Range.Text Like "#.#*" Then ' search for heading number consisting of x.x
            tCell.Range.Text = Right(tCell.Range.Text, Len(tCell.Range.Text) - 4) ' remove old heading numbers
            tCell.Range.Text = Replace(tCell.Range.Text, Chr(13), "") ' remove Carrige Return at end of string
            tCell.Range.Style = ActiveDocument.Styles("Heading 2")
        End If
    Next r
Next tbl

结束子代码的刷起。也一样

公共函数getHeadingNumber(ByRef s作为字符串)作为整数 作为整数的Dim i 将ws设置为字符串

If s Like "#.#*" Then ' is it a heading (note: heading 1 are not found)
    i = InStr(s, " ") ' search for first space charater
    ws = Left(s, i) ' keep only digits and bullets in ws
    getHeadingNumber = 1 + Len(ws) - Len(Replace(ws, ".", "")) ' count number of bullets
    
    s = Right(s, Len(s) - i) ' keep only the 12 left most characters
    s = Replace(s, Chr(13), "") ' remove Carrige Return at end of string
Else
    getHeadingNumber = 0 ' not a heading
End If
For Each tbl In ActiveDocument.Tables
    For r = 1 To tbl.Rows.Count
        Set tCell = tbl.Cell(r, 3) ' check only row 3
        ws = tCell.Range.Text
        heading = getHeadingNumber(ws)
        If heading > 0 Then
            tCell.Range.Text = ws
            Select Case heading
                Case 1
                    tCell.Range.Style = ActiveDocument.Styles("Heading 1")
                Case 2
                    tCell.Range.Style = ActiveDocument.Styles("Heading 2")
                Case 3
                    tCell.Range.Style = ActiveDocument.Styles("Heading 3")
                Case 4
                    tCell.Range.Style = ActiveDocument.Styles("Heading 4")
                Case 5
                    tCell.Range.Style = ActiveDocument.Styles("Heading 5")
                Case 6
                    tCell.Range.Style = ActiveDocument.Styles("Heading 6")
            End Select
        End If
    Next r
Next tbl

' Set heading in "Test Description"
For Each tbl In ActiveDocument.Tables
    Set tCell = tbl.Cell(2, 3)
    If tCell.Range.Text Like "1*" Then ' search for heading
        tCell.Range.Text = Right(tCell.Range.Text, Len(tCell.Range.Text) - 2) ' remove old heading numbers
        tCell.Range.Text = Replace(tCell.Range.Text, Chr(13), "") ' remove Carrige Return at end of string
        tCell.Range.Style = ActiveDocument.Styles("Heading 1")
    End If
Next tbl
端函数

子应用程序标题样式() 如表所示的尺寸tbl 暗淡的T细胞作为细胞 作为整数的Dim r 将标题设置为整数 将ws设置为字符串

If s Like "#.#*" Then ' is it a heading (note: heading 1 are not found)
    i = InStr(s, " ") ' search for first space charater
    ws = Left(s, i) ' keep only digits and bullets in ws
    getHeadingNumber = 1 + Len(ws) - Len(Replace(ws, ".", "")) ' count number of bullets
    
    s = Right(s, Len(s) - i) ' keep only the 12 left most characters
    s = Replace(s, Chr(13), "") ' remove Carrige Return at end of string
Else
    getHeadingNumber = 0 ' not a heading
End If
For Each tbl In ActiveDocument.Tables
    For r = 1 To tbl.Rows.Count
        Set tCell = tbl.Cell(r, 3) ' check only row 3
        ws = tCell.Range.Text
        heading = getHeadingNumber(ws)
        If heading > 0 Then
            tCell.Range.Text = ws
            Select Case heading
                Case 1
                    tCell.Range.Style = ActiveDocument.Styles("Heading 1")
                Case 2
                    tCell.Range.Style = ActiveDocument.Styles("Heading 2")
                Case 3
                    tCell.Range.Style = ActiveDocument.Styles("Heading 3")
                Case 4
                    tCell.Range.Style = ActiveDocument.Styles("Heading 4")
                Case 5
                    tCell.Range.Style = ActiveDocument.Styles("Heading 5")
                Case 6
                    tCell.Range.Style = ActiveDocument.Styles("Heading 6")
            End Select
        End If
    Next r
Next tbl

' Set heading in "Test Description"
For Each tbl In ActiveDocument.Tables
    Set tCell = tbl.Cell(2, 3)
    If tCell.Range.Text Like "1*" Then ' search for heading
        tCell.Range.Text = Right(tCell.Range.Text, Len(tCell.Range.Text) - 2) ' remove old heading numbers
        tCell.Range.Text = Replace(tCell.Range.Text, Chr(13), "") ' remove Carrige Return at end of string
        tCell.Range.Style = ActiveDocument.Styles("Heading 1")
    End If
Next tbl
端接头